loc-chain-app/loc_chain_app/lib/pages/home.dart

82 lines
2.3 KiB
Dart
Raw Normal View History

2021-04-10 15:26:20 +00:00
import 'package:flutter/material.dart';
2021-04-11 02:52:37 +00:00
import 'package:flutter_udid/flutter_udid.dart';
import 'package:loc_chain_app/util/bluetooth.dart';
import 'package:loc_chain_app/util/transaction_manager.dart';
2021-04-10 15:26:20 +00:00
class HomePage extends StatefulWidget {
2021-04-10 17:58:52 +00:00
HomePage({Key? key, required this.title}) : super(key: key);
2021-04-10 15:26:20 +00:00
final String title;
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
2021-04-11 02:52:37 +00:00
final Future<String> _id = FlutterUdid.consistentUdid;
Connect connector = Connect();
Map<String, Transaction> transactionMap = Map();
// String getId() =>
// SharedPreferences.getInstance().then((s) => s.getString('id') ?? '0');
2021-04-10 15:26:20 +00:00
2021-04-11 02:52:37 +00:00
void refreshTransactions() {
2021-04-10 15:26:20 +00:00
setState(() {
2021-04-11 02:52:37 +00:00
transactionMap = Connect.transactionMap;
Connect.context = context;
2021-04-10 15:26:20 +00:00
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
2021-04-11 02:52:37 +00:00
body: FutureBuilder<String>(
future: _id, // a previously-obtained Future<String> or null
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
String content = "Loading id...";
if (snapshot.hasData) {
content = "ID: ${snapshot.data!}";
}
List<Card> transactions = List.generate(
transactionMap.length,
(index) {
Transaction t = transactionMap.values.elementAt(index);
return Card(
child: Row(
children: [Text(t.hash), Text(t.pubKey)],
),
);
},
);
return ListView(
children: <Widget>[
Container(
child: Text(content),
),
] +
transactions,
);
}),
2021-04-10 15:26:20 +00:00
floatingActionButton: FloatingActionButton(
2021-04-11 02:52:37 +00:00
onPressed: () {
refreshTransactions();
try {
Connect.stop();
Connect.start();
} catch (e) {
print(e);
}
},
child: Icon(Icons.refresh_sharp),
2021-04-10 15:26:20 +00:00
),
);
}
2021-04-11 02:52:37 +00:00
void showSnackbar(dynamic a) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(a.toString()),
));
}
2021-04-10 15:26:20 +00:00
}