feat: adjust text width for web

main
98spag 2023-06-16 12:03:04 +02:00
parent 0cdab96552
commit 686657628a
5 changed files with 30 additions and 13 deletions

View File

@ -124,7 +124,7 @@ class _CardComponentState extends State<CardComponent> {
return Padding( return Padding(
padding: const EdgeInsets.only(top: 10.0), // Adjust the padding as needed padding: const EdgeInsets.only(top: 10.0), // Adjust the padding as needed
child: Text( child: Text(
"${i+1}. ${getFoodListStringByFood(foodName, values[0], values[1])}", "${i+1}. ${getFoodListStringByFood(foodName, values[0], values[1], context)}",
), ),
); );
}), }),

View File

@ -20,7 +20,7 @@ class SearchedFoodComponent extends StatefulWidget {
class _SearchFoodComponentState extends State<SearchedFoodComponent> { class _SearchFoodComponentState extends State<SearchedFoodComponent> {
int counter = 1; int counter = 1;
void storeFood(int counter) async { void storeFood(int counter, BuildContext context) async {
final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!); final mealplanBox = Hive.box(dotenv.env['MEALPLAN_BOX']!);
if (!mealplanBox.isOpen){ if (!mealplanBox.isOpen){
Hive.openBox(dotenv.env['MEALPLAN_BOX']!); Hive.openBox(dotenv.env['MEALPLAN_BOX']!);
@ -29,12 +29,12 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName); StatisticsService.instance.addItemToMainBox(widget.food, widget.cardName);
addValuesToList(mealplanBox, widget.cardName, [widget.food]); addValuesToList(mealplanBox, widget.cardName, [widget.food]);
} }
showSuccessToast(); showSuccessToast(context);
} }
void showSuccessToast(){ void showSuccessToast(BuildContext context){
Fluttertoast.showToast( 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, toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM, gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 5, timeInSecForIosWeb: 5,
@ -172,7 +172,7 @@ class _SearchFoodComponentState extends State<SearchedFoodComponent> {
padding: const EdgeInsets.only(top:12), padding: const EdgeInsets.only(top:12),
child: ElevatedButton( child: ElevatedButton(
onPressed: () async { onPressed: () async {
storeFood(counter); storeFood(counter,context);
}, },
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
foregroundColor: Colors.white, foregroundColor: Colors.white,

View File

@ -1,19 +1,36 @@
import 'package:flutter/cupertino.dart';
import '../models/food.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; 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"; String limitedText = foodName.length > maxWidth ? "${foodName.substring(0, maxWidth - 3)} ... $count x $calories kcal" : "$foodName $count x $calories kcal";
return limitedText; 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; int maxWidth = 40;
if(context != null){
maxWidth = isScreenWidthAbove1000(context) ? 100 : 40;
}
String limitedText = foodName.length > maxWidth ? "${foodName.substring(0, maxWidth - 3)} ..." : foodName; String limitedText = foodName.length > maxWidth ? "${foodName.substring(0, maxWidth - 3)} ..." : foodName;
return limitedText; return limitedText;
} }
String getToastFoodNameString(Food food){ String getToastFoodNameString(Food food,BuildContext? context){
int maxWidth = 25; 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; String limitedText = food.name.length > maxWidth ? "${food.name.substring(0, maxWidth - 3)} ..." : food.name;
return limitedText; return limitedText;
} }

View File

@ -49,7 +49,7 @@ class ProgressPage extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
SecondaryTextComponent( SecondaryTextComponent(
getWeeklyRankingString(item.name), getWeeklyRankingString(item.name,context),
value.indexOf(item)+1 value.indexOf(item)+1
), ),
SecondaryTextComponent( SecondaryTextComponent(

View File

@ -89,10 +89,10 @@ Future<void> main() async {
test('check if format helper are working right',() { test('check if format helper are working right',() {
expect(2,getListOfDistinctElements(getFoodListValid()).length); expect(2,getListOfDistinctElements(getFoodListValid()).length);
expect(3,getMapOfDistinctElementsWithCounterAndCalories(getFoodListValid()).keys.length); expect(3,getMapOfDistinctElementsWithCounterAndCalories(getFoodListValid()).keys.length);
expect("Testfood",getToastFoodNameString(getFoodListValid()[0])); expect("Testfood",getToastFoodNameString(getFoodListValid()[0],null));
expect("TestfoodTestfoodTestfo ...",getToastFoodNameString(getFoodListValid()[1])); expect("TestfoodTestfoodTestfo ...",getToastFoodNameString(getFoodListValid()[1],null));
Food food = getFoodListValid()[0]; 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));
}); });
} }