From 9ef0c1089623080100f910adf130c582183fde49 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 1 Feb 2024 15:18:44 +0100 Subject: [PATCH] Add common widget for macronutritional goals --- lib/models/nutrition/nutritional_plan.dart | 5 +- lib/screens/nutritional_plan_screen.dart | 1 + lib/widgets/nutrition/forms.dart | 116 +++++++++++---------- 3 files changed, 63 insertions(+), 59 deletions(-) diff --git a/lib/models/nutrition/nutritional_plan.dart b/lib/models/nutrition/nutritional_plan.dart index cd7eab09..e5493fe5 100644 --- a/lib/models/nutrition/nutritional_plan.dart +++ b/lib/models/nutrition/nutritional_plan.dart @@ -62,13 +62,13 @@ class NutritionalPlan { NutritionalPlan({ this.id, + required this.description, + required this.creationDate, this.onlyLogging = false, this.goalEnergy, this.goalProtein, this.goalCarbohydrates, this.goalFat, - required this.description, - required this.creationDate, List? meals, List? logs, }) { @@ -79,6 +79,7 @@ class NutritionalPlan { NutritionalPlan.empty() { creationDate = DateTime.now(); description = ''; + onlyLogging = false; } // Boilerplate diff --git a/lib/screens/nutritional_plan_screen.dart b/lib/screens/nutritional_plan_screen.dart index f79dddc4..636e4594 100644 --- a/lib/screens/nutritional_plan_screen.dart +++ b/lib/screens/nutritional_plan_screen.dart @@ -81,6 +81,7 @@ class NutritionalPlanScreen extends StatelessWidget { arguments: FormScreenArguments( AppLocalizations.of(context).edit, PlanForm(nutritionalPlan), + hasListView: true, ), ); diff --git a/lib/widgets/nutrition/forms.dart b/lib/widgets/nutrition/forms.dart index 98c63d71..1a4242eb 100644 --- a/lib/widgets/nutrition/forms.dart +++ b/lib/widgets/nutrition/forms.dart @@ -379,7 +379,7 @@ class _PlanFormState extends State { Widget build(BuildContext context) { return Form( key: _form, - child: Column( + child: ListView( children: [ // Description TextFormField( @@ -401,7 +401,6 @@ class _PlanFormState extends State { }); widget._plan.onlyLogging = value; }, - dense: false, ), SwitchListTile( title: Text(AppLocalizations.of(context).addGoalsToPlan), @@ -412,14 +411,16 @@ class _PlanFormState extends State { _addGoals = !_addGoals; }); }, - dense: false, ), if (_addGoals) Column( children: [ TextFormField( key: const Key('field-goal-energy'), - decoration: InputDecoration(labelText: AppLocalizations.of(context).goalEnergy), + decoration: InputDecoration( + labelText: AppLocalizations.of(context).goalEnergy, + suffix: Text(AppLocalizations.of(context).kcal), + ), controller: _goalEnergyController, keyboardType: TextInputType.number, onSaved: (newValue) { @@ -437,63 +438,23 @@ class _PlanFormState extends State { return null; }, ), - TextFormField( + GoalMacros( + widget: widget, + label: AppLocalizations.of(context).goalProtein, + onSaved: (String value) => widget._plan.goalProtein = double.parse(value), key: const Key('field-goal-protein'), - decoration: InputDecoration(labelText: AppLocalizations.of(context).goalProtein), - keyboardType: TextInputType.number, - onSaved: (newValue) { - widget._plan.goalProtein = double.parse(newValue!); - }, - validator: (value) { - if (value == '') { - return null; - } - try { - double.parse(value!); - } catch (error) { - return AppLocalizations.of(context).enterValidNumber; - } - return null; - }, ), - TextFormField( + GoalMacros( + widget: widget, + label: AppLocalizations.of(context).goalCarbohydrates, + onSaved: (String value) => widget._plan.goalCarbohydrates = double.parse(value), key: const Key('field-goal-carbohydrates'), - decoration: - InputDecoration(labelText: AppLocalizations.of(context).goalCarbohydrates), - keyboardType: TextInputType.number, - onSaved: (newValue) { - widget._plan.goalCarbohydrates = double.parse(newValue!); - }, - validator: (value) { - if (value == '') { - return null; - } - try { - double.parse(value!); - } catch (error) { - return AppLocalizations.of(context).enterValidNumber; - } - return null; - }, ), - TextFormField( + GoalMacros( + widget: widget, + label: AppLocalizations.of(context).goalFat, + onSaved: (String value) => widget._plan.goalFat = double.parse(value), key: const Key('field-goal-fat'), - decoration: InputDecoration(labelText: AppLocalizations.of(context).goalFat), - keyboardType: TextInputType.number, - onSaved: (newValue) { - widget._plan.goalFat = double.parse(newValue!); - }, - validator: (value) { - if (value == '') { - return null; - } - try { - double.parse(value!); - } catch (error) { - return AppLocalizations.of(context).enterValidNumber; - } - return null; - }, ), ], ), @@ -512,7 +473,8 @@ class _PlanFormState extends State { // Save to DB try { if (widget._plan.id != null) { - await Provider.of(context, listen: false).editPlan(_plan); + await Provider.of(context, listen: false) + .editPlan(widget._plan); if (context.mounted) { Navigator.of(context).pop(); } @@ -545,3 +507,43 @@ class _PlanFormState extends State { ); } } + +class GoalMacros extends StatelessWidget { + GoalMacros({ + super.key, + required this.widget, + required this.label, + required this.onSaved, + }); + + final PlanForm widget; + final String label; + final Function onSaved; + final TextEditingController controller = TextEditingController(); + + @override + Widget build(BuildContext context) { + return TextFormField( + controller: controller, + decoration: InputDecoration( + labelText: label, + suffixText: AppLocalizations.of(context).g, + ), + keyboardType: TextInputType.number, + onSaved: (newValue) { + onSaved(newValue); + }, + validator: (value) { + if (value == '') { + return null; + } + try { + double.parse(value!); + } catch (error) { + return AppLocalizations.of(context).enterValidNumber; + } + return null; + }, + ); + } +}