Skip to content

Commit

Permalink
web: update example
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmove committed Oct 30, 2023
1 parent 3ea248d commit 56fb648
Show file tree
Hide file tree
Showing 10 changed files with 466 additions and 83 deletions.
25 changes: 25 additions & 0 deletions example/lib/components/button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

import 'package:flutter/material.dart';

class Button extends StatelessWidget {
Button(this.title, this.onClick, { Key? key }) : super(key: key);

String title;
Function onClick;

@override
Widget build(BuildContext context) {
return Container(
child: SizedBox(
width: 150,
height: 50,
child: ElevatedButton(
child: Text(title, textAlign: TextAlign.center),
onPressed: () {
onClick();
},
),
),
);
}
}
32 changes: 32 additions & 0 deletions example/lib/helper/helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import 'package:another_flushbar/flushbar.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sui/sui.dart';

final suiClient = SuiClient(Constants.devnetAPI);

void showSnackBar(BuildContext context, String title, {int seconds = 3}) {
Flushbar(
icon: const Icon(Icons.info_outline),
Expand All @@ -13,6 +16,35 @@ void showSnackBar(BuildContext context, String title, {int seconds = 3}) {
).show(context);
}

void showToast(BuildContext context, String title, {int seconds = 3, Color? color}) {
Widget toast = Container(
padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 12.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
color: color ?? Colors.greenAccent,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(Icons.check),
SizedBox(
width: 12.0,
),
Text(title),
],
),
);
FToast().init(context).showToast(
gravity: ToastGravity.BOTTOM,
toastDuration: Duration(seconds: 3),
child: toast
);
}

void showErrorToast(BuildContext context, String title, {int seconds = 3}) {
showToast(context, title, seconds: seconds, color: Colors.redAccent);
}

Future<SuiAccount> getLocalSuiAccount() async {
const suiAccountKey = "sui_dart_account_key";
final SharedPreferences prefs = await SharedPreferences.getInstance();
Expand Down
217 changes: 138 additions & 79 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import 'package:example/pages/token_menu.dart';
import 'package:example/pages/faucet.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:focus_detector/focus_detector.dart';
import 'package:sui/sui.dart';
import 'package:sui/types/faucet.dart';

import 'helper/helper.dart';
import 'pages/split.dart';
import 'pages/token_menu.dart';
import 'pages/transfer.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({Key? key}): super(key: key);
const MyApp({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Sui Example',
title: 'Sui Dart Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
textSelectionTheme: const TextSelectionThemeData(
cursorColor: Color.fromRGBO(84, 150, 229, 1),
selectionColor: Colors.yellow,
selectionHandleColor: Colors.black,
),
),
home: const MyHomePage(title: 'Sui Example'),
home: const MyHomePage(title: 'Sui Dart Demo'),
builder: FToastBuilder(),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}): super(key: key);
const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

Expand All @@ -35,10 +45,8 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {

BigInt _balance = BigInt.zero;
late SuiClient suiClient;
SuiAccount? account;
late SuiAccount account;

@override
void initState() {
Expand All @@ -52,89 +60,140 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

bool requestingFaucet = false;

void _requestFaucet() async {
if (requestingFaucet || account == null) return;
void _navigateToTokenManage() {
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => TokenMenu(client: suiClient)));
}

var resp = await suiClient.getBalance(account!.getAddress());
_balance = resp.totalBalance;
if (_balance <= BigInt.zero) {
setState(() {
requestingFaucet = true;
});
int menuIndex = 0;

try {
final faucet = FaucetClient(Constants.faucetDevAPI);
final faucetResp = await faucet.requestSuiFromFaucetV1(account!.getAddress());
if (faucetResp.task != null) {
while(true) {
final statusResp = await faucet.getFaucetRequestStatus(faucetResp.task!);
if (statusResp.status.status == BatchSendStatus.SUCCEEDED) {
break;
} else {
await Future.delayed(const Duration(seconds: 3));
}
}
}
} catch(e) {
showSnackBar(context, e.toString());
} finally {
setState(() {
requestingFaucet = false;
});
}
Widget contentPage() {
switch(menuIndex) {
case 0:
return const Center(
child: Column(
children: [
Text("Sui Dart SDK", style: TextStyle(fontSize: 35)),
SizedBox(height: 20),
Text("A cross-platform Sui SDK for Mobile, Web and Desktop", style: TextStyle(fontSize: 28), textAlign: TextAlign.center)
],
)
);
case 1: return Faucet(account);
case 2: return Split(account);
case 4: return Transfer(account);
}

resp = await suiClient.getBalance(account!.getAddress());
_balance = resp.totalBalance;
return Container();
}

void menuClick(int menu) {
setState(() {
_balance = _balance;
menuIndex = menu;
});
}

void _navigateToTokenManage() {
Navigator.of(context).push(MaterialPageRoute(builder: (context) => TokenMenu(client: suiClient)));
}

@override
Widget build(BuildContext context) {
return FocusDetector(
onFocusGained: () {
_requestFaucet();
},
child:Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.only(top: 40.0),
child: Column(
children: <Widget>[
Text(
'${_balance / BigInt.from(10).pow(9)} SUI',
style: const TextStyle(fontSize: 20),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _requestFaucet,
child: Text(requestingFaucet ? "Inprogress" : "Faucet")),
const SizedBox(height: 20),
Padding(
padding: const EdgeInsets.only(left: 16, right: 16),
child: SelectableText(account?.getAddress() ?? ''),
)
],
onFocusGained: () {
// _requestFaucet();
},
child: Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
),
),
floatingActionButton: _balance != BigInt.zero ? FloatingActionButton(
onPressed: _navigateToTokenManage,
tooltip: 'TokenManager',
child: const Icon(Icons.menu),
) : null,
));
drawer: Drawer(
child: ListView(
children: [
DrawerHeader(
decoration: const BoxDecoration(
color: Color.fromRGBO(84, 150, 229, 1)
),
child: Center(
child: Column(
children: [
const Text("Sui Dart", style: TextStyle(fontSize: 20, color: Colors.white)),
const SizedBox(height: 30),
SelectableText(
account.getAddress(),
showCursor: true,
style: const TextStyle(fontSize: 14, color: Colors.white),
onTap: () {
print(account.getAddress());
},
),
],
),
)
),
ListTile(
title: const Text("Faucet"),
onTap: () {
Navigator.pop(context);
menuClick(1);
},
),
ListTile(
title: const Text("Split SUI"),
onTap: () {
Navigator.pop(context);
menuClick(2);
},
),
// ListTile(
// title: const Text(("Merge Sui")),
// onTap: () {
// Navigator.pop(context);
// menuClick(3);
// },
// ),
ListTile(
title: const Text(("Transfer Sui")),
onTap: () {
menuClick(4);
},
),
// ListTile(
// title: const Text(("Publish Package")),
// onTap: () {
// print("tap Publish...");
// },
// )
],
),
),
body: Center(
child: Padding(
padding: const EdgeInsets.only(top: 300.0),
child: contentPage()
// child: Column(
// children: <Widget>[
// Text(
// '${_balance / BigInt.from(10).pow(9)} SUI',
// style: const TextStyle(fontSize: 20),
// ),
// const SizedBox(height: 20),
// ElevatedButton(
// onPressed: _requestFaucet,
// child: Text(requestingFaucet ? "Inprogress" : "Faucet")),
// const SizedBox(height: 20),
// Padding(
// padding: const EdgeInsets.only(left: 16, right: 16),
// child: SelectableText(account?.getAddress() ?? ''),
// )
// ],
// ),
),
),
// floatingActionButton: balance != BigInt.zero
// ? FloatingActionButton(
// onPressed: _navigateToTokenManage,
// tooltip: 'TokenManager',
// child: const Icon(Icons.menu),
// )
// : null,
)
);
}
}
Loading

0 comments on commit 56fb648

Please sign in to comment.