added fiber

This commit is contained in:
doman 2023-07-30 20:44:50 +02:00
parent 657a7545ba
commit e2db6a6181
10 changed files with 84 additions and 18 deletions

View file

@ -1,6 +1,7 @@
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'dart:convert'; import 'dart:convert';
import 'dart:html'; import 'dart:html';
import 'package:intl/intl.dart';
class ApiClient { class ApiClient {
@ -148,8 +149,9 @@ class ApiClient {
} }
Future<Map<String, dynamic>> getDiary({required DateTime date}) async { Future<Map<String, dynamic>> getDiary({required DateTime date}) async {
var formatter = DateFormat('yyyy-MM-dd');
var params = { var params = {
"date": "${date.year}-${date.month}-${date.day}", "date": formatter.format(date),
}; };
var response = await get("/diary?${Uri(queryParameters: params).query}"); var response = await get("/diary?${Uri(queryParameters: params).query}");
return response; return response;
@ -225,12 +227,14 @@ class ApiClient {
required double protein, required double protein,
required double carb, required double carb,
required double fat, required double fat,
required double fiber,
}) async { }) async {
var response = await post("/product", { var response = await post("/product", {
"name": name, "name": name,
"protein": protein, "protein": protein,
"carb": carb, "carb": carb,
"fat": fat, "fat": fat,
"fiber": fiber,
}); });
return response; return response;
} }

View file

@ -9,6 +9,7 @@ class Diary {
final double protein; final double protein;
final double carb; final double carb;
final double fat; final double fat;
final double fiber;
Diary({ Diary({
required this.id, required this.id,
@ -18,6 +19,7 @@ class Diary {
required this.protein, required this.protein,
required this.carb, required this.carb,
required this.fat, required this.fat,
required this.fiber,
}); });
Diary.fromJson(Map<String, dynamic> map): Diary.fromJson(Map<String, dynamic> map):
@ -27,5 +29,6 @@ class Diary {
calories = map['calories'] as double, calories = map['calories'] as double,
protein = map['protein'] as double, protein = map['protein'] as double,
carb = map['carb'] as double, carb = map['carb'] as double,
fat = map['fat'] as double; fat = map['fat'] as double,
fiber = map['fiber'] as double;
} }

View file

@ -8,6 +8,7 @@ class Entry {
final double calories; final double calories;
final double protein; final double protein;
final double fat; final double fat;
final double fiber;
final double carb; final double carb;
@ -19,6 +20,7 @@ class Entry {
required this.calories, required this.calories,
required this.protein, required this.protein,
required this.fat, required this.fat,
required this.fiber,
required this.carb, required this.carb,
}); });
@ -30,5 +32,6 @@ class Entry {
calories = map['calories'] as double, calories = map['calories'] as double,
protein = map['protein'] as double, protein = map['protein'] as double,
fat = map['fat'] as double, fat = map['fat'] as double,
fiber = map['fiber'] as double,
carb = map['carb'] as double; carb = map['carb'] as double;
} }

View file

@ -10,6 +10,7 @@ class Meal {
final double protein; final double protein;
final double carb; final double carb;
final double fat; final double fat;
final double fiber;
final int diaryId; final int diaryId;
Meal({ Meal({
@ -21,6 +22,7 @@ class Meal {
required this.protein, required this.protein,
required this.carb, required this.carb,
required this.fat, required this.fat,
required this.fiber,
required this.diaryId, required this.diaryId,
}); });
@ -33,5 +35,6 @@ class Meal {
protein = map['protein'] as double, protein = map['protein'] as double,
carb = map['carb'] as double, carb = map['carb'] as double,
fat = map['fat'] as double, fat = map['fat'] as double,
fiber = map['fiber'] as double,
diaryId = map['diary_id'] as int; diaryId = map['diary_id'] as int;
} }

View file

@ -5,6 +5,7 @@ class Product {
final double protein; final double protein;
final double carb; final double carb;
final double fat; final double fat;
final double fiber;
Product({ Product({
required this.id, required this.id,
@ -13,6 +14,7 @@ class Product {
required this.protein, required this.protein,
required this.carb, required this.carb,
required this.fat, required this.fat,
required this.fiber,
}); });
Product.fromJson(Map<String, dynamic> map): Product.fromJson(Map<String, dynamic> map):
@ -21,5 +23,6 @@ class Product {
calories = map['calories'] as double, calories = map['calories'] as double,
protein = map['protein'] as double, protein = map['protein'] as double,
carb = map['carb'] as double, carb = map['carb'] as double,
fat = map['fat'] as double; fat = map['fat'] as double,
fiber = map['fiber'] as double;
} }

View file

@ -16,6 +16,7 @@ class _AddProductScreen extends State<AddProductScreen> {
final nameController = TextEditingController(); final nameController = TextEditingController();
final carbController = TextEditingController(); final carbController = TextEditingController();
final fatController = TextEditingController(); final fatController = TextEditingController();
final fiberController = TextEditingController();
final proteinController = TextEditingController(); final proteinController = TextEditingController();
@override @override
@ -23,6 +24,7 @@ class _AddProductScreen extends State<AddProductScreen> {
nameController.dispose(); nameController.dispose();
carbController.dispose(); carbController.dispose();
fatController.dispose(); fatController.dispose();
fiberController.dispose();
proteinController.dispose(); proteinController.dispose();
super.dispose(); super.dispose();
} }
@ -59,6 +61,13 @@ class _AddProductScreen extends State<AddProductScreen> {
return; return;
} }
try {
double.parse(fiberController.text);
} catch (e) {
showError("Fiber must be a number");
return;
}
try { try {
double.parse(proteinController.text); double.parse(proteinController.text);
} catch (e) { } catch (e) {
@ -71,6 +80,7 @@ class _AddProductScreen extends State<AddProductScreen> {
carb: double.parse(carbController.text), carb: double.parse(carbController.text),
fat: double.parse(fatController.text), fat: double.parse(fatController.text),
protein: double.parse(proteinController.text), protein: double.parse(proteinController.text),
fiber: double.parse(fiberController.text),
name: nameController.text, name: nameController.text,
); );
var product = Product.fromJson(productJson); var product = Product.fromJson(productJson);
@ -102,6 +112,12 @@ class _AddProductScreen extends State<AddProductScreen> {
// ignore // ignore
} }
try {
calories += double.parse(fiberController.text) * 2;
} catch (e) {
// ignore
}
return calories; return calories;
} }
@ -163,6 +179,19 @@ class _AddProductScreen extends State<AddProductScreen> {
setState(() {}); setState(() {});
}, },
), ),
TextFormField(
decoration: const InputDecoration(
labelText: 'Fiber',
),
keyboardType:const TextInputType.numberWithOptions(decimal: true),
inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')),
],
controller: fiberController,
onChanged: (String value) {
setState(() {});
},
),
Text( Text(
"${calculateCalories()} kcal", "${calculateCalories()} kcal",
textAlign: TextAlign.right, textAlign: TextAlign.right,

View file

@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
class MacroWidget extends StatelessWidget { class MacroWidget extends StatelessWidget {
final double? amount; final double? amount;
final double? calories; final double? calories;
final double? fiber;
final double protein; final double protein;
final double carb; final double carb;
final double fat; final double fat;
@ -15,6 +16,7 @@ class MacroWidget extends StatelessWidget {
this.calories, this.calories,
this.amount, this.amount,
this.child, this.child,
this.fiber,
required this.protein, required this.protein,
required this.carb, required this.carb,
required this.fat, required this.fat,
@ -50,6 +52,19 @@ class MacroWidget extends StatelessWidget {
), ),
]; ];
if (fiber != null) {
elements.add(
Expanded(
flex: 1,
child: Text(
"f: ${fiber!.toStringAsFixed(1)}g",
style: style,
textAlign: TextAlign.center,
),
),
);
}
if (calories != null) { if (calories != null) {
elements.add( elements.add(
Expanded( Expanded(
@ -58,9 +73,9 @@ class MacroWidget extends StatelessWidget {
"${calories!.toStringAsFixed(1)} kcal", "${calories!.toStringAsFixed(1)} kcal",
style: style, style: style,
textAlign: TextAlign.center, textAlign: TextAlign.center,
),
), ),
); ),
);
} }
if (amount != null) { if (amount != null) {
@ -72,8 +87,8 @@ class MacroWidget extends StatelessWidget {
style: style, style: style,
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
), ),
); );
} }
if (child != null) { if (child != null) {
@ -81,21 +96,17 @@ class MacroWidget extends StatelessWidget {
Expanded( Expanded(
flex: 1, flex: 1,
child: child!, child: child!,
), ),
); );
} }
if (amount == null && calories == null && child == null) { if (elements.length < 4) {
elements.add( elements.add(
Expanded( const Expanded(
flex: 1, flex: 1,
child: Text( child: Text(""),
"", ),
style: style, );
textAlign: TextAlign.center,
),
),
);
} }
return Padding( return Padding(

View file

@ -30,6 +30,7 @@ class ProductWidget extends StatelessWidget {
protein: product.protein, protein: product.protein,
carb: product.carb, carb: product.carb,
fat: product.fat, fat: product.fat,
fiber: product.fiber,
style: Theme.of(context).textTheme.bodyMedium!.copyWith( style: Theme.of(context).textTheme.bodyMedium!.copyWith(
color: Theme.of(context).colorScheme.secondary, color: Theme.of(context).colorScheme.secondary,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,

View file

@ -91,6 +91,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.0.2" version: "4.0.2"
intl:
dependency: "direct main"
description:
name: intl
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
url: "https://pub.dev"
source: hosted
version: "0.18.1"
js: js:
dependency: transitive dependency: transitive
description: description:

View file

@ -36,6 +36,7 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2 cupertino_icons: ^1.0.2
http: ^1.1.0 http: ^1.1.0
intl: ^0.18.1
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: