cdb_ui/lib/main.dart
2024-09-20 08:52:56 +02:00

192 lines
4.8 KiB
Dart

import 'package:cdb_ui/api.dart';
import 'package:cdb_ui/pages/flow.dart';
import 'package:cdb_ui/pages/itemview.dart';
import 'package:cdb_ui/pages/setup.dart';
import 'package:flutter/material.dart';
Future<void> main() async {
await API().init();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'CDB',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.deepPurple, brightness: Brightness.dark),
useMaterial3: true,
),
home: API().isInit() ? const MyHomePage() : const SetupPage(),
);
}
}
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
return Scaffold(
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 global_stat = data.$3;
return Column(
children: [
Card(
child: Column(
children: [
Text("Items: ${global_stat.item_count}"),
Text("Inventory: ${global_stat.total_transactions}"),
Text("Price: ${global_stat.total_price}")
],
)),
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(),
],
);
},
),
);
}
}
class LocationsPage extends StatelessWidget {
const LocationsPage({super.key});
@override
Widget build(BuildContext context) {
// todo : add locations tree view
return const Scaffold();
}
}
class ItemsPage extends StatelessWidget {
const ItemsPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Items"),
),
body: FutureBuilder(
future: API().getItems(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
var items = snapshot.data!;
return ListView(
children: items.map((x) {
return ItemTile(x);
}).toList());
}),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int pageIndex = 0;
List<Widget> pages = [
const StatsPage(),
const ItemsPage(),
const FlowsPage(),
const LocationsPage()
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
fixedColor: Colors.white,
unselectedItemColor: Colors.white70,
items: const [
BottomNavigationBarItem(icon: Icon(Icons.home), label: "Home"),
BottomNavigationBarItem(
icon: Icon(Icons.data_object), label: "Items"),
BottomNavigationBarItem(icon: Icon(Icons.receipt), label: "Flows"),
BottomNavigationBarItem(
icon: Icon(Icons.location_city), label: "Locations"),
],
currentIndex: pageIndex,
onTap: (value) {
setState(() {
pageIndex = value;
});
},
),
body: pages[pageIndex],
);
}
}
class ItemTile extends StatelessWidget {
final String item;
const ItemTile(this.item, {super.key});
@override
Widget build(BuildContext context) {
return ListTile(
onTap: () {
API().getItem(item).then((itemInfo) => Navigator.push(context,
MaterialPageRoute(builder: (context) => ItemView(item: itemInfo))));
},
title: Text(item),
);
}
}