import 'package:cdb_ui/api.dart'; import 'package:cdb_ui/pages/transaction.dart'; import 'package:flutter/material.dart'; import 'package:qr_bar_code_scanner_dialog/qr_bar_code_scanner_dialog.dart'; class LocationsPage extends StatefulWidget { const LocationsPage({super.key}); @override State createState() => _LocationsPageState(); } class _LocationsPageState extends State { Map? locations; @override void initState() { super.initState(); API().getLocations().then( (value) { setState(() { locations = value; }); }, ); } @override Widget build(BuildContext context) { // todo : add locations tree view if (locations == null) { return const Scaffold( body: CircularProgressIndicator(), ); } return Scaffold( appBar: AppBar( title: const Text("Locations"), ), body: null, floatingActionButton: FloatingActionButton( onPressed: () { // scan location code QrBarCodeScannerDialog().getScannedQrBarCode( context: context, onCode: (code) { // library is retarded code = code!.replaceFirst("Code scanned = ", ""); 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: 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()); }, ), ) ], ), ), ); } }