From 0673cbf3cea69d0a4f0f493e73cda605754fd271 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Wed, 28 Apr 2021 11:24:23 +0200 Subject: [PATCH] Correctly handle navigation when adding or editing plans --- lib/widgets/nutrition/forms.dart | 19 +++++++++++-------- lib/widgets/workouts/forms.dart | 19 ++++++++++--------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/widgets/nutrition/forms.dart b/lib/widgets/nutrition/forms.dart index baa90e9f8..31606d9d8 100644 --- a/lib/widgets/nutrition/forms.dart +++ b/lib/widgets/nutrition/forms.dart @@ -230,11 +230,18 @@ class PlanForm extends StatelessWidget { } _form.currentState!.save(); - // Save the entry on the server + // Save to DB try { - _plan.id != null - ? await Provider.of(context, listen: false).editPlan(_plan) - : _plan = await Provider.of(context, listen: false).addPlan(_plan); + if (_plan.id != null) { + await Provider.of(context, listen: false).editPlan(_plan); + Navigator.of(context).pop(); + } else { + _plan = await Provider.of(context, listen: false).addPlan(_plan); + Navigator.of(context).pushReplacementNamed( + NutritionalPlanScreen.routeName, + arguments: _plan, + ); + } // Saving was successful, reset the data _descriptionController.clear(); @@ -243,10 +250,6 @@ class PlanForm extends StatelessWidget { } catch (error) { showErrorDialog(error, context); } - Navigator.of(context).pushReplacementNamed( - NutritionalPlanScreen.routeName, - arguments: _plan, - ); }, ), ], diff --git a/lib/widgets/workouts/forms.dart b/lib/widgets/workouts/forms.dart index 907e96815..57e0542ca 100644 --- a/lib/widgets/workouts/forms.dart +++ b/lib/widgets/workouts/forms.dart @@ -80,15 +80,16 @@ class WorkoutForm extends StatelessWidget { _form.currentState!.save(); // Save to DB - _plan.id != null - ? await Provider.of(context, listen: false).editWorkout(_plan) - : _plan = - await Provider.of(context, listen: false).addWorkout(_plan); - - Navigator.of(context).pushReplacementNamed( - WorkoutPlanScreen.routeName, - arguments: _plan, - ); + if (_plan.id != null) { + await Provider.of(context, listen: false).editWorkout(_plan); + Navigator.of(context).pop(); + } else { + _plan = await Provider.of(context, listen: false).addWorkout(_plan); + Navigator.of(context).pushReplacementNamed( + WorkoutPlanScreen.routeName, + arguments: _plan, + ); + } }, ), ],