home added

This commit is contained in:
gitea
2026-08-31 12:57:17 +02:00
parent 92581e6d55
commit 7692809219
18 changed files with 1446 additions and 384 deletions
+83 -61
View File
@@ -8,6 +8,7 @@ import 'services/product_repository.dart';
import 'shopping_list_detail_screen.dart';
import 'widgets/confirm_dialog.dart';
import 'widgets/name_dialog.dart';
import 'widgets/profile_button.dart';
import 'widgets/settings_button.dart';
class ShoppingListsScreen extends StatefulWidget {
@@ -20,17 +21,40 @@ class ShoppingListsScreen extends StatefulWidget {
}
class _ShoppingListsScreenState extends State<ShoppingListsScreen> {
late Future<List<ShoppingList>> _future;
List<ShoppingList>? _lists;
Object? _error;
bool _editing = false;
int _loadToken = 0;
bool get _hasLists => _lists != null && _lists!.isNotEmpty;
@override
void initState() {
super.initState();
_future = widget.repository.getShoppingLists();
widget.repository.addListener(_load);
_load();
}
void _reload() {
setState(() => _future = widget.repository.getShoppingLists());
@override
void dispose() {
widget.repository.removeListener(_load);
super.dispose();
}
Future<void> _load() async {
final token = ++_loadToken;
try {
final lists = await widget.repository.getShoppingLists();
if (!mounted || token != _loadToken) return;
setState(() {
_lists = lists;
_error = null;
if (lists.isEmpty) _editing = false;
});
} catch (error) {
if (!mounted || token != _loadToken) return;
setState(() => _error = error);
}
}
Future<void> _create() async {
@@ -53,7 +77,7 @@ class _ShoppingListsScreenState extends State<ShoppingListsScreen> {
ShoppingListDetailScreen(repository: widget.repository, listId: id),
),
);
if (mounted) _reload();
if (mounted) _load();
}
Future<void> _removeList(ShoppingList list) async {
@@ -63,8 +87,15 @@ class _ShoppingListsScreenState extends State<ShoppingListsScreen> {
message: '"${list.name}" and its items will be deleted.',
);
if (!confirmed || !mounted) return;
await widget.repository.deleteShoppingList(list.id);
if (mounted) _reload();
setState(() {
_lists = [for (final item in _lists ?? const <ShoppingList>[]) if (item.id != list.id) item];
if (_lists!.isEmpty) _editing = false;
});
try {
await widget.repository.deleteShoppingList(list.id);
} catch (_) {
if (mounted) _load();
}
}
@override
@@ -75,11 +106,13 @@ class _ShoppingListsScreenState extends State<ShoppingListsScreen> {
appBar: AppBar(
title: const Text('Shopping lists'),
actions: [
IconButton(
tooltip: _editing ? 'Done' : 'Edit',
onPressed: () => setState(() => _editing = !_editing),
icon: Icon(_editing ? Icons.check : Icons.edit_outlined),
),
if (_hasLists)
IconButton(
tooltip: _editing ? 'Done' : 'Edit',
onPressed: () => setState(() => _editing = !_editing),
icon: Icon(_editing ? Icons.check : Icons.edit_outlined),
),
const ProfileButton(),
const SettingsButton(),
],
),
@@ -88,64 +121,53 @@ class _ShoppingListsScreenState extends State<ShoppingListsScreen> {
tooltip: 'New list',
child: const Icon(Icons.add),
),
body: FutureBuilder<List<ShoppingList>>(
future: _future,
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(
body: _error != null && _lists == null
? Center(
child: Text(
'Could not load shopping lists.',
style: theme.textTheme.bodyLarge,
),
);
}
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
final lists = snapshot.data!;
if (lists.isEmpty) {
return Center(
)
: _lists == null
? const Center(child: CircularProgressIndicator())
: _lists!.isEmpty
? Center(
child: Text(
'Create a list for your next shop.',
textAlign: TextAlign.center,
style: theme.textTheme.bodyLarge,
),
);
}
return ListView.separated(
padding: const EdgeInsets.fromLTRB(8, 8, 8, 88),
itemCount: lists.length,
separatorBuilder: (_, _) => const SizedBox(height: 4),
itemBuilder: (context, index) {
final list = lists[index];
final remaining = list.itemCount - list.checkedCount;
return ListTile(
leading: CircleAvatar(child: Text('${list.itemCount}')),
title: Text(list.name),
subtitle: Text(
list.itemCount == 0
? 'Empty'
: remaining == 0
? 'All done'
: remaining == 1
? '1 item left'
: '$remaining items left',
),
trailing: _editing
? IconButton(
tooltip: 'Delete',
onPressed: () => _removeList(list),
icon: const Icon(Icons.delete_outline),
)
: const Icon(Icons.chevron_right),
onTap: _editing ? null : () => _openList(list.id),
);
},
);
},
),
)
: ListView.separated(
padding: const EdgeInsets.fromLTRB(8, 8, 8, 88),
itemCount: _lists!.length,
separatorBuilder: (_, _) => const SizedBox(height: 4),
itemBuilder: (context, index) {
final list = _lists![index];
final remaining = list.itemCount - list.checkedCount;
return ListTile(
leading: CircleAvatar(child: Text('${list.itemCount}')),
title: Text(list.name),
subtitle: Text(
list.itemCount == 0
? 'Empty'
: remaining == 0
? 'All done'
: remaining == 1
? '1 item left'
: '$remaining items left',
),
trailing: _editing
? IconButton(
tooltip: 'Delete',
onPressed: () => _removeList(list),
icon: const Icon(Icons.delete_outline),
)
: const Icon(Icons.chevron_right),
onTap: _editing ? null : () => _openList(list.id),
);
},
),
);
}
}