From 686657628aff128e2d98c6408d76169f83d80727 Mon Sep 17 00:00:00 2001 From: 98spag Date: Fri, 16 Jun 2023 12:03:04 +0200 Subject: [PATCH] feat: adjust text width for web --- lib/android/components/card_component.dart | 2 +- .../components/founded_search_component.dart | 10 ++++---- lib/android/config/format_helper.dart | 23 ++++++++++++++++--- .../pages/nav_pages/progress_page.dart | 2 +- test/statistics_test.dart | 6 ++--- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/android/components/card_component.dart b/lib/android/components/card_component.dart index 32f3efd..2f53973 100644 --- a/lib/android/components/card_component.dart +++ b/lib/android/components/card_component.dart @@ -124,7 +124,7 @@ class _CardComponentState extends State { return Padding( padding: const EdgeInsets.only(top: 10.0), // Adjust the padding as needed child: Text( - "${i+1}. ${getFoodListStringByFood(foodName, values[0], values[1])}", + "${i+1}. ${getFoodListStringByFood(foodName, values[0], values[1], context)}", ), ); }), diff --git a/lib/android/components/founded_search_component.dart b/lib/android/components/founded_search_component.dart index 07528ee..61fce70 100644 --- a/lib/android/components/founded_search_component.dart +++ b/lib/android/components/founded_search_component.dart @@ -20,7 +20,7 @@ class SearchedFoodComponent extends StatefulWidget { class _SearchFoodComponentState extends State { int counter = 1; - void storeFood(int counter) async { + void storeFood(int counter, BuildContext context) async { final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!); if (!mealplanBox.isOpen){ Hive.openBox(dotenv.env['MEALPLAN_BOX']!); @@ -29,12 +29,12 @@ class _SearchFoodComponentState extends State { StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName); addValuesToList(mealplanBox, widget.cardName, [widget.food]); } - showSuccessToast(); + showSuccessToast(context); } - void showSuccessToast(){ + void showSuccessToast(BuildContext context){ Fluttertoast.showToast( - msg: "${getToastFoodNameString(widget.food)} erfolgreich zu ${StringUtils.capitalize(widget.cardName)} hinzugefügt", + msg: "${getToastFoodNameString(widget.food,context)} erfolgreich zu ${StringUtils.capitalize(widget.cardName)} hinzugefügt", toastLength: Toast.LENGTH_SHORT, gravity: ToastGravity.BOTTOM, timeInSecForIosWeb: 5, @@ -172,7 +172,7 @@ class _SearchFoodComponentState extends State { padding: const EdgeInsets.only(top:12), child: ElevatedButton( onPressed: () async { - storeFood(counter); + storeFood(counter,context); }, style: ElevatedButton.styleFrom( foregroundColor: Colors.white, diff --git a/lib/android/config/format_helper.dart b/lib/android/config/format_helper.dart index 637a4b8..c7615de 100644 --- a/lib/android/config/format_helper.dart +++ b/lib/android/config/format_helper.dart @@ -1,19 +1,36 @@ +import 'package:flutter/cupertino.dart'; + import '../models/food.dart'; -String getFoodListStringByFood(String foodName, int count, int calories){ +String getFoodListStringByFood(String foodName, int count, int calories, BuildContext? context){ int maxWidth = 35; + if(context != null){ + maxWidth = isScreenWidthAbove1000(context) ? 100 : 35; + } String limitedText = foodName.length > maxWidth ? "${foodName.substring(0, maxWidth - 3)} ... $count x $calories kcal" : "$foodName $count x $calories kcal"; return limitedText; } -String getWeeklyRankingString(String foodName){ + +bool isScreenWidthAbove1000(BuildContext context) { + double screenWidth = MediaQuery.of(context).size.width; + return screenWidth > 400; +} + +String getWeeklyRankingString(String foodName,BuildContext? context){ int maxWidth = 40; + if(context != null){ + maxWidth = isScreenWidthAbove1000(context) ? 100 : 40; + } String limitedText = foodName.length > maxWidth ? "${foodName.substring(0, maxWidth - 3)} ..." : foodName; return limitedText; } -String getToastFoodNameString(Food food){ +String getToastFoodNameString(Food food,BuildContext? context){ int maxWidth = 25; + if(context != null){ + maxWidth = isScreenWidthAbove1000(context) ? 100 : 25; + } String limitedText = food.name.length > maxWidth ? "${food.name.substring(0, maxWidth - 3)} ..." : food.name; return limitedText; } diff --git a/lib/android/pages/nav_pages/progress_page.dart b/lib/android/pages/nav_pages/progress_page.dart index cef701d..d69e748 100644 --- a/lib/android/pages/nav_pages/progress_page.dart +++ b/lib/android/pages/nav_pages/progress_page.dart @@ -49,7 +49,7 @@ class ProgressPage extends StatelessWidget { mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ SecondaryTextComponent( - getWeeklyRankingString(item.name), + getWeeklyRankingString(item.name,context), value.indexOf(item)+1 ), SecondaryTextComponent( diff --git a/test/statistics_test.dart b/test/statistics_test.dart index 02d9821..3cdf151 100644 --- a/test/statistics_test.dart +++ b/test/statistics_test.dart @@ -89,10 +89,10 @@ Future main() async { test('check if format helper are working right',() { expect(2,getListOfDistinctElements(getFoodListValid()).length); expect(3,getMapOfDistinctElementsWithCounterAndCalories(getFoodListValid()).keys.length); - expect("Testfood",getToastFoodNameString(getFoodListValid()[0])); - expect("TestfoodTestfoodTestfo ...",getToastFoodNameString(getFoodListValid()[1])); + expect("Testfood",getToastFoodNameString(getFoodListValid()[0],null)); + expect("TestfoodTestfoodTestfo ...",getToastFoodNameString(getFoodListValid()[1],null)); Food food = getFoodListValid()[0]; - expect("Testfood 2 x 600 kcal", getFoodListStringByFood(food.name, 2, 600)); + expect("Testfood 2 x 600 kcal", getFoodListStringByFood(food.name, 2, 600,null)); }); }