added willpopscope
This commit is contained in:
@@ -16,10 +16,18 @@ class CreateRecipe extends StatefulWidget {
|
|||||||
class _CreateRecipeState extends State<CreateRecipe> {
|
class _CreateRecipeState extends State<CreateRecipe> {
|
||||||
late RecipeProvider recipe;
|
late RecipeProvider recipe;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
super.dispose();
|
||||||
|
recipe.clearRecipe(silent: true);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
recipe = Provider.of<RecipeProvider>(context);
|
recipe = Provider.of<RecipeProvider>(context);
|
||||||
return Scaffold(
|
return WillPopScope(
|
||||||
|
onWillPop: _onWillPop,
|
||||||
|
child: Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('Create Recipe'),
|
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<IngredientListEntry> _ingredients = [];
|
||||||
final List<CookingStep> _steps = [];
|
final List<CookingStep> _steps = [];
|
||||||
|
|
||||||
void clearRecipe() {
|
void clearRecipe({silent = false}) {
|
||||||
_title = '';
|
_title = '';
|
||||||
_description = '';
|
_description = '';
|
||||||
_difficulty = null;
|
_difficulty = null;
|
||||||
_ingredients.clear();
|
_ingredients.clear();
|
||||||
_steps.clear();
|
_steps.clear();
|
||||||
|
if (!silent) {
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get isEmpty {
|
||||||
|
return _title.isEmpty &&
|
||||||
|
_description.isEmpty &&
|
||||||
|
_difficulty == null &&
|
||||||
|
_ingredients.isEmpty &&
|
||||||
|
steps.isEmpty;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get isNotEmpty {
|
||||||
|
return !isEmpty;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get description => _description;
|
String get description => _description;
|
||||||
|
|||||||
Reference in New Issue
Block a user