cdb_ui/lib/pages/stats.dart

135 lines
4 KiB
Dart
Raw Normal View History

2024-09-20 07:18:06 +00:00
import 'package:cdb_ui/api.dart';
2024-09-27 11:15:53 +00:00
import 'package:cdb_ui/pages/expandable_list.dart';
2024-09-21 15:15:12 +00:00
import 'package:cdb_ui/pages/transaction.dart';
2024-09-20 07:18:06 +00:00
import 'package:flutter/material.dart';
class StatsPage extends StatelessWidget {
const StatsPage({super.key});
Future<(List<MinItem>, List<Transaction>, GlobalItemStat)>
_fetchData() async {
return (
await API().getItemsUnderMin(),
await API().getExpiredItems(),
await API().getGlobalItemStat()
);
}
@override
Widget build(BuildContext context) {
// todo : add global statistics
// todo : demand stat
// todo : stat on origin + destination
2024-09-26 13:44:04 +00:00
// global origin / destinations
2024-09-20 07:18:06 +00:00
return Scaffold(
2024-10-07 19:48:48 +00:00
appBar: AppBar(title: Text("Home"),),
2024-09-20 07:18:06 +00:00
body: FutureBuilder(
future: _fetchData(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
var data = snapshot.data!;
var min = data.$1;
var expired = data.$2;
var globalStat = data.$3;
return Column(
children: [
Card(
2024-09-26 13:44:04 +00:00
margin: const EdgeInsets.all(10.0),
2024-09-21 15:15:12 +00:00
child: Padding(
2024-09-26 13:44:04 +00:00
padding: const EdgeInsets.all(10.0),
2024-09-21 15:15:12 +00:00
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconText(Icons.data_object,
"Items: ${globalStat.item_count}"),
IconText(Icons.list_alt,
"Inventory: ${globalStat.total_transactions}"),
IconText(Icons.money,
"Price: ${globalStat.total_price.toStringAsFixed(2)}")
],
),
)),
2024-09-20 07:18:06 +00:00
if (min.isNotEmpty)
const ListTile(title: Text("Items under Minimum")),
...min.map((item) {
return ListTile(
title: Text(
"Item ${item.itemVariant} under minimum. Needs ${item.need} more."),
);
}).toList(),
if (expired.isNotEmpty)
const ListTile(title: Text("Expired Items")),
// Mapping expired list to widgets
...expired.map((item) {
return ListTile(
title: TransactionCard(item, () {}),
);
}).toList(),
],
);
},
),
);
}
}
2024-09-26 13:44:04 +00:00
class ItemStatPage extends StatelessWidget {
final Item item;
const ItemStatPage(this.item, {super.key});
// todo : avg time of transaction active
2024-09-27 11:15:53 +00:00
ExpandableListItem buildVariantStat(String variant) {
return ExpandableListItem(
body: FutureBuilder(
future: API().getStat(item.id, variant, full: true),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
var data = snapshot.data! as FullItemVariantStat;
return Column(
children: [
Text("Amount: ${data.amount}"),
Text("Total Cost: ${data.totalPrice}"),
Text("Expiry Rate: ${data.expiryRate}"),
...data.origins.keys.map((key) {
var originStat = data.origins[key]!;
return Column(children: [
Text("Inventory: ${originStat.inventory}"),
Text(
"Average Price: ${originStat.average_price.toStringAsFixed(2)}"),
]);
}).toList()
],
);
},
),
header: Text(item.variants[variant]!.name));
}
2024-09-26 13:44:04 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Statistics of ${item.name}"),
),
2024-09-27 11:15:53 +00:00
body: Column(children: [
ExpandableList(item.variants.keys.map(buildVariantStat).toList())
]),
2024-09-26 13:44:04 +00:00
);
}
}