import 'package:cdb_ui/api.dart'; import 'package:cdb_ui/pages/supply.dart'; import 'package:cdb_ui/pages/transaction.dart'; import 'package:flutter/material.dart'; import 'package:flutter_simple_treeview/flutter_simple_treeview.dart'; class LocationsPage extends StatefulWidget { const LocationsPage({super.key}); @override State createState() => _LocationsPageState(); } class _LocationsPageState extends State { Map? locations; @override void initState() { super.initState(); locations = API().getLocations(); } TreeNode buildTree(BuildContext context, String locID) { return TreeNode( key: ValueKey(locID), content: Expanded( child: ListTile( title: Text(locations![locID]!.name), onTap: () { Navigator.of(context).push(MaterialPageRoute( builder: (context) => LocationView(locations![locID]!), )); }, ), ), 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()); } @override Widget build(BuildContext context) { if (locations == null) { return const Scaffold( body: CircularProgressIndicator(), ); } return Scaffold( appBar: AppBar( title: const Text("Locations"), ), body: TreeView( indent: 15, nodes: locations!.keys .where((key) => locations![key]!.parent == null) .map((key) { return buildTree(context, key); }).toList()), floatingActionButton: FloatingActionButton( onPressed: () async { // scan location code var code = await scanQRCode(context, title: "Scan Location Code"); 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]!), )); }, child: const Icon(Icons.qr_code), ), ); } } class LocationView extends StatefulWidget { final Location location; const LocationView(this.location, {super.key}); @override State createState() => _LocationViewState(); } class _LocationViewState extends State { bool recursive = true; void refresh() { setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.location.name), ), body: Padding( padding: const 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} C°") ], ), ), Row( children: [ Checkbox( value: !recursive, onChanged: (bool? newValue) { setState(() { recursive = !(newValue ?? false); }); }, ), const Expanded( child: Text( 'Show only exact matches with location', style: TextStyle(fontSize: 16), ), ), ], ), Expanded( child: FutureBuilder( future: API().getTransactionsOfLocation(widget.location.id, recursive: recursive), builder: (context, snapshot) { if (!snapshot.hasData) { return const CircularProgressIndicator(); } var data = snapshot.data!; return ListView( children: data .map((x) => TransactionCard(x, refresh)) .toList()); }, ), ) ], ), ), ); } }