From fd4e07fc8c445a0cd62e3504ce1e6e2661b031f4 Mon Sep 17 00:00:00 2001 From: 2211260 <2211260@hs-mannheim.de> Date: Fri, 2 Jun 2023 20:02:32 +0200 Subject: [PATCH] feat: adjust list logic on today page --- .../components/card/card_component.dart | 4 +- .../card/card_data_food_component.dart | 14 +- .../card/card_food_item_component.dart | 10 +- .../components/founded_search_component.dart | 130 ++++++++++++++---- lib/android/config/format_helper.dart | 1 + 5 files changed, 123 insertions(+), 36 deletions(-) diff --git a/lib/android/components/card/card_component.dart b/lib/android/components/card/card_component.dart index 0f0b634..a653b97 100644 --- a/lib/android/components/card/card_component.dart +++ b/lib/android/components/card/card_component.dart @@ -9,7 +9,7 @@ import '../../pages/nav_pages/search_food.dart'; class CardComponent extends StatelessWidget { final String title; final Color color; - final List foods; + final List foods; const CardComponent(this.title, this.foods, {super.key, Color? color}) : color = color ?? Colors.black; @@ -58,7 +58,7 @@ class CardComponent extends StatelessWidget { "${countCalories(castDynamicToListFood(foods))} Kalorien", ), CardDataFoodComponent( - castDynamicToListFood(foods), + foods, color, ), Padding( diff --git a/lib/android/components/card/card_data_food_component.dart b/lib/android/components/card/card_data_food_component.dart index 90735ee..12cc653 100644 --- a/lib/android/components/card/card_data_food_component.dart +++ b/lib/android/components/card/card_data_food_component.dart @@ -2,6 +2,8 @@ import 'package:ernaehrung/android/components/card/card_food_item_component.dart import 'package:ernaehrung/android/models/food.dart'; import 'package:flutter/material.dart'; +import '../../config/format_helper.dart'; + class CardDataFoodComponent extends StatelessWidget { final List foods; final Color color; @@ -19,11 +21,19 @@ class CardDataFoodComponent extends StatelessWidget { ListView.builder( physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, - itemCount: foods.length, + itemCount: getMapOfDistinctElementsWithCounterAndCalories( + foods) + .keys + .length, itemBuilder: (context, i) { + Map> map = + getMapOfDistinctElementsWithCounterAndCalories( + foods); + String foodName = map.keys.elementAt(i); + List values = map.values.elementAt(i); return Column( children: [ - CardFoodItemComponent(foods[i]), + CardFoodItemComponent(foodName,values), Divider( color: Colors.grey.shade300, thickness: 1.2, diff --git a/lib/android/components/card/card_food_item_component.dart b/lib/android/components/card/card_food_item_component.dart index fbad79c..5041a54 100644 --- a/lib/android/components/card/card_food_item_component.dart +++ b/lib/android/components/card/card_food_item_component.dart @@ -1,10 +1,10 @@ -import 'package:ernaehrung/android/models/food.dart'; import 'package:flutter/material.dart'; class CardFoodItemComponent extends StatelessWidget { - final Food food; + final String foodName; + final List countAndCalories; // [count, calories] - const CardFoodItemComponent(this.food, {Key? key}) : super(key: key); + const CardFoodItemComponent(this.foodName,this.countAndCalories, {Key? key}) : super(key: key); @override Widget build(BuildContext context) { @@ -16,7 +16,7 @@ class CardFoodItemComponent extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( - food.name.toString(), + "$foodName - ${countAndCalories[0] * 100}g", style: const TextStyle( color: Colors.white, fontSize: 17, @@ -26,7 +26,7 @@ class CardFoodItemComponent extends StatelessWidget { maxLines: 2, ), Text( - "\n${food.calories.toString()} kcal | 100g", + "\n${countAndCalories[1]} kcal | 100g", style: const TextStyle(color: Colors.white), ), ], diff --git a/lib/android/components/founded_search_component.dart b/lib/android/components/founded_search_component.dart index ddcd786..07528ee 100644 --- a/lib/android/components/founded_search_component.dart +++ b/lib/android/components/founded_search_component.dart @@ -19,15 +19,17 @@ class SearchedFoodComponent extends StatefulWidget { } class _SearchFoodComponentState extends State { - - void storeFood() async { - showSuccessToast(); - StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName); + int counter = 1; + void storeFood(int counter) async { final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!); if (!mealplanBox.isOpen){ Hive.openBox(dotenv.env['MEALPLAN_BOX']!); } - addValuesToList(mealplanBox, widget.cardName, [widget.food]); + for(int i = 0; i < counter; i++){ + StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName); + addValuesToList(mealplanBox, widget.cardName, [widget.food]); + } + showSuccessToast(); } void showSuccessToast(){ @@ -53,12 +55,13 @@ class _SearchFoodComponentState extends State { @override Widget build(BuildContext context) { + return Container( padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), - margin: const EdgeInsets.only(top:8), + margin: const EdgeInsets.only(top: 8), decoration: BoxDecoration( color: Colors.lightGreen, - borderRadius: BorderRadius.circular(5), // Rounded corners with 5px radius + borderRadius: BorderRadius.circular(5), ), child: WrapSuper( spacing: 32, @@ -69,7 +72,7 @@ class _SearchFoodComponentState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( - width: MediaQuery.of(context).size.width * 0.8, + width: MediaQuery.of(context).size.width * 0.6, child: Text( widget.food.name, maxLines: 2, @@ -83,7 +86,7 @@ class _SearchFoodComponentState extends State { ), ), SizedBox( - width: MediaQuery.of(context).size.width * 0.8, + width: MediaQuery.of(context).size.width * 0.6, child: Text( "${widget.food.calories} kcal | 100g", maxLines: 1, @@ -98,29 +101,102 @@ class _SearchFoodComponentState extends State { ), ], ), - ElevatedButton( - onPressed: () async { - storeFood(); - }, - style: ElevatedButton.styleFrom( - foregroundColor: Colors.white, - backgroundColor: Colors.green, - shadowColor: Colors.greenAccent, - elevation: 3, - shape: RoundedRectangleBorder( - borderRadius: BorderRadius.circular(32.0), - ), - ), - child: const Text( - '+', - style: TextStyle( - fontSize: 25 - ), + Padding( + padding: const EdgeInsets.only(top:12), + child: Row( + children: [ + Padding( + padding: const EdgeInsets.only(right :12), + child: Container( + width: 40, + height: 40, + decoration: BoxDecoration( + color: Colors.green, + borderRadius: BorderRadius.circular(5), + border: Border.all(color: Colors.white), + ), + child: InkWell( + onTap: () { + setState(() { + counter = counter > 1 ? counter - 1 : 1; + }); + }, + child: const Center( + child: Text( + "-", + style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold), + ), + ), + ), + ), + ), + + SizedBox( + width: 20, + child: Text( + counter.toString(), + style: const TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold), + textAlign: TextAlign.center, + ), + ), + Padding( + padding: const EdgeInsets.only(left:12), + child: Container( + width: 40, + height: 38, + decoration: BoxDecoration( + color: Colors.green, + borderRadius: BorderRadius.circular(5), + border: Border.all(color: Colors.white), + ), + child: InkWell( + onTap: () { + setState(() { + counter++; + }); + }, + child: const Center( + child: Text( + "+", + style: TextStyle(fontSize: 24, color: Colors.white, fontWeight: FontWeight.bold), + ), + ), + ), + ), + ) + + ], ), ), + Padding( + padding: const EdgeInsets.only(top:12), + child: ElevatedButton( + onPressed: () async { + storeFood(counter); + }, + style: ElevatedButton.styleFrom( + foregroundColor: Colors.white, + backgroundColor: Colors.green, + shadowColor: Colors.greenAccent, + elevation: 3, + shape: RoundedRectangleBorder( + borderRadius: BorderRadius.circular(5.0), + ), + ), + child: const Text( + '+', + style: TextStyle(fontSize: 25), + ), + ), + ) + ], ), ); + + + + } } diff --git a/lib/android/config/format_helper.dart b/lib/android/config/format_helper.dart index 752c265..89dc94e 100644 --- a/lib/android/config/format_helper.dart +++ b/lib/android/config/format_helper.dart @@ -30,6 +30,7 @@ Map> getMapOfDistinctElementsWithCounterAndCalories(List return resultMap; } + List getListOfDistinctElements(List foods){ List result = []; for(int i = 0; i < foods.length;i++){