123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:polkawallet_sdk/api/types/networkParams.dart';
- import 'package:polkawallet_sdk/polkawallet_sdk.dart';
- import 'package:polkawallet_sdk/storage/keyring.dart';
- import 'package:polkawallet_sdk_example/pages/account.dart';
- import 'package:polkawallet_sdk_example/pages/dAppPage.dart';
- import 'package:polkawallet_sdk_example/pages/keyring.dart';
- import 'package:polkawallet_sdk_example/pages/setting.dart';
- import 'package:polkawallet_sdk_example/pages/tx.dart';
- import 'pages/staking.dart';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatefulWidget {
- @override
- _MyAppState createState() => _MyAppState();
- }
- class _MyAppState extends State<MyApp> {
- final WalletSDK sdk = WalletSDK();
- final Keyring keyring = Keyring();
- bool _sdkReady = false;
- Future<void> _initApi() async {
- await keyring.init([0, 2]);
- await sdk.init(keyring);
- setState(() {
- _sdkReady = true;
- });
- }
- void _showResult(BuildContext context, String title, res) {
- showCupertinoDialog(
- context: context,
- builder: (BuildContext context) {
- return CupertinoAlertDialog(
- title: Text(title),
- content: SelectableText(res, textAlign: TextAlign.left),
- actions: [
- CupertinoButton(
- child: Text('OK'),
- onPressed: () {
- Navigator.of(context).pop();
- },
- )
- ],
- );
- },
- );
- }
- @override
- void initState() {
- super.initState();
- _initApi();
- }
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Polkawallet SDK Demo',
- theme: ThemeData(
- primarySwatch: Colors.blue,
- visualDensity: VisualDensity.adaptivePlatformDensity,
- ),
- home: MyHomePage(sdk, keyring, _sdkReady),
- routes: {
- DAppPage.route: (_) => DAppPage(sdk, keyring),
- KeyringPage.route: (_) => KeyringPage(sdk, keyring, _showResult),
- SettingPage.route: (_) => SettingPage(sdk, _showResult),
- AccountPage.route: (_) => AccountPage(sdk, _showResult),
- TxPage.route: (_) => TxPage(sdk, keyring, _showResult),
- StakingPage.route: (_) => StakingPage(sdk, keyring, _showResult),
- },
- );
- }
- }
- class MyHomePage extends StatefulWidget {
- MyHomePage(this.sdk, this.keyring, this.sdkReady);
- final WalletSDK sdk;
- final Keyring keyring;
- final bool sdkReady;
- @override
- _MyHomePageState createState() => _MyHomePageState();
- }
- class _MyHomePageState extends State<MyHomePage> {
- bool _connecting = false;
- bool _apiConnected = false;
- Future<void> _connectNode() async {
- setState(() {
- _connecting = true;
- });
- final node = NetworkParams();
- node.name = 'Kusama';
- node.endpoint = 'wss://kusama.api.onfinality.io/public-ws/';
- node.ss58 = 2;
- final res = await widget.sdk.api.connectNode(widget.keyring, [node]);
- if (res != null) {
- setState(() {
- _apiConnected = true;
- });
- }
- setState(() {
- _connecting = false;
- });
- }
- @override
- Widget build(BuildContext context) {
- final Widget trailing = widget.sdkReady
- ? IconButton(
- icon: Icon(Icons.arrow_forward_ios, size: 18),
- onPressed: null,
- )
- : CupertinoActivityIndicator();
- return Scaffold(
- appBar: AppBar(
- title: Text('Polkawallet SDK Demo'),
- ),
- body: SafeArea(
- child: ListView(
- children: [
- Padding(
- padding: EdgeInsets.all(16),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text('js-api loaded: ${widget.sdkReady}'),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text('js-api connected: $_apiConnected'),
- OutlinedButton(
- child: _connecting
- ? CupertinoActivityIndicator()
- : Text(_apiConnected
- ? 'connected ${widget.sdk.api.connectedNode.name}'
- : 'connect'),
- onPressed: _apiConnected || _connecting
- ? null
- : () => _connectNode(),
- )
- ],
- ),
- ],
- ),
- ),
- Divider(),
- ListTile(
- title: Text('WebViewWithExtension'),
- subtitle: Text('open polkassembly.io (DApp)'),
- trailing: trailing,
- onTap: () {
- if (!widget.sdkReady) return;
- Navigator.of(context).pushNamed(DAppPage.route,
- arguments: "https://apps.acala.network/#/loan");
- },
- ),
- Divider(),
- ListTile(
- title: Text('sdk.keyring'),
- subtitle: Text('keyPairs management'),
- trailing: trailing,
- onTap: () {
- if (!widget.sdkReady) return;
- Navigator.of(context).pushNamed(KeyringPage.route);
- },
- ),
- Divider(),
- ListTile(
- title: Text('sdk.setting'),
- subtitle: Text('network settings'),
- trailing: trailing,
- onTap: () {
- if (!widget.sdkReady) return;
- Navigator.of(context).pushNamed(SettingPage.route);
- },
- ),
- Divider(),
- ListTile(
- title: Text('sdk.account'),
- subtitle: Text('account management'),
- trailing: trailing,
- onTap: () {
- if (!widget.sdkReady) return;
- Navigator.of(context).pushNamed(AccountPage.route);
- },
- ),
- Divider(),
- ListTile(
- title: Text('sdk.tx'),
- subtitle: Text('extrinsic actions'),
- trailing: trailing,
- onTap: () {
- if (!widget.sdkReady) return;
- Navigator.of(context).pushNamed(TxPage.route);
- },
- ),
- Divider(),
- ListTile(
- title: Text('sdk.staking'),
- subtitle: Text('staking management'),
- trailing: trailing,
- onTap: () {
- if (!widget.sdkReady) return;
- Navigator.of(context).pushNamed(StakingPage.route);
- },
- ),
- ],
- ),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
- }
- }
|