diff --git a/lib/client.dart b/lib/client.dart index b52c2d7..bd380ed 100644 --- a/lib/client.dart +++ b/lib/client.dart @@ -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> 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; } diff --git a/lib/models/diary.dart b/lib/models/diary.dart index 8716c91..004bc7f 100644 --- a/lib/models/diary.dart +++ b/lib/models/diary.dart @@ -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 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; } diff --git a/lib/models/entry.dart b/lib/models/entry.dart index ba27139..322a8f7 100644 --- a/lib/models/entry.dart +++ b/lib/models/entry.dart @@ -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; } diff --git a/lib/models/meal.dart b/lib/models/meal.dart index 212e052..3e762e7 100644 --- a/lib/models/meal.dart +++ b/lib/models/meal.dart @@ -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; } diff --git a/lib/models/product.dart b/lib/models/product.dart index 5aa3739..c4a18f1 100644 --- a/lib/models/product.dart +++ b/lib/models/product.dart @@ -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 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; } diff --git a/lib/screens/add_product.dart b/lib/screens/add_product.dart index 8f3268b..bc93bfe 100644 --- a/lib/screens/add_product.dart +++ b/lib/screens/add_product.dart @@ -16,6 +16,7 @@ class _AddProductScreen extends State { final nameController = TextEditingController(); final carbController = TextEditingController(); final fatController = TextEditingController(); + final fiberController = TextEditingController(); final proteinController = TextEditingController(); @override @@ -23,6 +24,7 @@ class _AddProductScreen extends State { nameController.dispose(); carbController.dispose(); fatController.dispose(); + fiberController.dispose(); proteinController.dispose(); super.dispose(); } @@ -59,6 +61,13 @@ class _AddProductScreen extends State { 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 { 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 { // ignore } + try { + calories += double.parse(fiberController.text) * 2; + } catch (e) { + // ignore + } + return calories; } @@ -163,6 +179,19 @@ class _AddProductScreen extends State { setState(() {}); }, ), + TextFormField( + decoration: const InputDecoration( + labelText: 'Fiber', + ), + keyboardType:const TextInputType.numberWithOptions(decimal: true), + inputFormatters: [ + FilteringTextInputFormatter.allow(RegExp(r'^(\d+)?\.?\d{0,2}')), + ], + controller: fiberController, + onChanged: (String value) { + setState(() {}); + }, + ), Text( "${calculateCalories()} kcal", textAlign: TextAlign.right, diff --git a/lib/widgets/macro.dart b/lib/widgets/macro.dart index 5a4c4dd..d4a002f 100644 --- a/lib/widgets/macro.dart +++ b/lib/widgets/macro.dart @@ -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( diff --git a/lib/widgets/product.dart b/lib/widgets/product.dart index 54e70ec..69e0961 100644 --- a/lib/widgets/product.dart +++ b/lib/widgets/product.dart @@ -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, diff --git a/pubspec.lock b/pubspec.lock index fec9a81..0c0749d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index 3c67c8e..30099c8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: