From a989878cca03d55bc1b788859823462e43dd7928 Mon Sep 17 00:00:00 2001 From: bogdan <1926167@stud.hs-mannheim.de> Date: Fri, 2 Jun 2023 01:42:56 +0200 Subject: [PATCH] optimierung --- lib/android/andoird_app.dart | 2 +- .../components/card/card_component.dart | 9 +- lib/android/components/card_component.dart | 91 ++-- .../components/meal_page_food_component.dart | 13 +- .../meal_page_text/days_component.dart | 5 +- .../statistics_today_component.dart | 2 - lib/android/pages/nav_pages/main_page.dart | 5 +- .../pages/nav_pages/meal_plan_page.dart | 24 +- .../pages/nav_pages/progress_page.dart | 402 ++++++++++-------- lib/main.dart | 2 - 10 files changed, 301 insertions(+), 254 deletions(-) diff --git a/lib/android/andoird_app.dart b/lib/android/andoird_app.dart index b5062be..02c049c 100644 --- a/lib/android/andoird_app.dart +++ b/lib/android/andoird_app.dart @@ -14,7 +14,7 @@ class AndroidApp extends StatelessWidget { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( - scaffoldBackgroundColor: Colors.grey.shade100, //<-- SEE + scaffoldBackgroundColor: Colors.grey.shade200, //<-- SEE ), home: box.get("USER") == null ? const OnboardingPage() : const MainPage(), routes: { diff --git a/lib/android/components/card/card_component.dart b/lib/android/components/card/card_component.dart index 0f5c3a3..a7a1e68 100644 --- a/lib/android/components/card/card_component.dart +++ b/lib/android/components/card/card_component.dart @@ -45,12 +45,9 @@ class CardComponent extends StatelessWidget { Widget build(BuildContext context) { return Container( height: 300, - decoration: BoxDecoration( - color: const Color(0xFF6E7BFB), - borderRadius: const BorderRadius.all(Radius.circular(16)), - border: Border.all( - color: Colors.black, - ), + decoration: const BoxDecoration( + color: Color(0xFF6E7BFB), + borderRadius: BorderRadius.all(Radius.circular(16)), ), margin: const EdgeInsets.fromLTRB(0, 16, 0, 16), child: Column( diff --git a/lib/android/components/card_component.dart b/lib/android/components/card_component.dart index d5405b2..1bd69d5 100644 --- a/lib/android/components/card_component.dart +++ b/lib/android/components/card_component.dart @@ -11,7 +11,10 @@ class CardComponent extends StatefulWidget { final bool addButtonVisible; const CardComponent( - {Key? key, required this.eatingMealName, required this.selectedMeal, this.addButtonVisible = true}) + {Key? key, + required this.eatingMealName, + required this.selectedMeal, + this.addButtonVisible = true}) : super(key: key); @override @@ -40,11 +43,14 @@ class _CardComponentState extends State { } getImageOfMeal() { - if (widget.eatingMealName.toLowerCase() == dotenv.env['BREAKFAST_FIELD']!.toLowerCase()) { + if (widget.eatingMealName.toLowerCase() == + dotenv.env['BREAKFAST_FIELD']!.toLowerCase()) { return const Image(image: AssetImage('assets/images/tea.png')); - } else if (widget.eatingMealName.toLowerCase() == dotenv.env['LUNCH_FIELD']!.toLowerCase()) { + } else if (widget.eatingMealName.toLowerCase() == + dotenv.env['LUNCH_FIELD']!.toLowerCase()) { return const Image(image: AssetImage('assets/images/fries.png')); - } else if (widget.eatingMealName.toLowerCase() == dotenv.env['DINNER_FIELD']!.toLowerCase()) { + } else if (widget.eatingMealName.toLowerCase() == + dotenv.env['DINNER_FIELD']!.toLowerCase()) { return const Image(image: AssetImage('assets/images/ice.png')); } } @@ -60,7 +66,7 @@ class _CardComponentState extends State { } Widget getElevatedButton() { - if(widget.addButtonVisible){ + if (widget.addButtonVisible) { return ElevatedButton( onPressed: () async { Navigator.of(context).push(createRoute(widget.eatingMealName)); @@ -74,18 +80,17 @@ class _CardComponentState extends State { style: TextStyle(fontSize: 28), ), ); - }else{ + } else { return const SizedBox.shrink(); } - } int getCountedCalories() { double calories = 0; for (Food food in widget.selectedMeal) { - if (food.calories is int){ + if (food.calories is int) { calories += food.calories as int; - }else if(food.calories is double){ + } else if (food.calories is double) { calories += food.calories as double; } } @@ -94,60 +99,52 @@ class _CardComponentState extends State { @override Widget build(BuildContext context) { - return Card( - margin: const EdgeInsets.fromLTRB(0, 24, 0, 0), + return Container( + margin: const EdgeInsets.fromLTRB(0, 16, 0, 0), child: Column( children: [ Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Row( - children: [ - getImageOfMeal(), - Column( - mainAxisAlignment: MainAxisAlignment.start, - children: [ - SizedBox( - width: MediaQuery.of(context).size.width * 0.6, - child: Text( - StringUtils.capitalize(widget.eatingMealName), - maxLines: 1, - overflow: TextOverflow.ellipsis, - softWrap: false, - style: const TextStyle( - color: Colors.black, - fontWeight: FontWeight.w500, - fontSize: 14), - ), - ), - ], - ) - ], + getImageOfMeal(), + Text( + StringUtils.capitalize(widget.eatingMealName), + maxLines: 1, + overflow: TextOverflow.ellipsis, + softWrap: false, + style: const TextStyle( + color: Colors.black, + fontWeight: FontWeight.w500, + fontSize: 14), ), getElevatedButton(), ], ), SizedBox( - width: MediaQuery.of(context).size.width * 0.8, - child:ListView.builder( + width: MediaQuery.of(context).size.width * 1, + child: ListView.builder( primary: false, shrinkWrap: true, - itemCount: getMapOfDistinctElementsWithCounterAndCalories(widget.selectedMeal).keys.length, + itemCount: getMapOfDistinctElementsWithCounterAndCalories( + widget.selectedMeal) + .keys + .length, itemBuilder: (context, i) { - Map> map = getMapOfDistinctElementsWithCounterAndCalories(widget.selectedMeal); + Map> map = + getMapOfDistinctElementsWithCounterAndCalories( + widget.selectedMeal); String foodName = map.keys.elementAt(i); List values = map.values.elementAt(i); - return Text( - getFoodListStringByFood(foodName, values[0], values[1]) - ); - } - ), + return Text(getFoodListStringByFood( + foodName, values[0], values[1])); + }), ), widget.selectedMeal.isNotEmpty - ? const Divider() - : const SizedBox.shrink(), - widget.selectedMeal.isNotEmpty - ? Text("${getCountedCalories()} kcal") + ? Column( + children: [ + const Divider(), + Text("${getCountedCalories()} kcal") + ], + ) : const SizedBox.shrink(), ], )); diff --git a/lib/android/components/meal_page_food_component.dart b/lib/android/components/meal_page_food_component.dart index c62a067..08321d4 100644 --- a/lib/android/components/meal_page_food_component.dart +++ b/lib/android/components/meal_page_food_component.dart @@ -10,17 +10,18 @@ class MealPageStatisticsFoodComponent extends StatelessWidget { @override Widget build(BuildContext context) { return Container( + margin: const EdgeInsets.fromLTRB(0, 16, 0, 0), + padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16), decoration: const BoxDecoration( - color: Colors.white - ), + color: Colors.white, + borderRadius: BorderRadius.all(Radius.circular(8))), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - const SizedBox( - height: 24, - ), const TitleComponent("Nahrung"), - FoodComponent(box: Hive.box(StatisticsService.instance.todayStatisticsBoxName),) + FoodComponent( + box: Hive.box(StatisticsService.instance.todayStatisticsBoxName), + ) ], ), ); diff --git a/lib/android/components/meal_page_text/days_component.dart b/lib/android/components/meal_page_text/days_component.dart index b7ade1e..4a3811d 100644 --- a/lib/android/components/meal_page_text/days_component.dart +++ b/lib/android/components/meal_page_text/days_component.dart @@ -34,9 +34,7 @@ class _DaysMealPageState extends State { @override Widget build(BuildContext context) { - return Padding( - padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 0), - child: Row( + return Row( mainAxisAlignment: MainAxisAlignment.start, children: [ DaysTextComponent( @@ -65,7 +63,6 @@ class _DaysMealPageState extends State { index: 2 ), ], - ), ); } } diff --git a/lib/android/components/meal_page_text/statistics_today_component.dart b/lib/android/components/meal_page_text/statistics_today_component.dart index 10ef4d2..4386065 100644 --- a/lib/android/components/meal_page_text/statistics_today_component.dart +++ b/lib/android/components/meal_page_text/statistics_today_component.dart @@ -15,8 +15,6 @@ class StatisticsPercentage extends StatelessWidget { valueListenable: StatisticsService.instance.ingredients, builder: (context, value, child) { return Container( - decoration: BoxDecoration( - border: Border.all(width: 2.0, color: Colors.black)), height: heightStatisticsContainer, padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 0), child: Row( diff --git a/lib/android/pages/nav_pages/main_page.dart b/lib/android/pages/nav_pages/main_page.dart index 6945c05..d33c526 100644 --- a/lib/android/pages/nav_pages/main_page.dart +++ b/lib/android/pages/nav_pages/main_page.dart @@ -36,9 +36,12 @@ class MainPageState extends State { @override Widget build(BuildContext context) { return Scaffold( + extendBodyBehindAppBar: false, appBar: AppBar( title: Text(pages[currentIndex].title), - backgroundColor: Colors.grey.shade100, + backgroundColor: Colors.transparent, + foregroundColor: Colors.grey.shade400, + elevation: 0, ), body: Padding( padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 8), diff --git a/lib/android/pages/nav_pages/meal_plan_page.dart b/lib/android/pages/nav_pages/meal_plan_page.dart index ebe74dc..ecc3411 100644 --- a/lib/android/pages/nav_pages/meal_plan_page.dart +++ b/lib/android/pages/nav_pages/meal_plan_page.dart @@ -24,16 +24,28 @@ class _MealPlanState extends State { @override Widget build(BuildContext context) { return Scaffold( - body: Container( - margin: const EdgeInsets.all(8), + body: SizedBox( width: double.infinity, height: double.infinity, child: SingleChildScrollView( child: Column( - children: const [ - DaysMealPageComponent(), - StatisticsPercentage(), - MealPageStatisticsFoodComponent(), + children: [ + Container( + decoration: const BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.all(Radius.circular(8))), + child: Padding( + padding: const EdgeInsets.symmetric( + vertical: 8, horizontal: 16), + child: Column( + children: const [ + DaysMealPageComponent(), + StatisticsPercentage(), + ], + ), + ), + ), + const MealPageStatisticsFoodComponent(), ], ), ))); diff --git a/lib/android/pages/nav_pages/progress_page.dart b/lib/android/pages/nav_pages/progress_page.dart index 2cbd688..d2c200b 100644 --- a/lib/android/pages/nav_pages/progress_page.dart +++ b/lib/android/pages/nav_pages/progress_page.dart @@ -20,160 +20,204 @@ class ProgressPage extends StatelessWidget { return getTitle; } - /* - * TODO: in versch. Dateien auslagern, damit der Code nicht voll gemüllt wird - * */ @override Widget build(BuildContext context) { return Scaffold( body: SingleChildScrollView( - child: Padding( - padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 8), - child: Column( - children: [ - ValueListenableBuilder( - valueListenable: StatisticsService.instance.dailyAverageForCurrentWeek, - builder: (context, value, child) { - return SizedBox( - height: 100, - width: 400, - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - const TitleComponent("Täglicher Durchschnitt (in kcal)"), - const SizedBox( - height: 10, - ), - const SecondaryTextComponent("Durchschnittlich"), - SecondaryBigTextComponent("${value.toString()} kcal/Tag"), - ], - ), - ); - }, - ), - ValueListenableBuilder( - valueListenable: StatisticsService.instance.barChartData, - builder: (context, value, child) { - return SizedBox( - height: 325, - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - SizedBox( - height: 15, // Adjust the height of the legend as needed - child: Row( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Container( - margin: const EdgeInsets.symmetric(horizontal: 8.0), // Adjust the spacing between legend items + child: Column( + children: [ + Container( + decoration: const BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.all(Radius.circular(8))), + child: Padding( + padding: + const EdgeInsets.symmetric(vertical: 8, horizontal: 16), + child: Column( + children: [ + ValueListenableBuilder( + valueListenable: StatisticsService + .instance.dailyAverageForCurrentWeek, + builder: (context, value, child) { + return SizedBox( + height: 100, + width: 400, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const TitleComponent("Kalorien"), + const SizedBox( + height: 10, + ), + const SecondaryTextComponent( + "Durchschnittlich"), + SecondaryBigTextComponent( + "${value.toString()} kcal/Tag"), + ], + ), + ); + }, + ), + ValueListenableBuilder( + valueListenable: + StatisticsService.instance.barChartData, + builder: (context, value, child) { + return SizedBox( + height: 300, + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + SizedBox( + height: 15, + // Adjust the height of the legend as needed child: Row( + mainAxisAlignment: MainAxisAlignment.center, children: [ Container( - width: 12, - height: 12, - decoration: const BoxDecoration( - color: Colors.red, // Use the color of the first bar rod for the legend item - shape: BoxShape.circle, + margin: const EdgeInsets.symmetric( + horizontal: 8.0), + // Adjust the spacing between legend items + child: Row( + children: [ + Container( + width: 12, + height: 12, + decoration: const BoxDecoration( + color: Colors.red, + // Use the color of the first bar rod for the legend item + shape: BoxShape.circle, + ), + ), + const SizedBox(width: 4.0), + // Adjust the spacing between the color indicator and the legend label + const Text( + 'Frühstück', + // Replace with your desired legend label + style: TextStyle( + fontSize: + 12), // Adjust the font size of the legend labels + ), + ], ), ), - const SizedBox(width: 4.0), // Adjust the spacing between the color indicator and the legend label - const Text( - 'Frühstück', // Replace with your desired legend label - style: TextStyle(fontSize: 12), // Adjust the font size of the legend labels + Container( + margin: const EdgeInsets.symmetric( + horizontal: 8.0), + // Adjust the spacing between legend items + child: Row( + children: [ + Container( + width: 12, + height: 12, + decoration: const BoxDecoration( + color: Colors.green, + // Use the color of the first bar rod for the legend item + shape: BoxShape.circle, + ), + ), + const SizedBox(width: 4.0), + // Adjust the spacing between the color indicator and the legend label + const Text( + 'Mittagessen', + // Replace with your desired legend label + style: TextStyle( + fontSize: + 12), // Adjust the font size of the legend labels + ), + ], + ), + ), + Container( + margin: const EdgeInsets.symmetric( + horizontal: 8.0), + // Adjust the spacing between legend items + child: Row( + children: [ + Container( + width: 12, + height: 12, + decoration: const BoxDecoration( + color: Colors.yellow, + // Use the color of the first bar rod for the legend item + shape: BoxShape.circle, + ), + ), + const SizedBox(width: 4.0), + // Adjust the spacing between the color indicator and the legend label + const Text( + 'Abendessen', + // Replace with your desired legend label + style: TextStyle( + fontSize: + 12), // Adjust the font size of the legend labels + ), + ], + ), ), ], ), ), - Container( - margin: const EdgeInsets.symmetric(horizontal: 8.0), // Adjust the spacing between legend items - child: Row( - children: [ - Container( - width: 12, - height: 12, - decoration: const BoxDecoration( - color: Colors.green, // Use the color of the first bar rod for the legend item - shape: BoxShape.circle, - ), + Container( + margin: + const EdgeInsets.fromLTRB(0, 24, 0, 0), + height: 250, + child: BarChart( + BarChartData( + barTouchData: barTouchData, + titlesData: titlesData, + borderData: borderData, + barGroups: value, + gridData: FlGridData(show: false), + alignment: BarChartAlignment.spaceAround, ), - const SizedBox(width: 4.0), // Adjust the spacing between the color indicator and the legend label - const Text( - 'Mittagessen', // Replace with your desired legend label - style: TextStyle(fontSize: 12), // Adjust the font size of the legend labels - ), - ], + ), ), - ), - Container( - margin: const EdgeInsets.symmetric(horizontal: 8.0), // Adjust the spacing between legend items - child: Row( - children: [ - Container( - width: 12, - height: 12, - decoration: const BoxDecoration( - color: Colors.yellow, // Use the color of the first bar rod for the legend item - shape: BoxShape.circle, - ), - ), - const SizedBox(width: 4.0), // Adjust the spacing between the color indicator and the legend label - const Text( - 'Abendessen', // Replace with your desired legend label - style: TextStyle(fontSize: 12), // Adjust the font size of the legend labels - ), - ], - ), - ), - ], - ), - ), - Padding( - padding: const EdgeInsets.only(top:50.0), // Adjust the margin value as needed - child: SizedBox( - height: 250, - child: BarChart( - BarChartData( - barTouchData: barTouchData, - titlesData: titlesData, - borderData: borderData, - barGroups: value, - gridData: FlGridData(show: false), - alignment: BarChartAlignment.spaceAround, - ), + ], ), - ), - ), - ], - ), - ); - }, - ), - ValueListenableBuilder( - valueListenable: StatisticsService.instance.weeklyCaloryRanking, - builder: (context, value, child) { - return Column( - //TODO: Loop through the values and display them dynamically - children: [ - const TitleComponent("Lebensmittel mit dem höchsten Kaloriengehalt"), - const SizedBox(height: 24,), - for (var item in value.toSet()) - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - children: [ - SecondaryTextComponent(getWeeklyRankingString(item.name)), - SecondaryTextComponent(item.calories.toString()), - ], - ), + ); + }, + ), ], - ); - }, - ), - ], - ), + ), + )), + Container( + margin: const EdgeInsets.fromLTRB(0, 16, 0, 0), + decoration: const BoxDecoration( + color: Colors.white, + borderRadius: BorderRadius.all(Radius.circular(8))), + child: Padding( + padding: + const EdgeInsets.symmetric(vertical: 8, horizontal: 16), + child: ValueListenableBuilder( + valueListenable: + StatisticsService.instance.weeklyCaloryRanking, + builder: (context, value, child) { + return Column( + children: [ + const TitleComponent( + "Lebensmittel mit dem höchsten Kaloriengehalt"), + const SizedBox( + height: 24, + ), + for (var item in value.toSet()) + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + SecondaryTextComponent( + getWeeklyRankingString(item.name)), + SecondaryTextComponent( + item.calories.toString()), + ], + ), + ], + ); + }, + ), + )) + ], ), ), ); - } BarTouchData get barTouchData => BarTouchData( @@ -240,56 +284,56 @@ class ProgressPage extends StatelessWidget { } FlTitlesData get titlesData => FlTitlesData( - show: true, - bottomTitles: AxisTitles( - sideTitles: SideTitles( - showTitles: true, - reservedSize: 30, - getTitlesWidget: getTitles, - ), + show: true, + bottomTitles: AxisTitles( + sideTitles: SideTitles( + showTitles: true, + reservedSize: 30, + getTitlesWidget: getTitles, ), - topTitles: AxisTitles( - sideTitles: SideTitles(showTitles: false), + ), + topTitles: AxisTitles( + sideTitles: SideTitles(showTitles: false), + ), + rightTitles: AxisTitles( + sideTitles: SideTitles( + showTitles: false, ), - rightTitles: AxisTitles( - sideTitles: SideTitles( - showTitles: false, - ), - ), - leftTitles: AxisTitles( - sideTitles: SideTitles( - showTitles: true, - getTitlesWidget: (value, _) { - // Return a custom widget for each axis value - return Container( - constraints: const BoxConstraints.tightFor(width: double.infinity), - child: Padding( - padding: const EdgeInsets.only(right: 3.0), // Adjust the margin value as needed - child: FittedBox( - fit: BoxFit.scaleDown, - alignment: Alignment.centerLeft, - child: Text( - value.toInt().toString(), - style: const TextStyle( - color: Colors.orange, - fontWeight: FontWeight.bold, - ), + ), + leftTitles: AxisTitles( + sideTitles: SideTitles( + showTitles: true, + getTitlesWidget: (value, _) { + // Return a custom widget for each axis value + return Container( + constraints: + const BoxConstraints.tightFor(width: double.infinity), + child: Padding( + padding: const EdgeInsets.only(right: 3.0), + // Adjust the margin value as needed + child: FittedBox( + fit: BoxFit.scaleDown, + alignment: Alignment.centerLeft, + child: Text( + value.toInt().toString(), + style: const TextStyle( + color: Colors.orange, + fontWeight: FontWeight.bold, ), ), ), - ); - }, - ), - ) - ); + ), + ); + }, + ), + )); FlBorderData get borderData => FlBorderData( - show: true, // Set to true to display the chart border - border: const Border( - bottom: BorderSide(color: Colors.black, width: 1), - left: BorderSide(color: Colors.black, width: 1),// Hide the left border - ), - ); - - + show: true, // Set to true to display the chart border + border: const Border( + bottom: BorderSide(color: Colors.black, width: 1), + left: + BorderSide(color: Colors.black, width: 1), // Hide the left border + ), + ); } diff --git a/lib/main.dart b/lib/main.dart index 0054c1c..e396c02 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -23,9 +23,7 @@ void main() async { await Hive.openBox(dotenv.env['STATISTICS_MAIN_BOX'] ?? 'STATISTICS_MAIN_BOX'); await Hive.openBox(dotenv.env['MEALPLAN_BOX'] ?? 'MEALPLAN'); await Hive.openBox('USER_BOX'); - //Hive.deleteFromDisk(); setupTodayBox(); - //mockDataForWholeWeek(); if (defaultTargetPlatform == TargetPlatform.android) { runApp(const AndroidApp());