feat: adjust foodname list: shorten names, show count and calories

welcome
98spag 2023-05-30 15:15:22 +02:00
parent 6631c7766a
commit 6aadf7bbfa
1 changed files with 23 additions and 2 deletions

View File

@ -90,6 +90,24 @@ class _CardComponentState extends State<CardComponent> {
return calories.round();
}
Map<String,List<int>> getMapOfDistinctElementsWithCounterAndCalories(List<Food> foods){
Map<String,List<int>> resultMap = <String,List<int>>{};
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<CardComponent> {
child:ListView.builder(
primary: false,
shrinkWrap: true,
itemCount: widget.selectedMeal.length,
itemCount: getMapOfDistinctElementsWithCounterAndCalories(widget.selectedMeal).keys.length,
itemBuilder: (context, i) {
Map<String,List<int>> map = getMapOfDistinctElementsWithCounterAndCalories(widget.selectedMeal);
String foodName = map.keys.elementAt(i);
List<int> values = map.values.elementAt(i);
return Text(
widget.selectedMeal[i].name
getFoodListStringByFood(foodName, values[0], values[1])
);
}
),