main.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:polkawallet_sdk/api/types/networkParams.dart';
  4. import 'package:polkawallet_sdk/polkawallet_sdk.dart';
  5. import 'package:polkawallet_sdk/storage/keyring.dart';
  6. import 'package:polkawallet_sdk/storage/keyringEVM.dart';
  7. import 'package:polkawallet_sdk_example/pages/account.dart';
  8. import 'package:polkawallet_sdk_example/pages/dAppPage.dart';
  9. import 'package:polkawallet_sdk_example/pages/ethWithJS.dart';
  10. import 'package:polkawallet_sdk_example/pages/evm.dart';
  11. import 'package:polkawallet_sdk_example/pages/keyring.dart';
  12. import 'package:polkawallet_sdk_example/pages/setting.dart';
  13. import 'package:polkawallet_sdk_example/pages/tx.dart';
  14. import 'pages/staking.dart';
  15. void main() {
  16. runApp(MyApp());
  17. }
  18. class MyApp extends StatefulWidget {
  19. @override
  20. _MyAppState createState() => _MyAppState();
  21. }
  22. class _MyAppState extends State<MyApp> {
  23. final WalletSDK sdk = WalletSDK();
  24. final Keyring keyring = Keyring();
  25. final KeyringEVM keyringEVM = KeyringEVM();
  26. bool _sdkReady = false;
  27. Future<void> _initApi() async {
  28. await keyring.init([0, 2]);
  29. await keyringEVM.init();
  30. await sdk.init(keyring, keyringEVM: keyringEVM);
  31. setState(() {
  32. _sdkReady = true;
  33. });
  34. }
  35. void _showResult(BuildContext context, String title, res) {
  36. showCupertinoDialog(
  37. context: context,
  38. builder: (BuildContext context) {
  39. return CupertinoAlertDialog(
  40. title: Text(title),
  41. content: SelectableText(res, textAlign: TextAlign.left),
  42. actions: [
  43. CupertinoButton(
  44. child: Text('OK'),
  45. onPressed: () {
  46. Navigator.of(context).pop();
  47. },
  48. )
  49. ],
  50. );
  51. },
  52. );
  53. }
  54. @override
  55. void initState() {
  56. super.initState();
  57. _initApi();
  58. }
  59. @override
  60. Widget build(BuildContext context) {
  61. return MaterialApp(
  62. title: 'Polkawallet SDK Demo',
  63. theme: ThemeData(
  64. primarySwatch: Colors.blue,
  65. visualDensity: VisualDensity.adaptivePlatformDensity,
  66. ),
  67. home: MyHomePage(sdk, keyring, _sdkReady),
  68. routes: {
  69. DAppPage.route: (_) => DAppPage(sdk, keyring),
  70. KeyringPage.route: (_) => KeyringPage(sdk, keyring, _showResult),
  71. SettingPage.route: (_) => SettingPage(sdk, _showResult),
  72. AccountPage.route: (_) => AccountPage(sdk, _showResult),
  73. TxPage.route: (_) => TxPage(sdk, keyring, _showResult),
  74. StakingPage.route: (_) => StakingPage(sdk, keyring, _showResult),
  75. EVMPage.route: (_) => EVMPage(sdk, keyringEVM, _showResult),
  76. EthWithJSPage.route: (_) => EthWithJSPage(sdk, keyringEVM, _showResult),
  77. },
  78. );
  79. }
  80. }
  81. class MyHomePage extends StatefulWidget {
  82. MyHomePage(this.sdk, this.keyring, this.sdkReady);
  83. final WalletSDK sdk;
  84. final Keyring keyring;
  85. final bool sdkReady;
  86. @override
  87. _MyHomePageState createState() => _MyHomePageState();
  88. }
  89. class _MyHomePageState extends State<MyHomePage> {
  90. bool _connecting = false;
  91. bool _apiConnected = false;
  92. Future<void> _connectNode() async {
  93. setState(() {
  94. _connecting = true;
  95. });
  96. final node = NetworkParams();
  97. node.name = 'Kusama';
  98. node.endpoint = 'wss://kusama.api.onfinality.io/public-ws/';
  99. node.ss58 = 2;
  100. final res = await widget.sdk.api.connectNode(widget.keyring, [node]);
  101. if (res != null) {
  102. setState(() {
  103. _apiConnected = true;
  104. });
  105. }
  106. setState(() {
  107. _connecting = false;
  108. });
  109. }
  110. @override
  111. Widget build(BuildContext context) {
  112. final Widget trailing = widget.sdkReady
  113. ? IconButton(
  114. icon: Icon(Icons.arrow_forward_ios, size: 18),
  115. onPressed: null,
  116. )
  117. : CupertinoActivityIndicator();
  118. return Scaffold(
  119. appBar: AppBar(
  120. title: Text('Polkawallet SDK Demo'),
  121. ),
  122. body: SafeArea(
  123. child: ListView(
  124. children: [
  125. Padding(
  126. padding: EdgeInsets.all(16),
  127. child: Column(
  128. crossAxisAlignment: CrossAxisAlignment.start,
  129. children: [
  130. Text('js-api loaded: ${widget.sdkReady}'),
  131. Row(
  132. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  133. children: [
  134. Text('js-api connected: $_apiConnected'),
  135. OutlinedButton(
  136. child: _connecting
  137. ? CupertinoActivityIndicator()
  138. : Text(_apiConnected
  139. ? 'connected ${widget.sdk.api.connectedNode?.name}'
  140. : 'connect'),
  141. onPressed: _apiConnected || _connecting
  142. ? null
  143. : () => _connectNode(),
  144. )
  145. ],
  146. ),
  147. ],
  148. ),
  149. ),
  150. Divider(),
  151. ListTile(
  152. title: Text('WebViewWithExtension'),
  153. subtitle: Text('open polkassembly.io (DApp)'),
  154. trailing: trailing,
  155. onTap: () {
  156. if (!widget.sdkReady) return;
  157. Navigator.of(context).pushNamed(DAppPage.route,
  158. arguments: "https://apps.acala.network/#/loan");
  159. },
  160. ),
  161. Divider(),
  162. ListTile(
  163. title: Text('sdk.keyring'),
  164. subtitle: Text('keyPairs management'),
  165. trailing: trailing,
  166. onTap: () {
  167. if (!widget.sdkReady) return;
  168. Navigator.of(context).pushNamed(KeyringPage.route);
  169. },
  170. ),
  171. Divider(),
  172. ListTile(
  173. title: Text('sdk.setting'),
  174. subtitle: Text('network settings'),
  175. trailing: trailing,
  176. onTap: () {
  177. if (!widget.sdkReady) return;
  178. Navigator.of(context).pushNamed(SettingPage.route);
  179. },
  180. ),
  181. Divider(),
  182. ListTile(
  183. title: Text('sdk.account'),
  184. subtitle: Text('account management'),
  185. trailing: trailing,
  186. onTap: () {
  187. if (!widget.sdkReady) return;
  188. Navigator.of(context).pushNamed(AccountPage.route);
  189. },
  190. ),
  191. Divider(),
  192. ListTile(
  193. title: Text('sdk.tx'),
  194. subtitle: Text('extrinsic actions'),
  195. trailing: trailing,
  196. onTap: () {
  197. if (!widget.sdkReady) return;
  198. Navigator.of(context).pushNamed(TxPage.route);
  199. },
  200. ),
  201. Divider(),
  202. ListTile(
  203. title: Text('sdk.staking'),
  204. subtitle: Text('staking management'),
  205. trailing: trailing,
  206. onTap: () {
  207. if (!widget.sdkReady) return;
  208. Navigator.of(context).pushNamed(StakingPage.route);
  209. },
  210. ),
  211. Divider(),
  212. ListTile(
  213. title: Text('ethers'),
  214. subtitle: Text('ethers keyring'),
  215. onTap: () {
  216. Navigator.of(context).pushNamed(EVMPage.route);
  217. },
  218. ),
  219. Divider(),
  220. ListTile(
  221. title: Text('sdk.eth'),
  222. subtitle: Text('eth js keyring'),
  223. onTap: () {
  224. Navigator.of(context).pushNamed(EthWithJSPage.route);
  225. },
  226. ),
  227. Divider(),
  228. ],
  229. ),
  230. ), // This trailing comma makes auto-formatting nicer for build methods.
  231. );
  232. }
  233. }