mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-30 01:41:05 +01:00
Calculate nutritional values for a meal item
This commit is contained in:
@@ -39,28 +39,36 @@ class Ingredient {
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString)
|
||||
final num carbohydrates;
|
||||
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString, name: 'carbohydrates_sugar')
|
||||
final num carbohydratesSugar;
|
||||
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString)
|
||||
final num carbohydrates_sugar;
|
||||
final num protein;
|
||||
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString)
|
||||
final num fat;
|
||||
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString)
|
||||
final num fat_saturated;
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString, name: 'fat_saturated')
|
||||
final num fatSaturated;
|
||||
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString)
|
||||
final num fibres;
|
||||
|
||||
@JsonKey(required: true, fromJson: toNum, toJson: toString)
|
||||
final num sodium;
|
||||
|
||||
Ingredient({
|
||||
@required this.id,
|
||||
@required this.name,
|
||||
@required this.creationDate,
|
||||
@required this.energy,
|
||||
@required this.carbohydrates,
|
||||
@required this.carbohydrates_sugar,
|
||||
@required this.carbohydratesSugar,
|
||||
@required this.protein,
|
||||
@required this.fat,
|
||||
@required this.fat_saturated,
|
||||
@required this.fatSaturated,
|
||||
@required this.fibres,
|
||||
@required this.sodium,
|
||||
});
|
||||
|
||||
// Boilerplate
|
||||
|
||||
@@ -14,9 +14,11 @@ Ingredient _$IngredientFromJson(Map<String, dynamic> json) {
|
||||
'energy',
|
||||
'carbohydrates',
|
||||
'carbohydrates_sugar',
|
||||
'protein',
|
||||
'fat',
|
||||
'fat_saturated',
|
||||
'fibres'
|
||||
'fibres',
|
||||
'sodium'
|
||||
]);
|
||||
return Ingredient(
|
||||
id: json['id'] as int,
|
||||
@@ -26,10 +28,12 @@ Ingredient _$IngredientFromJson(Map<String, dynamic> json) {
|
||||
: DateTime.parse(json['creation_date'] as String),
|
||||
energy: json['energy'] as int,
|
||||
carbohydrates: toNum(json['carbohydrates'] as String),
|
||||
carbohydrates_sugar: toNum(json['carbohydrates_sugar'] as String),
|
||||
carbohydratesSugar: toNum(json['carbohydrates_sugar'] as String),
|
||||
protein: toNum(json['protein'] as String),
|
||||
fat: toNum(json['fat'] as String),
|
||||
fat_saturated: toNum(json['fat_saturated'] as String),
|
||||
fatSaturated: toNum(json['fat_saturated'] as String),
|
||||
fibres: toNum(json['fibres'] as String),
|
||||
sodium: toNum(json['sodium'] as String),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -40,8 +44,10 @@ Map<String, dynamic> _$IngredientToJson(Ingredient instance) =>
|
||||
'creation_date': toDate(instance.creationDate),
|
||||
'energy': instance.energy,
|
||||
'carbohydrates': toString(instance.carbohydrates),
|
||||
'carbohydrates_sugar': toString(instance.carbohydrates_sugar),
|
||||
'carbohydrates_sugar': toString(instance.carbohydratesSugar),
|
||||
'protein': toString(instance.protein),
|
||||
'fat': toString(instance.fat),
|
||||
'fat_saturated': toString(instance.fat_saturated),
|
||||
'fat_saturated': toString(instance.fatSaturated),
|
||||
'fibres': toString(instance.fibres),
|
||||
'sodium': toString(instance.sodium),
|
||||
};
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:wger/helpers/json.dart';
|
||||
import 'package:wger/models/nutrition/ingredient.dart';
|
||||
@@ -47,5 +48,44 @@ class MealItem {
|
||||
|
||||
// Boilerplate
|
||||
factory MealItem.fromJson(Map<String, dynamic> json) => _$MealItemFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$MealItemToJson(this);
|
||||
|
||||
/// Calculations
|
||||
get nutritionalValues {
|
||||
// This is already done on the server. It might be better to read it from there.
|
||||
var out = {
|
||||
'energy': 0.0,
|
||||
'energyKj': 0.0,
|
||||
'protein': 0.0,
|
||||
'carbohydrates': 0.0,
|
||||
'carbohydrates_sugar': 0.0,
|
||||
'fat': 0.0,
|
||||
'fat_saturated': 0.0,
|
||||
'fibres': 0.0,
|
||||
'sodium': 0.0
|
||||
};
|
||||
|
||||
final weight = this.weightUnit == null ? amount : amount * weightUnit.amount * weightUnit.grams;
|
||||
|
||||
out['energy'] += ingredient.energy * weight / 100;
|
||||
out['energyKj'] += out['energy'] * 4.184;
|
||||
out['protein'] += ingredient.protein * weight / 100;
|
||||
out['carbohydrates'] += ingredient.carbohydrates * weight / 100;
|
||||
out['fat'] += ingredient.fat * weight / 100;
|
||||
|
||||
if (ingredient.fatSaturated != null) {
|
||||
out['fat_saturated'] += ingredient.fatSaturated * weight / 100;
|
||||
}
|
||||
|
||||
if (ingredient.fibres != null) {
|
||||
out['fibres'] += ingredient.fibres * weight / 100;
|
||||
}
|
||||
|
||||
if (ingredient.sodium != null) {
|
||||
out['sodium'] += ingredient.sodium * weight / 100;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,20 +48,22 @@ class MealItemWidget extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
String unit = _item.weightUnit == null ? 'g' : _item.weightUnit.weightUnit.name;
|
||||
String amount = '${_item.amount}$unit';
|
||||
final values = _item.nutritionalValues;
|
||||
|
||||
return Container(
|
||||
padding: EdgeInsets.all(8),
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.all(5),
|
||||
child: Table(
|
||||
children: [
|
||||
TableRow(
|
||||
children: [
|
||||
Text(
|
||||
_item.ingredient.name,
|
||||
textAlign: TextAlign.left,
|
||||
'${_item.amount.toStringAsFixed(0)}$unit ${_item.ingredient.name}',
|
||||
),
|
||||
Text(amount),
|
||||
Text(
|
||||
'${values["energy"].toStringAsFixed(0)} kcal / \n ${values["energyKj"].toStringAsFixed(0)} kJ'),
|
||||
Text('${values["protein"].toStringAsFixed(0)} g'),
|
||||
Text('${values["carbohydrates"].toStringAsFixed(0)} g'),
|
||||
Text('${values["fat"].toStringAsFixed(0)} g'),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user