From 6aadf7bbfa838d47e7b4ef6af8d9cb53f5b50d7a Mon Sep 17 00:00:00 2001 From: 98spag Date: Tue, 30 May 2023 15:15:22 +0200 Subject: [PATCH] feat: adjust foodname list: shorten names, show count and calories --- lib/android/components/card_component.dart | 25 ++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/android/components/card_component.dart b/lib/android/components/card_component.dart index ba35f58..edfe03b 100644 --- a/lib/android/components/card_component.dart +++ b/lib/android/components/card_component.dart @@ -90,6 +90,24 @@ class _CardComponentState extends State { return calories.round(); } + Map> getMapOfDistinctElementsWithCounterAndCalories(List foods){ + Map> resultMap = >{}; + for(int i = 0; i < foods.length;i++){ + if(!resultMap.keys.contains(foods[i].name)){ + resultMap.putIfAbsent(foods[i].name, () => [1,foods[i].calories]); + }else{ + resultMap[foods[i].name]![0] = resultMap[foods[i].name]![0] + 1; + } + } + return resultMap; + } + + String getFoodListStringByFood(String foodName, int count, int calories){ + int maxWidth = 35; + String limitedText = foodName.length > maxWidth ? "${foodName.substring(0, maxWidth - 3)} ... $count x $calories kcal" : "$foodName $count x $calories kcal"; + return limitedText; + } + @override Widget build(BuildContext context) { return Card( @@ -130,10 +148,13 @@ class _CardComponentState extends State { child:ListView.builder( primary: false, shrinkWrap: true, - itemCount: widget.selectedMeal.length, + itemCount: getMapOfDistinctElementsWithCounterAndCalories(widget.selectedMeal).keys.length, itemBuilder: (context, i) { + Map> map = getMapOfDistinctElementsWithCounterAndCalories(widget.selectedMeal); + String foodName = map.keys.elementAt(i); + List values = map.values.elementAt(i); return Text( - widget.selectedMeal[i].name + getFoodListStringByFood(foodName, values[0], values[1]) ); } ),