parser logic added
This commit is contained in:
+92
-13
@@ -5,11 +5,14 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'models/line_item.dart';
|
||||
import 'models/shop.dart';
|
||||
import 'product_detail_screen.dart';
|
||||
import 'services/product_repository.dart';
|
||||
import 'utils/dates.dart';
|
||||
import 'utils/money.dart';
|
||||
import 'widgets/confirm_dialog.dart';
|
||||
import 'widgets/item_edit_sheet.dart';
|
||||
|
||||
class ShopDetailScreen extends StatefulWidget {
|
||||
const ShopDetailScreen({
|
||||
@@ -27,6 +30,7 @@ class ShopDetailScreen extends StatefulWidget {
|
||||
|
||||
class _ShopDetailScreenState extends State<ShopDetailScreen> {
|
||||
late Future<({Shop shop, List<ShopItem> items})> _future;
|
||||
bool _editing = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -43,6 +47,10 @@ class _ShopDetailScreenState extends State<ShopDetailScreen> {
|
||||
return (shop: shop, items: items);
|
||||
}
|
||||
|
||||
void _reload() {
|
||||
setState(() => _future = _load());
|
||||
}
|
||||
|
||||
Future<void> _openReceipt(File file) async {
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
@@ -68,12 +76,66 @@ class _ShopDetailScreenState extends State<ShopDetailScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _editItem(ShopItem item) async {
|
||||
final result = await showItemEditSheet(
|
||||
context,
|
||||
item: LineItem(
|
||||
id: '${item.id}',
|
||||
name: item.name,
|
||||
price: item.price,
|
||||
quantity: item.quantity,
|
||||
unitPrice: item.unitPrice,
|
||||
),
|
||||
);
|
||||
if (result == null || !mounted) return;
|
||||
await widget.repository.updateShopItem(
|
||||
id: item.id,
|
||||
name: result.name,
|
||||
price: result.price,
|
||||
quantity: result.quantity,
|
||||
unitPrice: result.unitPrice,
|
||||
);
|
||||
if (mounted) _reload();
|
||||
}
|
||||
|
||||
Future<void> _removeItem(ShopItem item) async {
|
||||
final confirmed = await confirmAction(
|
||||
context,
|
||||
title: 'Remove item?',
|
||||
message:
|
||||
'"${item.name}" will be removed from this trip. The product stays in your catalog.',
|
||||
confirmLabel: 'Remove',
|
||||
);
|
||||
if (!confirmed || !mounted) return;
|
||||
await widget.repository.deleteShopItem(item.id);
|
||||
if (mounted) _reload();
|
||||
}
|
||||
|
||||
Widget? _itemSubtitle(ShopItem item) {
|
||||
final parts = [
|
||||
if (item.quantity != null)
|
||||
'${item.quantity} × ${formatEuro(item.unitPrice ?? 0)}',
|
||||
if (_editing) formatEuro(item.price),
|
||||
];
|
||||
if (parts.isEmpty) return null;
|
||||
return Text(parts.join(' · '));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Shopping trip')),
|
||||
appBar: AppBar(
|
||||
title: const Text('Shopping trip'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: _editing ? 'Done' : 'Edit',
|
||||
onPressed: () => setState(() => _editing = !_editing),
|
||||
icon: Icon(_editing ? Icons.check : Icons.edit_outlined),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: FutureBuilder(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
@@ -92,10 +154,8 @@ class _ShopDetailScreenState extends State<ShopDetailScreen> {
|
||||
final shop = snapshot.data!.shop;
|
||||
final items = snapshot.data!.items;
|
||||
final receipt = shop.receiptImagePath;
|
||||
final receiptFile =
|
||||
receipt == null ? null : File(receipt);
|
||||
final hasReceipt =
|
||||
receiptFile != null && receiptFile.existsSync();
|
||||
final receiptFile = receipt == null ? null : File(receipt);
|
||||
final hasReceipt = receiptFile != null && receiptFile.existsSync();
|
||||
|
||||
return ListView(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 32),
|
||||
@@ -125,20 +185,39 @@ class _ShopDetailScreenState extends State<ShopDetailScreen> {
|
||||
const SizedBox(height: 24),
|
||||
Text('Products', style: theme.textTheme.titleMedium),
|
||||
const SizedBox(height: 8),
|
||||
if (items.isEmpty)
|
||||
Text(
|
||||
'No items on this trip.',
|
||||
style: theme.textTheme.bodyLarge,
|
||||
),
|
||||
for (final item in items)
|
||||
ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(item.name),
|
||||
subtitle: item.quantity == null
|
||||
? null
|
||||
subtitle: _itemSubtitle(item),
|
||||
trailing: _editing
|
||||
? Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
tooltip: 'Edit',
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: () => _editItem(item),
|
||||
icon: const Icon(Icons.edit_outlined),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Remove',
|
||||
visualDensity: VisualDensity.compact,
|
||||
onPressed: () => _removeItem(item),
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
),
|
||||
],
|
||||
)
|
||||
: Text(
|
||||
'${item.quantity} × ${formatEuro(item.unitPrice ?? 0)}',
|
||||
formatEuro(item.price),
|
||||
style: theme.textTheme.titleMedium,
|
||||
),
|
||||
trailing: Text(
|
||||
formatEuro(item.price),
|
||||
style: theme.textTheme.titleMedium,
|
||||
),
|
||||
onTap: () => _openProduct(item.productId),
|
||||
onTap: _editing ? null : () => _openProduct(item.productId),
|
||||
),
|
||||
const Divider(),
|
||||
ListTile(
|
||||
|
||||
Reference in New Issue
Block a user