Skip to content

Commit

Permalink
fix: Don't show un-necessary alert dialog on nutrient page (#1648)
Browse files Browse the repository at this point in the history
* don't show un-necessary alert dialog on
nutrition page

* made isEdited function private

* return  true to let will pop scope pop to previous
screen

* added comments for better understanding

* localizations.okay instead of localizations.close
  • Loading branch information
AshAman999 authored Apr 26, 2022
1 parent f192283 commit 3da10a4
Showing 1 changed file with 50 additions and 21 deletions.
71 changes: 50 additions & 21 deletions packages/smooth_app/lib/pages/product/nutrition_page_loaded.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class _NutritionPageLoadedState extends State<NutritionPageLoaded> {
),
),
),
//return a boolean to decide whether to return to previous page or not
onWillPop: () => _showCancelPopup(localizations),
);
}
Expand Down Expand Up @@ -360,28 +361,40 @@ class _NutritionPageLoadedState extends State<NutritionPageLoaded> {
label: Text(appLocalizations.nutrition_page_add_nutrient),
);

Future<bool> _showCancelPopup(AppLocalizations localizations) async =>
await showDialog<bool>(
context: context,
builder: (BuildContext context) => AlertDialog(
shape: const RoundedRectangleBorder(
borderRadius: ROUNDED_BORDER_RADIUS,
),
title: Text(localizations.general_confirmation),
content: Text(localizations.nutrition_page_close_confirmation),
actions: <TextButton>[
TextButton(
child: Text(localizations.cancel.toUpperCase()),
onPressed: () => Navigator.pop(context, false),
),
TextButton(
child: Text(localizations.close.toUpperCase()),
onPressed: () => Navigator.pop(context, true),
Future<bool> _showCancelPopup(AppLocalizations localizations) async {
//if no changes made then returns true to the onWillPop
// allowing it to let the user return back to previous screen
if (!_isEdited()) {
return true;
}
return await showDialog<bool>(
context: context,
builder: (BuildContext context) => AlertDialog(
shape: const RoundedRectangleBorder(
borderRadius: ROUNDED_BORDER_RADIUS,
),
],
),
) ??
false;
title: Text(localizations.general_confirmation),
content: Text(localizations.nutrition_page_close_confirmation),
actions: <TextButton>[
TextButton(
child: Text(localizations.cancel.toUpperCase()),
// returns false to onWillPop after the alert dialog is closed with cancel button
//blocking return to the previous screen
onPressed: () => Navigator.pop(context, false),
),
TextButton(
child: Text(localizations.okay.toUpperCase()),
// returns true to onWillPop after the alert dialog is closed with close button
//letting return to the previous screen
onPressed: () => Navigator.pop(context, true),
),
],
),
) ??
// in case alert dialog is closed, a false is return
// blocking the return to the previous screen
false;
}

Future<void> _validateAndSave(final AppLocalizations localizations,
final LocalDatabase localDatabase) async {
Expand Down Expand Up @@ -437,4 +450,20 @@ class _NutritionPageLoadedState extends State<NutritionPageLoaded> {
Navigator.of(context).pop(true);
}
}

bool _isEdited() {
for (final String key in _controllers.keys) {
final TextEditingController controller = _controllers[key]!;
if (_nutritionContainer.getValue(key) != null) {
if (_numberFormat.format(_nutritionContainer.getValue(key)) !=
controller.value.text) {
//if any controller is not equal to the value in the container
// then the form is edited, return true
return true;
}
}
}
//else form is not edited just return false
return false;
}
}

0 comments on commit 3da10a4

Please sign in to comment.