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

View file

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

View file

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

View file

@ -5,6 +5,7 @@ class Product {
final double protein;
final double carb;
final double fat;
final double fiber;
Product({
required this.id,
@ -13,6 +14,7 @@ class Product {
required this.protein,
required this.carb,
required this.fat,
required this.fiber,
});
Product.fromJson(Map<String, dynamic> map):
@ -21,5 +23,6 @@ class Product {
calories = map['calories'] as double,
protein = map['protein'] 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 carbController = TextEditingController();
final fatController = TextEditingController();
final fiberController = TextEditingController();
final proteinController = TextEditingController();
@override
@ -23,6 +24,7 @@ class _AddProductScreen extends State<AddProductScreen> {
nameController.dispose();
carbController.dispose();
fatController.dispose();
fiberController.dispose();
proteinController.dispose();
super.dispose();
}
@ -59,6 +61,13 @@ class _AddProductScreen extends State<AddProductScreen> {
return;
}
try {
double.parse(fiberController.text);
} catch (e) {
showError("Fiber must be a number");
return;
}
try {
double.parse(proteinController.text);
} catch (e) {
@ -71,6 +80,7 @@ class _AddProductScreen extends State<AddProductScreen> {
carb: double.parse(carbController.text),
fat: double.parse(fatController.text),
protein: double.parse(proteinController.text),
fiber: double.parse(fiberController.text),
name: nameController.text,
);
var product = Product.fromJson(productJson);
@ -102,6 +112,12 @@ class _AddProductScreen extends State<AddProductScreen> {
// ignore
}
try {
calories += double.parse(fiberController.text) * 2;
} catch (e) {
// ignore
}
return calories;
}
@ -163,6 +179,19 @@ class _AddProductScreen extends State<AddProductScreen> {
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(
"${calculateCalories()} kcal",
textAlign: TextAlign.right,

View file

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

View file

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

View file

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

View file

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