From 86dac312a5e11d4ffd1dca43bc220945d94190a1 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Wed, 5 Apr 2023 10:07:42 +0200 Subject: [PATCH] Moved from category list to category map This was done to make category selection easier NOTE: this also fixes the bug, where the wrong category was selected, or the category id returned out-of-bounds --- lib/screens/room/pages/list.dart | 56 +++++++++++++++----------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/lib/screens/room/pages/list.dart b/lib/screens/room/pages/list.dart index 17fc8a8..a0be616 100644 --- a/lib/screens/room/pages/list.dart +++ b/lib/screens/room/pages/list.dart @@ -26,7 +26,7 @@ class _ShoppingListPageState extends State { List list = []; List cart = []; Map weights = {}; - List categories = []; + Map categories = {}; List products = []; void fetchItems() { @@ -124,14 +124,16 @@ class _ShoppingListPageState extends State { .toList(); Map map = {}; + Map cat = {}; for (int i = 0; i < resp.length; i++) { map[resp[i].id] = i; + cat[resp[i].id] = resp[i]; } if (mounted) { setState(() { weights = map; - categories = resp; + categories = cat; sortAll(); }); } @@ -199,12 +201,8 @@ class _ShoppingListPageState extends State { itemCount: list.length, itemBuilder: (context, index) { final item = list[index]; - - RoomCategory cat = RoomCategory.other(context); - try { - cat = categories[item.category!]; - }catch(_){} - + final cat = + categories[item.category] ?? RoomCategory.other(context); return ShoppingListItem( name: item.name, description: item.description, @@ -239,7 +237,7 @@ class _ShoppingListPageState extends State { context: context, builder: (context) => ShoppingListItemInfo( products: products, - categories: categories, + category: cat, item: item, room: widget.room!.id, server: widget.room!.serverTag)); @@ -253,13 +251,13 @@ class _ShoppingListPageState extends State { physics: const ClampingScrollPhysics(), itemBuilder: (context, index) { final item = cart[index]; + final cat = + categories[item.category] ?? RoomCategory.other(context); return ShoppingListItem( name: item.name, description: item.description, - category: (item.category != null) - ? categories[item.category!] - : RoomCategory.other(context), + category: cat, key: Key(item.id.toString()), inCart: item.state != 0, onDismiss: () { @@ -278,7 +276,7 @@ class _ShoppingListPageState extends State { changeItemState(item); }, onTap: () { - // TODO: show modal bottom sheet + // show modal bottom sheet // containing // - item info // - option to view linked product (if available) @@ -289,7 +287,7 @@ class _ShoppingListPageState extends State { context: context, builder: (context) => ShoppingListItemInfo( products: products, - categories: categories, + category: cat, item: item, room: widget.room!.id, server: widget.room!.serverTag)); @@ -360,8 +358,9 @@ class ShoppingListItem extends StatelessWidget { }, background: Icon(!inCart ? Icons.shopping_cart : Icons.remove_shopping_cart), - child: ListTile( - enabled: !inCart, + child: Opacity( + opacity: inCart?0.5:1.0, + child: ListTile( title: Text(name), subtitle: Text(description), trailing: CategoryChip( @@ -372,23 +371,24 @@ class ShoppingListItem extends StatelessWidget { onTap!(); } }, - ), + )), ); } } class ShoppingListItemInfo extends StatelessWidget { - RoomItem item; - String server; - String room; - List categories = []; - List products = []; + final RoomItem item; + final String server; + final String room; + final RoomCategory category; + final List products; - ShoppingListItemInfo( - {required this.item, + const ShoppingListItemInfo( + {super.key, + required this.item, required this.server, required this.room, - required this.categories, + required this.category, required this.products}); @override @@ -404,11 +404,7 @@ class ShoppingListItemInfo extends StatelessWidget { child: Column(children: [ Text(item.name, style: textTheme.headlineLarge), Text(item.description, style: textTheme.titleMedium), - CategoryPicker( - label: AppLocalizations.of(context)!.selectCategoryLabel, - categories: categories, - selected: item.category, - enabled: false), + CategoryChip(category: category,), ProductPicker( label: AppLocalizations.of(context)!.selectLinkedProductLabel,