cdb_ui/lib/pages/transaction.dart

194 lines
5.5 KiB
Dart
Raw Normal View History

2024-09-20 07:24:22 +00:00
import 'package:cdb_ui/api.dart';
2024-09-21 15:15:12 +00:00
import 'package:cdb_ui/pages/consume.dart';
2024-09-20 07:24:22 +00:00
import 'package:flutter/material.dart';
2024-09-21 15:15:12 +00:00
import 'package:intl/intl.dart';
2024-09-23 06:55:40 +00:00
import 'package:qr_bar_code/qr/qr.dart';
2024-09-20 07:24:22 +00:00
class TransactionPage extends StatelessWidget {
2024-09-23 06:55:40 +00:00
final Transaction transaction;
2024-09-23 07:03:40 +00:00
final Function? refresh;
2024-09-20 07:24:22 +00:00
2024-09-23 07:03:40 +00:00
const TransactionPage(this.transaction, {this.refresh, super.key});
2024-09-20 07:24:22 +00:00
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
2024-09-23 06:55:40 +00:00
Text("UUID: ${transaction.uuid}"),
QRCode(
data: transaction.uuid,
size: 22,
semanticsLabel: "Transaction UUID",
),
Text("${transaction.item} - ${transaction.variant}"),
Text("Added: ${tsFormat(transaction.timestamp)}"),
if (transaction.expired) const Text("Transaction is Expired!"),
IconText(Icons.money, transaction.price.format(),
color: Colors.green),
IconText(Icons.store, transaction.origin!, color: Colors.blue),
IconText(Icons.location_city, transaction.location!),
if (transaction.note != null) Text(transaction.note!),
if (transaction.consumed != null) ...[
const Divider(),
Text("Consumed on: ${tsFormat(transaction.consumed!.timestamp)}"),
IconText(Icons.store, transaction.consumed!.destination,
color: Colors.blue),
IconText(Icons.money, transaction.consumed!.price.format(),
color: Colors.green),
]
2024-09-20 07:24:22 +00:00
// todo : chart with price history
],
),
2024-09-23 07:03:40 +00:00
floatingActionButton: FloatingActionButton(
onPressed: () {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) {
return ConsumePage(transaction, refresh ?? () {});
},
));
},
child: const Icon(Icons.receipt_long)),
2024-09-20 07:24:22 +00:00
);
}
}
2024-09-21 15:15:12 +00:00
class TransactionCard extends StatelessWidget {
final Transaction t;
final Function refresh;
2024-09-23 14:34:39 +00:00
final Function(Transaction)? onTap;
final Function(Transaction)? onLongPress;
2024-09-21 15:15:12 +00:00
2024-09-23 14:34:39 +00:00
const TransactionCard(this.t, this.refresh,
{this.onTap, this.onLongPress, super.key});
2024-09-21 15:15:12 +00:00
@override
Widget build(BuildContext context) {
return InkWell(
onTap: () {
2024-09-23 14:34:39 +00:00
if (onTap != null) {
onTap!(t);
return;
}
2024-09-21 15:15:12 +00:00
Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
2024-09-23 07:03:40 +00:00
return ConsumePage(t, refresh);
2024-09-21 15:15:12 +00:00
},
));
},
2024-09-22 20:15:43 +00:00
onLongPress: () {
2024-09-23 14:34:39 +00:00
if (onLongPress != null) {
onLongPress!(t);
return;
}
2024-09-22 20:15:43 +00:00
Navigator.of(context).push(MaterialPageRoute(
builder: (context) {
return TransactionPage(t);
},
));
},
2024-09-21 15:15:12 +00:00
child: Card(
color: t.expired ? Colors.red[100] : Colors.black,
margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
elevation: 4,
child: Padding(
padding: const EdgeInsets.all(12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
Text(
t.item,
style: const TextStyle(fontSize: 16),
),
const SizedBox(
width: 4,
),
Text(
t.variant,
style: TextStyle(fontSize: 14, color: Colors.grey[400]),
),
],
),
Text(
tsFormat(t.timestamp),
style: TextStyle(fontSize: 14, color: Colors.grey[700]),
),
],
),
const SizedBox(
height: 10,
),
if ((t.note ?? "").isNotEmpty) ...[
const SizedBox(height: 8),
Text(t.note!),
const SizedBox(
height: 10,
)
],
IconText(Icons.money,
"${t.price.value.toStringAsFixed(2)} ${t.price.currency}",
color: Colors.green),
if (t.origin != null) ...[
const SizedBox(height: 8),
IconText(Icons.store, t.origin!, color: Colors.blue)
],
2024-09-23 06:42:58 +00:00
if (t.location != null) ...[
const SizedBox(
height: 8,
),
IconText(Icons.location_city, t.location!)
]
2024-09-21 15:15:12 +00:00
],
),
),
),
);
}
}
String tsFormat(int ts) {
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(ts * 1000);
return DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
}
class IconText extends StatelessWidget {
final IconData icon;
final String text;
final Color? color;
const IconText(this.icon, this.text, {super.key, this.color});
@override
Widget build(BuildContext context) {
return Row(
children: [
Icon(icon, size: 18, color: color),
const SizedBox(width: 6),
Text(
text,
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
],
);
}
}