Flutter-Ernaehrungsapp/lib/android/pages/nav_pages/progress_page.dart

267 lines
7.5 KiB
Dart

import 'package:ernaehrung/android/components/meal_page_text/secondary_big_text_component.dart';
import 'package:ernaehrung/android/components/meal_page_text/secondary_text_component.dart';
import 'package:ernaehrung/android/components/meal_page_text/title_component.dart';
import 'package:ernaehrung/android/config/statistics.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import '../../config/format_helper.dart';
class ProgressPage extends StatelessWidget {
final String title;
final Color backgroundColor = const Color(0xff47a44b);
const ProgressPage({Key? key, required this.title}) : super(key: key);
String get getTitle => title;
@override
String toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) {
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 Container(
height: 284,
decoration: BoxDecoration(
border: Border.all(),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const TitleComponent("Kalorien"),
const SizedBox(
height: 10,
),
const SecondaryTextComponent("Durchschnittlich"),
SecondaryBigTextComponent(value.toString()),
SizedBox(
height: 200,
child: BarChart(
BarChartData(
barTouchData: barTouchData,
titlesData: titlesData,
borderData: borderData,
barGroups: barGroups,
gridData: FlGridData(show: false),
alignment: BarChartAlignment.spaceAround,
maxY: 200,
),
),
)
],
),
);
},
),
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()),
],
),
],
);
},
),
],
),
),
),
);
}
BarTouchData get barTouchData => BarTouchData(
enabled: false,
touchTooltipData: BarTouchTooltipData(
tooltipBgColor: Colors.transparent,
tooltipPadding: EdgeInsets.zero,
tooltipMargin: 4,
getTooltipItem: (
BarChartGroupData group,
int groupIndex,
BarChartRodData rod,
int rodIndex,
) {
return BarTooltipItem(
rod.toY.round().toString(),
const TextStyle(
color: Colors.orange,
fontWeight: FontWeight.bold,
),
);
},
),
);
Widget getTitles(double value, TitleMeta meta) {
const style = TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
fontSize: 14,
);
String text;
switch (value.toInt()) {
case 0:
text = 'Mn';
break;
case 1:
text = 'Te';
break;
case 2:
text = 'Wd';
break;
case 3:
text = 'Tu';
break;
case 4:
text = 'Fr';
break;
case 5:
text = 'St';
break;
case 6:
text = 'Sn';
break;
default:
text = '';
break;
}
return SideTitleWidget(
axisSide: meta.axisSide,
space: 4,
child: Text(text, style: style),
);
}
FlTitlesData get titlesData => FlTitlesData(
show: true,
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 30,
getTitlesWidget: getTitles,
),
),
leftTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
topTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
rightTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
);
FlBorderData get borderData => FlBorderData(
show: false,
);
LinearGradient get _barsGradient => const LinearGradient(
colors: [
Colors.cyan,
Colors.deepPurple,
],
begin: Alignment.bottomCenter,
end: Alignment.topCenter,
);
List<BarChartGroupData> get barGroups => [
BarChartGroupData(
x: 0,
barRods: [
BarChartRodData(
toY: 8,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 1,
barRods: [
BarChartRodData(
toY: 10,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 2,
barRods: [
BarChartRodData(
toY: 14,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 3,
barRods: [
BarChartRodData(
toY: 150,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 4,
barRods: [
BarChartRodData(
toY: 13,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 5,
barRods: [
BarChartRodData(
toY: 10,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
BarChartGroupData(
x: 6,
barRods: [
BarChartRodData(
toY: 16,
gradient: _barsGradient,
)
],
showingTooltipIndicators: [0],
),
];
}