fixes before build
This commit is contained in:
@@ -3,10 +3,11 @@ library;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'models/product.dart';
|
||||
import 'models/shopping_list.dart';
|
||||
import 'product_detail_screen.dart';
|
||||
import 'services/product_repository.dart';
|
||||
import 'utils/dates.dart';
|
||||
import 'widgets/add_list_items_sheet.dart';
|
||||
import 'widgets/confirm_dialog.dart';
|
||||
import 'widgets/name_dialog.dart';
|
||||
|
||||
@@ -26,34 +27,41 @@ class ShoppingListDetailScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ShoppingListDetailScreenState extends State<ShoppingListDetailScreen> {
|
||||
late Future<({ShoppingList list, List<ShoppingListItem> items})> _future;
|
||||
ShoppingList? _list;
|
||||
List<ShoppingListItem>? _items;
|
||||
Object? _error;
|
||||
bool _editing = false;
|
||||
int _loadToken = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_future = _load();
|
||||
widget.repository.addListener(_reload);
|
||||
widget.repository.addListener(_load);
|
||||
_load();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
widget.repository.removeListener(_reload);
|
||||
widget.repository.removeListener(_load);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<({ShoppingList list, List<ShoppingListItem> items})> _load() async {
|
||||
final list = await widget.repository.getShoppingList(widget.listId);
|
||||
if (list == null) {
|
||||
throw StateError('List not found');
|
||||
Future<void> _load() async {
|
||||
final token = ++_loadToken;
|
||||
try {
|
||||
final list = await widget.repository.getShoppingList(widget.listId);
|
||||
if (list == null) throw StateError('List not found');
|
||||
final items = await widget.repository.getShoppingListItems(widget.listId);
|
||||
if (!mounted || token != _loadToken) return;
|
||||
setState(() {
|
||||
_list = list;
|
||||
_items = items;
|
||||
_error = null;
|
||||
});
|
||||
} catch (error) {
|
||||
if (!mounted || token != _loadToken) return;
|
||||
setState(() => _error = error);
|
||||
}
|
||||
final items = await widget.repository.getShoppingListItems(widget.listId);
|
||||
return (list: list, items: items);
|
||||
}
|
||||
|
||||
void _reload() {
|
||||
if (!mounted) return;
|
||||
setState(() => _future = _load());
|
||||
}
|
||||
|
||||
Future<void> _rename(ShoppingList list) async {
|
||||
@@ -65,25 +73,70 @@ class _ShoppingListDetailScreenState extends State<ShoppingListDetailScreen> {
|
||||
);
|
||||
if (name == null || !mounted) return;
|
||||
await widget.repository.renameShoppingList(widget.listId, name);
|
||||
if (mounted) _reload();
|
||||
if (mounted) {
|
||||
setState(
|
||||
() => _list = ShoppingList(
|
||||
id: list.id,
|
||||
name: name,
|
||||
itemCount: list.itemCount,
|
||||
checkedCount: list.checkedCount,
|
||||
createdAt: list.createdAt,
|
||||
updatedAt: DateTime.now(),
|
||||
),
|
||||
);
|
||||
_load();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _openAddSheet() async {
|
||||
final products = await widget.repository.getAll();
|
||||
if (!mounted) return;
|
||||
final result = await showModalBottomSheet<({String name, int? productId})>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
showDragHandle: true,
|
||||
builder: (context) => _AddListItemSheet(products: products),
|
||||
);
|
||||
if (result == null || !mounted) return;
|
||||
await widget.repository.addShoppingListItem(
|
||||
listId: widget.listId,
|
||||
name: result.name,
|
||||
productId: result.productId,
|
||||
);
|
||||
if (mounted) _reload();
|
||||
final selections = await showAddListItemsSheet(context, products: products);
|
||||
if (selections == null || selections.isEmpty || !mounted) return;
|
||||
|
||||
final pending = [
|
||||
for (var i = 0; i < selections.length; i++)
|
||||
ShoppingListItem(
|
||||
id: -(i + 1),
|
||||
listId: widget.listId,
|
||||
name: selections[i].name,
|
||||
checked: false,
|
||||
sortOrder: (_items?.length ?? 0) + i,
|
||||
productId: selections[i].productId,
|
||||
),
|
||||
];
|
||||
setState(() {
|
||||
_items = [...?_items, ...pending];
|
||||
if (_list != null) {
|
||||
_list = ShoppingList(
|
||||
id: _list!.id,
|
||||
name: _list!.name,
|
||||
itemCount: _list!.itemCount + pending.length,
|
||||
checkedCount: _list!.checkedCount,
|
||||
createdAt: _list!.createdAt,
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
final added = await widget.repository.addShoppingListItems(
|
||||
listId: widget.listId,
|
||||
items: [
|
||||
for (final selection in selections)
|
||||
(name: selection.name, productId: selection.productId),
|
||||
],
|
||||
);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
final withoutPending = _items
|
||||
?.where((item) => item.id >= 0)
|
||||
.toList(growable: false);
|
||||
_items = [...?withoutPending, ...added];
|
||||
});
|
||||
} catch (_) {
|
||||
if (mounted) _load();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _editItem(ShoppingListItem item) async {
|
||||
@@ -95,7 +148,7 @@ class _ShoppingListDetailScreenState extends State<ShoppingListDetailScreen> {
|
||||
);
|
||||
if (name == null || !mounted) return;
|
||||
await widget.repository.updateShoppingListItem(id: item.id, name: name);
|
||||
if (mounted) _reload();
|
||||
if (mounted) _load();
|
||||
}
|
||||
|
||||
Future<void> _removeItem(ShoppingListItem item) async {
|
||||
@@ -106,16 +159,100 @@ class _ShoppingListDetailScreenState extends State<ShoppingListDetailScreen> {
|
||||
confirmLabel: 'Remove',
|
||||
);
|
||||
if (!confirmed || !mounted) return;
|
||||
await widget.repository.deleteShoppingListItem(item.id);
|
||||
if (mounted) _reload();
|
||||
setState(() {
|
||||
_items = [
|
||||
for (final entry in _items ?? const <ShoppingListItem>[])
|
||||
if (entry.id != item.id) entry,
|
||||
];
|
||||
if (_list != null) {
|
||||
_list = ShoppingList(
|
||||
id: _list!.id,
|
||||
name: _list!.name,
|
||||
itemCount: _list!.itemCount - 1,
|
||||
checkedCount: item.checked
|
||||
? _list!.checkedCount - 1
|
||||
: _list!.checkedCount,
|
||||
createdAt: _list!.createdAt,
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
}
|
||||
});
|
||||
try {
|
||||
await widget.repository.deleteShoppingListItem(item.id);
|
||||
} catch (_) {
|
||||
if (mounted) _load();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _toggle(ShoppingListItem item) async {
|
||||
await widget.repository.updateShoppingListItem(
|
||||
id: item.id,
|
||||
checked: !item.checked,
|
||||
);
|
||||
if (mounted) _reload();
|
||||
final next = !item.checked;
|
||||
setState(() {
|
||||
_items = [
|
||||
for (final entry in _items ?? const <ShoppingListItem>[])
|
||||
if (entry.id == item.id)
|
||||
ShoppingListItem(
|
||||
id: entry.id,
|
||||
listId: entry.listId,
|
||||
name: entry.name,
|
||||
checked: next,
|
||||
sortOrder: entry.sortOrder,
|
||||
productId: entry.productId,
|
||||
)
|
||||
else
|
||||
entry,
|
||||
];
|
||||
if (_list != null) {
|
||||
_list = ShoppingList(
|
||||
id: _list!.id,
|
||||
name: _list!.name,
|
||||
itemCount: _list!.itemCount,
|
||||
checkedCount: _list!.checkedCount + (next ? 1 : -1),
|
||||
createdAt: _list!.createdAt,
|
||||
updatedAt: _list!.updatedAt,
|
||||
);
|
||||
}
|
||||
});
|
||||
try {
|
||||
await widget.repository.updateShoppingListItem(
|
||||
id: item.id,
|
||||
checked: next,
|
||||
);
|
||||
} catch (_) {
|
||||
if (mounted) _load();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _shopAgain() async {
|
||||
final list = _list;
|
||||
if (list == null || list.checkedCount == 0) return;
|
||||
|
||||
setState(() {
|
||||
_items = [
|
||||
for (final item in _items ?? const <ShoppingListItem>[])
|
||||
ShoppingListItem(
|
||||
id: item.id,
|
||||
listId: item.listId,
|
||||
name: item.name,
|
||||
checked: false,
|
||||
sortOrder: item.sortOrder,
|
||||
productId: item.productId,
|
||||
),
|
||||
];
|
||||
_list = ShoppingList(
|
||||
id: list.id,
|
||||
name: list.name,
|
||||
itemCount: list.itemCount,
|
||||
checkedCount: 0,
|
||||
createdAt: list.createdAt,
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
});
|
||||
|
||||
try {
|
||||
await widget.repository.resetShoppingListChecks(widget.listId);
|
||||
} catch (_) {
|
||||
if (mounted) _load();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _openProduct(int productId) async {
|
||||
@@ -133,192 +270,112 @@ class _ShoppingListDetailScreenState extends State<ShoppingListDetailScreen> {
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Scaffold(
|
||||
body: FutureBuilder(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: Text(
|
||||
'Could not load this list.',
|
||||
style: theme.textTheme.bodyLarge,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (!snapshot.hasData) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: const Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
final list = snapshot.data!.list;
|
||||
final items = snapshot.data!.items;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(list.name),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: 'Rename',
|
||||
onPressed: () => _rename(list),
|
||||
icon: const Icon(Icons.drive_file_rename_outline),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: _editing ? 'Done' : 'Edit',
|
||||
onPressed: () => setState(() => _editing = !_editing),
|
||||
icon: Icon(_editing ? Icons.check : Icons.edit_outlined),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _openAddSheet,
|
||||
tooltip: 'Add item',
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
body: items.isEmpty
|
||||
? Center(
|
||||
child: Text(
|
||||
'Add things to buy.',
|
||||
style: theme.textTheme.bodyLarge,
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.fromLTRB(8, 8, 8, 88),
|
||||
itemCount: items.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = items[index];
|
||||
return ListTile(
|
||||
leading: Checkbox(
|
||||
value: item.checked,
|
||||
onChanged: _editing ? null : (_) => _toggle(item),
|
||||
),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: item.checked
|
||||
? theme.textTheme.bodyLarge?.copyWith(
|
||||
decoration: TextDecoration.lineThrough,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
trailing: _editing
|
||||
? Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
tooltip: 'Edit',
|
||||
onPressed: () => _editItem(item),
|
||||
icon: const Icon(Icons.edit_outlined),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Remove',
|
||||
onPressed: () => _removeItem(item),
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
),
|
||||
],
|
||||
)
|
||||
: item.productId == null
|
||||
? null
|
||||
: IconButton(
|
||||
tooltip: 'Product',
|
||||
onPressed: () => _openProduct(item.productId!),
|
||||
icon: const Icon(Icons.chevron_right),
|
||||
),
|
||||
onTap: _editing ? null : () => _toggle(item),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _AddListItemSheet extends StatefulWidget {
|
||||
const _AddListItemSheet({required this.products});
|
||||
|
||||
final List<Product> products;
|
||||
|
||||
@override
|
||||
State<_AddListItemSheet> createState() => _AddListItemSheetState();
|
||||
}
|
||||
|
||||
class _AddListItemSheetState extends State<_AddListItemSheet> {
|
||||
final TextEditingController _controller = TextEditingController();
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
List<Product> get _matches {
|
||||
final query = _controller.text.trim().toLowerCase();
|
||||
if (query.isEmpty) {
|
||||
return widget.products.take(8).toList();
|
||||
}
|
||||
return widget.products
|
||||
.where((product) => product.name.toLowerCase().contains(query))
|
||||
.take(8)
|
||||
.toList();
|
||||
}
|
||||
|
||||
void _add({String? name, int? productId}) {
|
||||
final value = (name ?? _controller.text).trim();
|
||||
if (value.isEmpty) {
|
||||
setState(() => _error = 'Enter an item name.');
|
||||
return;
|
||||
}
|
||||
Navigator.of(context).pop((name: value, productId: productId));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final bottom = MediaQuery.viewInsetsOf(context).bottom;
|
||||
final matches = _matches;
|
||||
|
||||
return Padding(
|
||||
padding: EdgeInsets.fromLTRB(16, 0, 16, 16 + bottom),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text('Add item', style: theme.textTheme.titleLarge),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _controller,
|
||||
autofocus: true,
|
||||
textCapitalization: TextCapitalization.sentences,
|
||||
decoration: const InputDecoration(labelText: 'Item name'),
|
||||
onChanged: (_) => setState(() => _error = null),
|
||||
onSubmitted: (_) => _add(),
|
||||
if (_error != null && _list == null) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: Center(
|
||||
child: Text(
|
||||
'Could not load this list.',
|
||||
style: theme.textTheme.bodyLarge,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (_list == null || _items == null) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: const Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
}
|
||||
|
||||
final list = _list!;
|
||||
final items = _items!;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text('${list.name} (${formatDate(list.createdAt)})'),
|
||||
actions: [
|
||||
if (list.checkedCount > 0 && !_editing)
|
||||
TextButton(
|
||||
onPressed: _shopAgain,
|
||||
child: const Text('Shop again'),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Rename',
|
||||
onPressed: () => _rename(list),
|
||||
icon: const Icon(Icons.drive_file_rename_outline),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: _editing ? 'Done' : 'Edit',
|
||||
onPressed: () => setState(() => _editing = !_editing),
|
||||
icon: Icon(_editing ? Icons.check : Icons.edit_outlined),
|
||||
),
|
||||
if (_error != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(_error!, style: TextStyle(color: theme.colorScheme.error)),
|
||||
],
|
||||
if (matches.isNotEmpty) ...[
|
||||
const SizedBox(height: 12),
|
||||
Text('From products', style: theme.textTheme.labelMedium),
|
||||
const SizedBox(height: 4),
|
||||
for (final product in matches)
|
||||
ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(product.name),
|
||||
onTap: () => _add(name: product.name, productId: product.id),
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 8),
|
||||
FilledButton(onPressed: () => _add(), child: const Text('Add')),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: _openAddSheet,
|
||||
tooltip: 'Add item',
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
body: items.isEmpty
|
||||
? Center(
|
||||
child: Text(
|
||||
'Add things to buy.',
|
||||
style: theme.textTheme.bodyLarge,
|
||||
),
|
||||
)
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.fromLTRB(8, 8, 8, 88),
|
||||
itemCount: items.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = items[index];
|
||||
return ListTile(
|
||||
leading: Checkbox(
|
||||
value: item.checked,
|
||||
onChanged: _editing || item.id < 0
|
||||
? null
|
||||
: (_) => _toggle(item),
|
||||
),
|
||||
title: Text(
|
||||
item.name,
|
||||
style: item.checked
|
||||
? theme.textTheme.bodyLarge?.copyWith(
|
||||
decoration: TextDecoration.lineThrough,
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
: null,
|
||||
),
|
||||
trailing: _editing
|
||||
? Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
tooltip: 'Edit',
|
||||
onPressed: item.id < 0
|
||||
? null
|
||||
: () => _editItem(item),
|
||||
icon: const Icon(Icons.edit_outlined),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Remove',
|
||||
onPressed: item.id < 0
|
||||
? null
|
||||
: () => _removeItem(item),
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
),
|
||||
],
|
||||
)
|
||||
: item.productId == null
|
||||
? null
|
||||
: IconButton(
|
||||
tooltip: 'Product',
|
||||
onPressed: () => _openProduct(item.productId!),
|
||||
icon: const Icon(Icons.chevron_right),
|
||||
),
|
||||
onTap: _editing || item.id < 0 ? null : () => _toggle(item),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user