firewallet-mobile/lib/main.dart

138 lines
3.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:firewallet/home.dart';
import 'package:firewallet/transactions.dart';
import 'package:firewallet/domains.dart';
import 'package:firewallet/receive.dart';
import 'package:firewallet/send.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
// Check for the uuid key in the shared preferences
if (!prefs.containsKey('uuid')) {
// Create an account using https://api.firewallet.au/account
// and get the uuid from the response
final response = await http.post(
Uri.parse('https://api.firewallet.au/account'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: '{}',
);
// Get uuid from { "userID": "..." }
final uuid = jsonDecode(response.body)['userID'];
// Save the uuid to the shared preferences
await prefs.setString('uuid', uuid);
}
// Get the uuid from the shared preferences
final String? uuid = prefs.getString('uuid');
print('UUID: $uuid');
runApp(MyApp(uuid: uuid ?? 'null'));
}
class MyApp extends StatelessWidget {
const MyApp({
super.key,
required this.uuid,
});
final String uuid;
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Fire Wallet',
theme: ThemeData(
brightness: Brightness.light,
/* light theme settings */
),
darkTheme: ThemeData(
brightness: Brightness.dark,
/* dark theme settings */
),
themeMode: ThemeMode.system,
debugShowCheckedModeBanner: false,
home: MyHomePage(uuid: uuid),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({
super.key,
required this.uuid,
});
final String uuid;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int currentPageIndex = 0;
String wallet = '';
@override
Widget build(BuildContext context) {
final ThemeData theme = Theme.of(context);
return Scaffold(
bottomNavigationBar: NavigationBar(
onDestinationSelected: (int index) {
setState(() {
currentPageIndex = index;
});
},
selectedIndex: currentPageIndex,
destinations: const <Widget>[
NavigationDestination(
selectedIcon: Icon(Icons.home),
icon: Icon(Icons.home_outlined),
label: 'Home',
),
NavigationDestination(
icon: Icon(Icons.call_received), label: 'Receive'),
NavigationDestination(
icon: Icon(Icons.send),
label: 'Send',
),
NavigationDestination(
icon: Icon(Icons.text_format_rounded),
label: 'Domains',
),
NavigationDestination(
icon: Icon(Icons.receipt),
label: 'Transactions',
),
],
),
body: SafeArea(
child: IndexedStack(
index: currentPageIndex,
children: <Widget>[
IndexPage(uuid: widget.uuid, wallet: wallet, setWallet: setWallet),
ReceivePage(uuid: widget.uuid, wallet: wallet),
SendPage(uuid: widget.uuid, wallet: wallet),
DomainsPage(uuid: widget.uuid, wallet: wallet),
TransactionsPage(uuid: widget.uuid, wallet: wallet),
],
),
),
);
}
void setWallet(String s) {
setState(() {
wallet = s;
});
}
}