mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-02 18:19:25 +01:00
Show percent of energy and g per body-kg for macros
This commit is contained in:
@@ -60,3 +60,12 @@ const BAR_WEIGHT = 20;
|
||||
|
||||
/// ID of the equipment entry for barbell
|
||||
const ID_EQUIPMENT_BARBELL = 1;
|
||||
|
||||
/// kcal per gram of protein (approx)
|
||||
const ENERGY_PROTEIN = 4;
|
||||
|
||||
/// kcal per gram of carbohydrates (approx)
|
||||
const ENERGY_CARBOHYDRATES = 4;
|
||||
|
||||
/// kcal per gram of fat (approx)
|
||||
const ENERGY_FAT = 9;
|
||||
|
||||
@@ -289,6 +289,16 @@
|
||||
"@kcal": {
|
||||
"description": "Energy in a meal in kilocalories, kcal"
|
||||
},
|
||||
"macronutrients": "Macronutrients",
|
||||
"percentEnergy": "Percent of energy",
|
||||
"gPerBodyKg": "g per body kg",
|
||||
"@gPerBodyKg": {
|
||||
"description": "Label used for total sums of e.g. calories or similar in grams per Kg of body weight"
|
||||
},
|
||||
"total": "Total",
|
||||
"@total": {
|
||||
"description": "Label used for total sums of e.g. calories or similar"
|
||||
},
|
||||
"kJ": "kJ",
|
||||
"@kJ": {
|
||||
"description": "Energy in a meal in kilo joules, kJ"
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/helpers/json.dart';
|
||||
import 'package:wger/models/nutrition/log.dart';
|
||||
import 'package:wger/models/nutrition/meal.dart';
|
||||
@@ -74,6 +75,28 @@ class NutritionalPlan {
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Calculates the percentage each macro nutrient adds to the total energy
|
||||
BaseNutritionalValues energyPercentage(NutritionalValues values) {
|
||||
return BaseNutritionalValues(
|
||||
values.protein > 0 ? ((values.protein * ENERGY_PROTEIN * 100) / values.energy) : 0,
|
||||
values.carbohydrates > 0
|
||||
? ((values.carbohydrates * ENERGY_CARBOHYDRATES * 100) / values.energy)
|
||||
: 0,
|
||||
values.fat > 0 ? ((values.fat * ENERGY_FAT * 100) / values.energy) : 0,
|
||||
);
|
||||
}
|
||||
|
||||
/// Calculates the grams per body-kg for each macro nutrient
|
||||
BaseNutritionalValues gPerBodyKg(num weight, NutritionalValues values) {
|
||||
assert(weight > 0);
|
||||
|
||||
return BaseNutritionalValues(
|
||||
values.protein / weight,
|
||||
values.carbohydrates / weight,
|
||||
values.fat / weight,
|
||||
);
|
||||
}
|
||||
|
||||
Map<DateTime, NutritionalValues> get logEntriesValues {
|
||||
var out = <DateTime, NutritionalValues>{};
|
||||
for (var log in logs) {
|
||||
|
||||
@@ -88,3 +88,11 @@ class NutritionalValues {
|
||||
return 'e: $energy, p: $protein, c: $carbohydrates, cS: $carbohydratesSugar, f: $fat, fS: $fatSaturated, fi: $fibres, s: $sodium';
|
||||
}
|
||||
}
|
||||
|
||||
class BaseNutritionalValues {
|
||||
double protein = 0;
|
||||
double carbohydrates = 0;
|
||||
double fat = 0;
|
||||
|
||||
BaseNutritionalValues(this.protein, this.carbohydrates, this.fat);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,11 @@ class BodyWeightProvider extends WgerBaseProvider with ChangeNotifier {
|
||||
_entries = [];
|
||||
}
|
||||
|
||||
/// Returns the latest (newest) weight entry or null if there are no entries
|
||||
WeightEntry? getLastEntry() {
|
||||
return this._entries.length > 0 ? this._entries.last : null;
|
||||
}
|
||||
|
||||
WeightEntry findById(int id) {
|
||||
return _entries.firstWhere((plan) => plan.id == id);
|
||||
}
|
||||
@@ -47,7 +52,7 @@ class BodyWeightProvider extends WgerBaseProvider with ChangeNotifier {
|
||||
WeightEntry? findByDate(DateTime date) {
|
||||
try {
|
||||
return _entries.firstWhere((plan) => plan.date == date);
|
||||
} on StateError catch (e) {
|
||||
} on StateError {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/models/nutrition/nutritional_plan.dart';
|
||||
import 'package:wger/models/nutrition/nutritrional_values.dart';
|
||||
import 'package:wger/providers/body_weight.dart';
|
||||
import 'package:wger/screens/form_screen.dart';
|
||||
import 'package:wger/theme/theme.dart';
|
||||
import 'package:wger/widgets/nutrition/charts.dart';
|
||||
import 'package:wger/widgets/nutrition/forms.dart';
|
||||
import 'package:wger/widgets/nutrition/meal.dart';
|
||||
@@ -29,10 +32,16 @@ import 'package:wger/widgets/nutrition/meal.dart';
|
||||
class NutritionalPlanDetailWidget extends StatelessWidget {
|
||||
final NutritionalPlan _nutritionalPlan;
|
||||
NutritionalPlanDetailWidget(this._nutritionalPlan);
|
||||
static const double tablePadding = 7;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final nutritionalValues = _nutritionalPlan.nutritionalValues;
|
||||
final valuesPercentage = _nutritionalPlan.energyPercentage(nutritionalValues);
|
||||
final lastWeightEntry = Provider.of<BodyWeightProvider>(context, listen: false).getLastEntry();
|
||||
final valuesGperKg = lastWeightEntry != null
|
||||
? _nutritionalPlan.gPerBodyKg(lastWeightEntry.weight, nutritionalValues)
|
||||
: null;
|
||||
|
||||
return SliverList(
|
||||
delegate: SliverChildListDelegate(
|
||||
@@ -60,54 +69,135 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
|
||||
height: 220,
|
||||
child: NutritionalPlanPieChartWidget(nutritionalValues),
|
||||
),
|
||||
Container(
|
||||
width: double.infinity,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
child: Table(
|
||||
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
|
||||
border: TableBorder(
|
||||
horizontalInside: BorderSide(width: 1, color: wgerTextMuted),
|
||||
),
|
||||
columnWidths: {0: FractionColumnWidth(0.4)},
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 10),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(AppLocalizations.of(context).energy),
|
||||
Text(AppLocalizations.of(context).protein),
|
||||
Text(AppLocalizations.of(context).carbohydrates),
|
||||
Text(AppLocalizations.of(context).sugars),
|
||||
Text(AppLocalizations.of(context).fat),
|
||||
Text(AppLocalizations.of(context).saturatedFat),
|
||||
Text(AppLocalizations.of(context).fibres),
|
||||
Text(AppLocalizations.of(context).sodium),
|
||||
],
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 3),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(nutritionalValues.energy.toStringAsFixed(0)),
|
||||
Text(nutritionalValues.protein.toStringAsFixed(0)),
|
||||
Text(nutritionalValues.carbohydrates.toStringAsFixed(0)),
|
||||
Text(nutritionalValues.carbohydratesSugar.toStringAsFixed(0)),
|
||||
Text(nutritionalValues.fat.toStringAsFixed(0)),
|
||||
Text(nutritionalValues.fatSaturated.toStringAsFixed(0)),
|
||||
Text(nutritionalValues.fibres.toStringAsFixed(0)),
|
||||
Text(nutritionalValues.sodium.toStringAsFixed(0)),
|
||||
],
|
||||
),
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
TableRow(
|
||||
children: [
|
||||
Text(AppLocalizations.of(context).kcal),
|
||||
Text(AppLocalizations.of(context).g),
|
||||
Text(AppLocalizations.of(context).g),
|
||||
Text(AppLocalizations.of(context).g),
|
||||
Text(AppLocalizations.of(context).g),
|
||||
Text(AppLocalizations.of(context).g),
|
||||
Text(AppLocalizations.of(context).g),
|
||||
Text(AppLocalizations.of(context).g),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding),
|
||||
child: Text(
|
||||
AppLocalizations.of(context).macronutrients,
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
AppLocalizations.of(context).total,
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text(
|
||||
AppLocalizations.of(context).percentEnergy,
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
Text(
|
||||
AppLocalizations.of(context).gPerBodyKg,
|
||||
style: TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
],
|
||||
),
|
||||
TableRow(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding),
|
||||
child: Text(AppLocalizations.of(context).energy),
|
||||
),
|
||||
Text(
|
||||
nutritionalValues.energy.toStringAsFixed(0) +
|
||||
AppLocalizations.of(context).kcal,
|
||||
),
|
||||
Text(''),
|
||||
Text(''),
|
||||
],
|
||||
),
|
||||
TableRow(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding),
|
||||
child: Text(AppLocalizations.of(context).protein),
|
||||
),
|
||||
Text(nutritionalValues.protein.toStringAsFixed(0) +
|
||||
AppLocalizations.of(context).g),
|
||||
Text(valuesPercentage.protein.toStringAsFixed(1)),
|
||||
valuesGperKg != null ? Text(valuesGperKg.protein.toStringAsFixed(1)) : Text(''),
|
||||
],
|
||||
),
|
||||
TableRow(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding),
|
||||
child: Text(AppLocalizations.of(context).carbohydrates),
|
||||
),
|
||||
Text(nutritionalValues.carbohydrates.toStringAsFixed(0) +
|
||||
AppLocalizations.of(context).g),
|
||||
Text(valuesPercentage.carbohydrates.toStringAsFixed(1)),
|
||||
valuesGperKg != null
|
||||
? Text(valuesGperKg.carbohydrates.toStringAsFixed(1))
|
||||
: Text(''),
|
||||
],
|
||||
),
|
||||
TableRow(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding, horizontal: 12),
|
||||
child: Text(AppLocalizations.of(context).sugars),
|
||||
),
|
||||
Text(nutritionalValues.carbohydratesSugar.toStringAsFixed(0) +
|
||||
AppLocalizations.of(context).g),
|
||||
Text(''),
|
||||
Text(''),
|
||||
],
|
||||
),
|
||||
TableRow(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding),
|
||||
child: Text(AppLocalizations.of(context).fat),
|
||||
),
|
||||
Text(nutritionalValues.fat.toStringAsFixed(0) + AppLocalizations.of(context).g),
|
||||
Text(valuesPercentage.fat.toStringAsFixed(1)),
|
||||
valuesGperKg != null ? Text(valuesGperKg.fat.toStringAsFixed(1)) : Text(''),
|
||||
],
|
||||
),
|
||||
TableRow(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding, horizontal: 12),
|
||||
child: Text(AppLocalizations.of(context).saturatedFat),
|
||||
),
|
||||
Text(nutritionalValues.fatSaturated.toStringAsFixed(0) +
|
||||
AppLocalizations.of(context).g),
|
||||
Text(''),
|
||||
Text(''),
|
||||
],
|
||||
),
|
||||
TableRow(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding),
|
||||
child: Text(AppLocalizations.of(context).fibres),
|
||||
),
|
||||
Text(nutritionalValues.fibres.toStringAsFixed(0) +
|
||||
AppLocalizations.of(context).g),
|
||||
Text(''),
|
||||
Text(''),
|
||||
],
|
||||
),
|
||||
TableRow(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: tablePadding),
|
||||
child: Text(AppLocalizations.of(context).sodium),
|
||||
),
|
||||
Text(nutritionalValues.sodium.toStringAsFixed(0) +
|
||||
AppLocalizations.of(context).g),
|
||||
Text(''),
|
||||
Text(''),
|
||||
],
|
||||
),
|
||||
],
|
||||
@@ -116,6 +206,7 @@ class NutritionalPlanDetailWidget extends StatelessWidget {
|
||||
Padding(padding: const EdgeInsets.all(8.0)),
|
||||
Text(
|
||||
AppLocalizations.of(context).nutritionalDiary,
|
||||
textAlign: TextAlign.center,
|
||||
style: Theme.of(context).textTheme.headline6,
|
||||
),
|
||||
Container(
|
||||
|
||||
@@ -872,7 +872,7 @@ packages:
|
||||
name: video_player
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.12"
|
||||
version: "2.1.13"
|
||||
video_player_platform_interface:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
||||
Reference in New Issue
Block a user