cdb_ui/lib/pages/items.dart
2024-09-23 10:31:40 +02:00

68 lines
1.8 KiB
Dart

import 'package:cdb_ui/api.dart';
import 'package:cdb_ui/pages/itemview.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 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());
}),
floatingActionButton: FloatingActionButton(
onPressed: () {
// scan transaction code
QrBarCodeScannerDialog().getScannedQrBarCode(
context: context,
onCode: (code) {
// library is retarded
code = code!.replaceFirst("Code scanned = ", "");
API().getTransaction(code).then((t) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => TransactionPage(t),
));
});
},
);
},
child: const Icon(Icons.qr_code),
),
);
}
}
class ItemTile extends StatelessWidget {
final Item item;
const ItemTile(this.item, {super.key});
@override
Widget build(BuildContext context) {
return ListTile(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => ItemView(item: item)));
},
title: Text(item.name),
);
}
}