...
This commit is contained in:
@@ -17,6 +17,7 @@ class ListDetailPage extends StatefulWidget {
|
||||
class _ListDetailPageState extends State<ListDetailPage> {
|
||||
late Future<List<Item>> _itemsFuture;
|
||||
StreamSubscription? _itemsSubscription;
|
||||
List<Item> _items = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -34,7 +35,9 @@ class _ListDetailPageState extends State<ListDetailPage> {
|
||||
}
|
||||
|
||||
void _loadItems() {
|
||||
setState(() => _itemsFuture = apiService.getItems(widget.list.id));
|
||||
setState(() {
|
||||
_itemsFuture = apiService.getItems(widget.list.id);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _showMembersDialog() async {
|
||||
@@ -72,10 +75,16 @@ class _ListDetailPageState extends State<ListDetailPage> {
|
||||
);
|
||||
if (ok != true || ctl.text.trim().isEmpty) return;
|
||||
|
||||
final String newName = ctl.text.trim();
|
||||
if (newName == item.name) return;
|
||||
|
||||
final String oldName = item.name;
|
||||
setState(() => item.name = newName);
|
||||
|
||||
try {
|
||||
final String newName = ctl.text.trim();
|
||||
await apiService.updateItem(item.id, name: newName);
|
||||
} catch (e) {
|
||||
setState(() => item.name = oldName);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
@@ -84,7 +93,7 @@ class _ListDetailPageState extends State<ListDetailPage> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _addItem(List<Item> currentItems) async {
|
||||
Future<void> _addItem() async {
|
||||
final TextEditingController ctl = TextEditingController();
|
||||
final bool? ok = await showDialog<bool>(
|
||||
context: context,
|
||||
@@ -110,7 +119,7 @@ class _ListDetailPageState extends State<ListDetailPage> {
|
||||
if (ok != true || ctl.text.trim().isEmpty) return;
|
||||
|
||||
try {
|
||||
final int maxPos = currentItems.fold<int>(
|
||||
final int maxPos = _items.fold<int>(
|
||||
-1,
|
||||
(max, item) => item.position > max ? item.position : max,
|
||||
);
|
||||
@@ -125,28 +134,33 @@ class _ListDetailPageState extends State<ListDetailPage> {
|
||||
}
|
||||
|
||||
Future<void> _deleteItem(Item item) async {
|
||||
final int index = _items.indexOf(item);
|
||||
setState(() => _items.remove(item));
|
||||
|
||||
try {
|
||||
await apiService.deleteItem(item.id);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('"${item.name}" gelöscht.')),
|
||||
);
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('"${item.name}" gelöscht.')));
|
||||
}
|
||||
} catch (e) {
|
||||
setState(() => _items.insert(index, item));
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Fehler beim Löschen: $e')),
|
||||
);
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('Fehler beim Löschen: $e')));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _toggleItem(Item item) async {
|
||||
final bool oldState = item.checked;
|
||||
setState(() => item.checked = !item.checked);
|
||||
try {
|
||||
await apiService.updateItem(item.id, checked: item.checked);
|
||||
} catch (e) {
|
||||
setState(() => item.checked = !item.checked);
|
||||
setState(() => item.checked = oldState);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Fehler beim Aktualisieren: $e')),
|
||||
@@ -178,17 +192,17 @@ class _ListDetailPageState extends State<ListDetailPage> {
|
||||
return Center(child: Text('Fehler: ${snapshot.error}'));
|
||||
}
|
||||
|
||||
final List<Item> items = snapshot.data ?? [];
|
||||
_items = snapshot.data ?? [];
|
||||
|
||||
if (items.isEmpty) {
|
||||
if (_items.isEmpty) {
|
||||
return const Center(
|
||||
child: Text('Füge deinen ersten Eintrag hinzu!'),
|
||||
);
|
||||
}
|
||||
return ListView.builder(
|
||||
itemCount: items.length,
|
||||
itemCount: _items.length,
|
||||
itemBuilder: (context, index) {
|
||||
final Item item = items[index];
|
||||
final Item item = _items[index];
|
||||
return ListTile(
|
||||
leading: Checkbox(
|
||||
value: item.checked,
|
||||
@@ -197,8 +211,9 @@ class _ListDetailPageState extends State<ListDetailPage> {
|
||||
title: Text(
|
||||
item.name,
|
||||
style: TextStyle(
|
||||
decoration:
|
||||
item.checked ? TextDecoration.lineThrough : null,
|
||||
decoration: item.checked
|
||||
? TextDecoration.lineThrough
|
||||
: null,
|
||||
),
|
||||
),
|
||||
trailing: Row(
|
||||
@@ -220,10 +235,7 @@ class _ListDetailPageState extends State<ListDetailPage> {
|
||||
},
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () async {
|
||||
final currentItems = await _itemsFuture;
|
||||
_addItem(currentItems);
|
||||
},
|
||||
onPressed: _addItem,
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user