cdb_ui/lib/pages/itemview.dart

162 lines
4.6 KiB
Dart
Raw Normal View History

2024-09-07 21:56:52 +00:00
import 'package:cdb_ui/api.dart';
2024-09-07 22:24:13 +00:00
import 'package:cdb_ui/pages/consume.dart';
2024-09-07 21:56:52 +00:00
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'supply.dart';
2024-09-07 22:24:13 +00:00
class ItemView extends StatefulWidget {
2024-09-07 21:56:52 +00:00
final Item item;
2024-09-07 22:24:13 +00:00
@override
State<ItemView> createState() => _ItemViewState();
const ItemView({super.key, required this.item});
}
class _ItemViewState extends State<ItemView> {
void refresh() {
setState(() {});
}
2024-09-07 21:56:52 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(children: [
Row(
children: [
const Align(
alignment: Alignment.centerLeft,
child: Placeholder(),
), // todo
Column(
children: [
Text(
2024-09-07 22:24:13 +00:00
widget.item.name,
2024-09-07 21:56:52 +00:00
style: const TextStyle(fontWeight: FontWeight.bold),
),
2024-09-07 22:24:13 +00:00
Text(widget.item.category)
2024-09-07 21:56:52 +00:00
],
)
],
),
const SizedBox(height: 10),
Row(
2024-09-07 22:24:13 +00:00
children: widget.item.variants.entries.map((entry) {
2024-09-07 21:56:52 +00:00
return Text(entry.value.name);
}).toList()),
FutureBuilder(
2024-09-07 22:24:13 +00:00
future: API().getInventory(widget.item.id),
2024-09-07 21:56:52 +00:00
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const CircularProgressIndicator();
}
var data = snapshot.data!;
return Expanded(
child: ListView(
2024-09-07 22:24:13 +00:00
children:
data.map((x) => TransactionCard(x, refresh)).toList()));
2024-09-07 21:56:52 +00:00
},
)
]),
floatingActionButton: FloatingActionButton(
onPressed: () {
2024-09-07 22:24:13 +00:00
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SupplyPage(widget.item, refresh)));
2024-09-07 21:56:52 +00:00
},
child: const Icon(Icons.add)),
);
}
}
class TransactionCard extends StatelessWidget {
final Transaction t;
2024-09-07 22:24:13 +00:00
Function refresh;
2024-09-07 21:56:52 +00:00
2024-09-07 22:24:13 +00:00
TransactionCard(this.t, this.refresh, {super.key});
2024-09-07 21:56:52 +00:00
@override
Widget build(BuildContext context) {
2024-09-07 22:24:13 +00:00
return InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return ConsumePage(t.uuid, t.item, t.variant, this.refresh);
},
));
},
child: Card(
color: t.expired ? Colors.red[100] : Colors.white,
margin: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
elevation: 4,
child: Padding(
padding: EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2024-09-07 21:56:52 +00:00
Row(
2024-09-07 22:24:13 +00:00
mainAxisAlignment: MainAxisAlignment.spaceBetween,
2024-09-07 21:56:52 +00:00
children: [
2024-09-07 22:24:13 +00:00
Row(
children: [
Text(
t.item,
style: TextStyle(fontSize: 16),
),
SizedBox(
width: 4,
),
Text(
t.variant,
style: TextStyle(fontSize: 14, color: Colors.grey[600]),
),
],
),
2024-09-07 21:56:52 +00:00
Text(
2024-09-07 22:24:13 +00:00
tsFormat(t.timestamp),
2024-09-07 21:56:52 +00:00
style: TextStyle(fontSize: 14, color: Colors.grey[700]),
),
],
),
2024-09-07 22:24:13 +00:00
SizedBox(
height: 10,
),
Row(
children: [
Icon(Icons.money, size: 18, color: Colors.green),
SizedBox(width: 6),
Text(
"${t.price.value} ${t.price.currency}",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
],
),
if (t.origin != null) ...[
SizedBox(height: 8),
Row(
children: [
Icon(Icons.store, size: 18, color: Colors.blue),
SizedBox(width: 6),
Text(
t.origin!,
style: TextStyle(fontSize: 14, color: Colors.grey[700]),
),
],
),
],
2024-09-07 21:56:52 +00:00
],
2024-09-07 22:24:13 +00:00
),
2024-09-07 21:56:52 +00:00
),
),
);
}
}
String tsFormat(int ts) {
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(ts * 1000);
return DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
}