feat: Add wallet creation and access

This commit is contained in:
Nathan Woodburn 2024-06-03 14:16:29 +10:00
parent 9c0d1085ae
commit f0e6dcfbbe
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -1,4 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class IndexPage extends StatefulWidget { class IndexPage extends StatefulWidget {
IndexPage( IndexPage(
@ -18,40 +20,88 @@ class IndexPage extends StatefulWidget {
class _IndexPageState extends State<IndexPage> { class _IndexPageState extends State<IndexPage> {
late String wallet; late String wallet;
List<String> walletNames = [];
@override @override
void initState() { void initState() {
super.initState(); super.initState();
wallet = widget.wallet; wallet = widget.wallet;
fetchWalletNames();
} }
@override Future<void> fetchWalletNames() async {
Widget build(BuildContext context) { final response = await http.get(
return Scaffold( Uri.parse('https://api.firewallet.au/account?uuid=${widget.uuid}'));
appBar: AppBar( if (response.statusCode == 200) {
title: Text(wallet), final data = jsonDecode(response.body);
), final wallets = data['wallets'] as List;
// Get wallet list from api and display here setState(() {
body: Center( walletNames =
child: LayoutBuilder( wallets.map((wallet) => wallet['name'] as String).toList();
builder: (BuildContext context, BoxConstraints constraints) { });
return Column( } else {
mainAxisAlignment: MainAxisAlignment.center, // Handle error
children: <Widget>[ print('Failed to load wallet names');
TextButton( }
onPressed: () => setWallet('wallet1'), }
child: Text('Wallet 1')),
TextButton( Future<void> addNewWallet(String name, String xpub) async {
onPressed: () => setWallet('wallet2'), final response = await http.post(
child: Text('Wallet 2')), Uri.parse(
TextButton( 'https://api.firewallet.au/wallet?uuid=${widget.uuid}&name=$name&xpub=$xpub'),
onPressed: () => setWallet('wallet3'), );
child: Text('Wallet 3')),
], if (response.statusCode == 200) {
); // Refresh the wallet list after adding a new wallet
}, fetchWalletNames();
), } else {
), // Handle error
print('Failed to add new wallet');
}
}
void showNewWalletDialog() {
String name = '';
String xpub = '';
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('Add New Wallet'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
TextField(
decoration: InputDecoration(labelText: 'Name'),
onChanged: (value) {
name = value;
},
),
TextField(
decoration: InputDecoration(labelText: 'XPUB'),
onChanged: (value) {
xpub = value;
},
),
],
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
addNewWallet(name, xpub);
},
child: Text('Add'),
),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Cancel'),
),
],
);
},
); );
} }
@ -61,4 +111,39 @@ class _IndexPageState extends State<IndexPage> {
}); });
widget.setWallet(s); widget.setWallet(s);
} }
int getWalletBalance() {
return 100;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('$wallet - ${getWalletBalance()} HNS'),
),
body: Center(
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
...walletNames.map((name) {
return TextButton(
onPressed: () => setWallet(name),
child: Text(name),
);
}).toList(),
SizedBox(height: 20),
ElevatedButton(
onPressed: showNewWalletDialog,
child: Text('New Wallet'),
),
],
);
},
),
),
);
}
} }