cdb_ui/lib/pages/locations.dart

166 lines
4.6 KiB
Dart
Raw Normal View History

2024-09-20 07:18:06 +00:00
import 'package:cdb_ui/api.dart';
2024-10-08 08:11:18 +00:00
import 'package:cdb_ui/pages/supply.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';
2024-09-26 09:32:55 +00:00
import 'package:flutter_simple_treeview/flutter_simple_treeview.dart';
2024-09-20 07:18:06 +00:00
2024-09-21 17:34:10 +00:00
class LocationsPage extends StatefulWidget {
2024-09-20 07:18:06 +00:00
const LocationsPage({super.key});
2024-09-21 17:34:10 +00:00
@override
State<LocationsPage> createState() => _LocationsPageState();
}
class _LocationsPageState extends State<LocationsPage> {
Map<String, Location>? locations;
@override
void initState() {
super.initState();
2024-09-26 19:35:28 +00:00
locations = API().getLocations();
2024-09-21 17:34:10 +00:00
}
2024-09-26 09:32:55 +00:00
TreeNode buildTree(BuildContext context, String locID) {
return TreeNode(
key: ValueKey(locID),
2024-09-26 16:17:09 +00:00
content: Expanded(
child: ListTile(
title: Text(locations![locID]!.name),
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LocationView(locations![locID]!),
));
},
),
2024-09-26 09:32:55 +00:00
),
children: locations!.keys.where((key2) {
if (locations![key2]!.parent != null) {
return locations![key2]!.parent! == locations![locID]!.id;
}
return false;
}).map((key2) {
return buildTree(context, key2);
}).toList());
}
2024-09-20 07:18:06 +00:00
@override
Widget build(BuildContext context) {
2024-09-21 17:34:10 +00:00
if (locations == null) {
return const Scaffold(
body: CircularProgressIndicator(),
);
}
return Scaffold(
appBar: AppBar(
title: const Text("Locations"),
),
2024-09-26 09:32:55 +00:00
body: TreeView(
2024-09-26 13:44:04 +00:00
indent: 15,
2024-09-26 09:32:55 +00:00
nodes: locations!.keys
.where((key) => locations![key]!.parent == null)
.map((key) {
2024-09-26 13:44:04 +00:00
return buildTree(context, key);
}).toList()),
2024-09-21 17:34:10 +00:00
floatingActionButton: FloatingActionButton(
2024-10-08 08:11:18 +00:00
onPressed: () async {
2024-09-21 17:34:10 +00:00
// scan location code
2024-10-08 08:11:18 +00:00
var code = await scanQRCode(context, title: "Scan Location Code");
2024-09-21 17:34:10 +00:00
2024-10-08 08:11:18 +00:00
if (!locations!.containsKey(code)) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('The location $code does not exist.')),
);
return;
}
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => LocationView(locations![code]!),
));
2024-09-21 17:34:10 +00:00
},
child: const Icon(Icons.qr_code),
),
);
2024-09-20 07:18:06 +00:00
}
}
class LocationView extends StatefulWidget {
final Location location;
const LocationView(this.location, {super.key});
@override
State<LocationView> createState() => _LocationViewState();
}
class _LocationViewState extends State<LocationView> {
bool recursive = true;
void refresh() {
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.location.name),
),
2024-09-21 17:34:10 +00:00
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: Column(
children: [
Card(
child: Column(
children: [
if (widget.location.parent != null)
Text("Inside: ${widget.location.parent!}"),
if (widget.location.conditions?.temperature != null)
Text(
"Temperature: ${widget.location.conditions!.temperature}")
],
),
),
Row(
2024-09-20 07:18:06 +00:00
children: [
2024-09-21 17:34:10 +00:00
Checkbox(
value: !recursive,
onChanged: (bool? newValue) {
setState(() {
recursive = !(newValue ?? false);
});
},
),
const Expanded(
child: Text(
'Show only exact matches with location',
style: TextStyle(fontSize: 16),
),
),
2024-09-20 07:18:06 +00:00
],
),
2024-09-21 17:34:10 +00:00
Expanded(
child: FutureBuilder(
future: API().getTransactionsOfLocation(widget.location.id,
recursive: recursive),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
2024-09-20 07:18:06 +00:00
2024-09-21 17:34:10 +00:00
var data = snapshot.data!;
2024-09-20 07:18:06 +00:00
2024-09-21 17:34:10 +00:00
return ListView(
children: data
.map((x) => TransactionCard(x, refresh))
.toList());
},
),
)
],
),
2024-09-20 07:18:06 +00:00
),
);
}
}