added willpopscope
This commit is contained in:
@@ -16,10 +16,18 @@ class CreateRecipe extends StatefulWidget {
|
||||
class _CreateRecipeState extends State<CreateRecipe> {
|
||||
late RecipeProvider recipe;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
recipe.clearRecipe(silent: true);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
recipe = Provider.of<RecipeProvider>(context);
|
||||
return Scaffold(
|
||||
return WillPopScope(
|
||||
onWillPop: _onWillPop,
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Create Recipe'),
|
||||
),
|
||||
@@ -60,6 +68,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -102,4 +111,29 @@ class _CreateRecipeState extends State<CreateRecipe> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> _onWillPop() {
|
||||
if (recipe.isEmpty) {
|
||||
return Future<bool>(
|
||||
() {
|
||||
return true;
|
||||
},
|
||||
);
|
||||
}
|
||||
return showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Leave the page?'),
|
||||
content: const Text('Progress will be lost.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(false),
|
||||
child: const Text('cancel')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('leave')),
|
||||
],
|
||||
),
|
||||
).then((value) => value ?? false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,14 +12,28 @@ class RecipeProvider extends ChangeNotifier implements Recipe {
|
||||
final List<IngredientListEntry> _ingredients = [];
|
||||
final List<CookingStep> _steps = [];
|
||||
|
||||
void clearRecipe() {
|
||||
void clearRecipe({silent = false}) {
|
||||
_title = '';
|
||||
_description = '';
|
||||
_difficulty = null;
|
||||
_ingredients.clear();
|
||||
_steps.clear();
|
||||
if (!silent) {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
bool get isEmpty {
|
||||
return _title.isEmpty &&
|
||||
_description.isEmpty &&
|
||||
_difficulty == null &&
|
||||
_ingredients.isEmpty &&
|
||||
steps.isEmpty;
|
||||
}
|
||||
|
||||
bool get isNotEmpty {
|
||||
return !isEmpty;
|
||||
}
|
||||
|
||||
@override
|
||||
String get description => _description;
|
||||
|
||||
Reference in New Issue
Block a user