main.dart 6.7 KB

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