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]) ); } ),