From f90609f147e14dca1969c5ea92f0fcabc26cb578 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Wed, 18 Sep 2024 21:40:00 +0300 Subject: [PATCH] various dcm cleanups --- lib/models/nutrition/nutritional_goals.dart | 1 + lib/screens/nutritional_plan_screen.dart | 31 ++++++++++--------- lib/screens/nutritional_plans_screen.dart | 2 +- lib/widgets/nutrition/charts.dart | 23 ++++++-------- lib/widgets/nutrition/forms.dart | 13 ++++---- lib/widgets/nutrition/helpers.dart | 2 +- lib/widgets/nutrition/ingredient_dialogs.dart | 19 +++++++----- .../nutritional_meal_item_form_test.dart | 7 ----- 8 files changed, 47 insertions(+), 51 deletions(-) diff --git a/lib/models/nutrition/nutritional_goals.dart b/lib/models/nutrition/nutritional_goals.dart index 59cdf33b..7a668a46 100644 --- a/lib/models/nutrition/nutritional_goals.dart +++ b/lib/models/nutrition/nutritional_goals.dart @@ -134,6 +134,7 @@ class NutritionalGoals { @override String toString() { + // ignore: avoid-nullable-interpolation return 'e: $energy, p: $protein, c: $carbohydrates, cS: $carbohydratesSugar, f: $fat, fS: $fatSaturated, fi: $fiber, s: $sodium'; } diff --git a/lib/screens/nutritional_plan_screen.dart b/lib/screens/nutritional_plan_screen.dart index bb13be6c..6d695a29 100644 --- a/lib/screens/nutritional_plan_screen.dart +++ b/lib/screens/nutritional_plan_screen.dart @@ -112,20 +112,23 @@ class NutritionalPlanScreen extends StatelessWidget { PopupMenuButton( icon: const Icon(Icons.more_vert, color: appBarForeground), onSelected: (value) { - if (value == NutritionalPlanOptions.edit) { - Navigator.pushNamed( - context, - FormScreen.routeName, - arguments: FormScreenArguments( - AppLocalizations.of(context).edit, - PlanForm(nutritionalPlan), - hasListView: true, - ), - ); - } else if (value == NutritionalPlanOptions.delete) { - Provider.of(context, listen: false) - .deletePlan(nutritionalPlan.id!); - Navigator.of(context).pop(); + switch (value) { + case NutritionalPlanOptions.edit: + Navigator.pushNamed( + context, + FormScreen.routeName, + arguments: FormScreenArguments( + AppLocalizations.of(context).edit, + PlanForm(nutritionalPlan), + hasListView: true, + ), + ); + break; + case NutritionalPlanOptions.delete: + Provider.of(context, listen: false) + .deletePlan(nutritionalPlan.id!); + Navigator.of(context).pop(); + break; } }, itemBuilder: (BuildContext context) { diff --git a/lib/screens/nutritional_plans_screen.dart b/lib/screens/nutritional_plans_screen.dart index b3ed56b3..43ce024c 100644 --- a/lib/screens/nutritional_plans_screen.dart +++ b/lib/screens/nutritional_plans_screen.dart @@ -35,7 +35,7 @@ class NutritionalPlansScreen extends StatelessWidget { appBar: EmptyAppBar(AppLocalizations.of(context).nutritionalPlans), floatingActionButton: FloatingActionButton( child: const Icon(Icons.add, color: Colors.white), - onPressed: () async { + onPressed: () { Navigator.pushNamed( context, FormScreen.routeName, diff --git a/lib/widgets/nutrition/charts.dart b/lib/widgets/nutrition/charts.dart index 32c558e1..c429f0a8 100644 --- a/lib/widgets/nutrition/charts.dart +++ b/lib/widgets/nutrition/charts.dart @@ -26,18 +26,6 @@ import 'package:wger/models/nutrition/nutritional_plan.dart'; import 'package:wger/models/nutrition/nutritional_values.dart'; import 'package:wger/widgets/measurements/charts.dart'; -class FlNutritionalPlanGoalWidget extends StatefulWidget { - const FlNutritionalPlanGoalWidget({ - super.key, - required NutritionalPlan nutritionalPlan, - }) : _nutritionalPlan = nutritionalPlan; - - final NutritionalPlan _nutritionalPlan; - - @override - State createState() => FlNutritionalPlanGoalWidgetState(); -} - // * fl_chart doesn't support horizontal bar charts yet. // see https://github.com/imaNNeo/fl_chart/issues/113 // even if it did, i doubt it would let us put text between the gauges/bars @@ -45,7 +33,14 @@ class FlNutritionalPlanGoalWidget extends StatefulWidget { // using multiple colors to show multiple components such as surplus, deficit // * here we draw our own simple gauges that can go beyond 100%, // and support multiple segments -class FlNutritionalPlanGoalWidgetState extends State { +class FlNutritionalPlanGoalWidget extends StatelessWidget { + const FlNutritionalPlanGoalWidget({ + super.key, + required NutritionalPlan nutritionalPlan, + }) : _nutritionalPlan = nutritionalPlan; + + final NutritionalPlan _nutritionalPlan; + // normWidth is the width representing 100% completion // note that if val > plan, we will draw beyond this width // therefore, caller must set this width to accommodate surpluses. @@ -91,7 +86,7 @@ class FlNutritionalPlanGoalWidgetState extends State { style: const TextStyle(color: Colors.red), ), ); - } else { - return const SizedBox( - width: 20, - height: 20, - child: CircularProgressIndicator(), - ); } + return const SizedBox( + width: 20, + height: 20, + child: CircularProgressIndicator(), + ); }, ), ], @@ -394,7 +393,7 @@ class IngredientFormState extends State { ElevatedButton( key: const Key(SUBMIT_BUTTON_KEY_NAME), child: Text(AppLocalizations.of(context).save), - onPressed: () async { + onPressed: () { if (!_form.currentState!.validate()) { return; } diff --git a/lib/widgets/nutrition/helpers.dart b/lib/widgets/nutrition/helpers.dart index f4f6d3f7..b60661fb 100644 --- a/lib/widgets/nutrition/helpers.dart +++ b/lib/widgets/nutrition/helpers.dart @@ -105,7 +105,7 @@ void showIngredientDetails(BuildContext context, int id, {void Function()? selec builder: (context) => FutureBuilder( future: Provider.of(context, listen: false).fetchIngredient(id), builder: (BuildContext context, AsyncSnapshot snapshot) { - return IngredientDetails(snapshot, select: select); + return IngredientDetails(snapshot, onSelect: select); }, ), ); diff --git a/lib/widgets/nutrition/ingredient_dialogs.dart b/lib/widgets/nutrition/ingredient_dialogs.dart index e636e22c..b5fcde09 100644 --- a/lib/widgets/nutrition/ingredient_dialogs.dart +++ b/lib/widgets/nutrition/ingredient_dialogs.dart @@ -21,8 +21,8 @@ Widget ingredientImage(String url, BuildContext context) { class IngredientDetails extends StatelessWidget { final AsyncSnapshot snapshot; - final void Function()? select; - const IngredientDetails(this.snapshot, {super.key, this.select}); + final void Function()? onSelect; + const IngredientDetails(this.snapshot, {super.key, this.onSelect}); @override Widget build(BuildContext context) { @@ -76,12 +76,12 @@ class IngredientDetails extends StatelessWidget { ), ), actions: [ - if (snapshot.hasData && select != null) + if (snapshot.hasData && onSelect != null) TextButton( key: const Key('ingredient-details-continue-button'), child: Text(MaterialLocalizations.of(context).continueButtonLabel), onPressed: () { - select!(); + onSelect!(); Navigator.of(context).pop(); }, ), @@ -100,9 +100,14 @@ class IngredientDetails extends StatelessWidget { class IngredientScanResultDialog extends StatelessWidget { final AsyncSnapshot snapshot; final String barcode; - final Function(int id, String name, num? amount) selectIngredient; + final Function(int id, String name, num? amount) onSelectIngredient; - const IngredientScanResultDialog(this.snapshot, this.barcode, this.selectIngredient, {super.key}); + const IngredientScanResultDialog( + this.snapshot, + this.barcode, + this.onSelectIngredient, { + super.key, + }); @override Widget build(BuildContext context) { @@ -181,7 +186,7 @@ class IngredientScanResultDialog extends StatelessWidget { key: const Key('ingredient-scan-result-dialog-confirm-button'), child: Text(MaterialLocalizations.of(context).continueButtonLabel), onPressed: () { - selectIngredient(ingredient!.id, ingredient.name, null); + onSelectIngredient(ingredient!.id, ingredient.name, null); Navigator.of(context).pop(); }, ), diff --git a/test/nutrition/nutritional_meal_item_form_test.dart b/test/nutrition/nutritional_meal_item_form_test.dart index fddca2b3..e37435e9 100644 --- a/test/nutrition/nutritional_meal_item_form_test.dart +++ b/test/nutrition/nutritional_meal_item_form_test.dart @@ -21,7 +21,6 @@ import 'package:wger/widgets/nutrition/forms.dart'; import '../../test_data/nutritional_plans.dart'; import '../fixtures/fixture_reader.dart'; -import '../measurements/measurement_provider_test.mocks.dart'; import '../other/base_provider_test.mocks.dart'; import 'nutritional_plan_form_test.mocks.dart'; @@ -44,15 +43,9 @@ void main() { sodium: 0.5, ); - late MockWgerBaseProvider mockWgerBaseProvider; - var mockNutrition = MockNutritionPlansProvider(); final client = MockClient(); - setUp(() { - mockWgerBaseProvider = MockWgerBaseProvider(); - }); - var plan1 = NutritionalPlan.empty(); var meal1 = Meal();