From 0f1222040fa749da948e0e068338e44158917fdb Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Wed, 1 Nov 2023 14:53:42 +0100 Subject: [PATCH] added willpopscope --- lib/pages/create_recipe_page.dart | 104 +++++++++++++------- lib/services/providers/recipe_provider.dart | 18 +++- 2 files changed, 85 insertions(+), 37 deletions(-) diff --git a/lib/pages/create_recipe_page.dart b/lib/pages/create_recipe_page.dart index 49857ee..3b59276 100644 --- a/lib/pages/create_recipe_page.dart +++ b/lib/pages/create_recipe_page.dart @@ -16,47 +16,56 @@ class CreateRecipe extends StatefulWidget { class _CreateRecipeState extends State { late RecipeProvider recipe; + @override + void dispose() { + super.dispose(); + recipe.clearRecipe(silent: true); + } + @override Widget build(BuildContext context) { recipe = Provider.of(context); - return Scaffold( - appBar: AppBar( - title: const Text('Create Recipe'), - ), - body: Form( - child: Padding( - padding: const EdgeInsets.symmetric(horizontal: 20), - child: Column( - children: [ - TextFormField( - onChanged: (value) => recipe.title = value, - decoration: const InputDecoration( - label: Text('Title'), + return WillPopScope( + onWillPop: _onWillPop, + child: Scaffold( + appBar: AppBar( + title: const Text('Create Recipe'), + ), + body: Form( + child: Padding( + padding: const EdgeInsets.symmetric(horizontal: 20), + child: Column( + children: [ + TextFormField( + onChanged: (value) => recipe.title = value, + decoration: const InputDecoration( + label: Text('Title'), + ), ), - ), - TextFormField( - onChanged: (value) => recipe.description = value, - decoration: const InputDecoration( - label: Text('Description'), + TextFormField( + onChanged: (value) => recipe.description = value, + decoration: const InputDecoration( + label: Text('Description'), + ), ), - ), - DropdownMenu( - dropdownMenuEntries: DifficultyUtil.getDropdownList(), - onSelected: (value) => recipe.difficulty = value, - label: const Text('Difficulty'), - ), - ElevatedButton( - onPressed: _openIngredientBottomSheet, - child: const Text('Add Ingredient'), - ), - Expanded( - child: ListView.separated( - itemCount: recipe.ingredients.length, - itemBuilder: _ingredientListBuilder, - separatorBuilder: (context, index) => const Divider(), + DropdownMenu( + dropdownMenuEntries: DifficultyUtil.getDropdownList(), + onSelected: (value) => recipe.difficulty = value, + label: const Text('Difficulty'), ), - ) - ], + ElevatedButton( + onPressed: _openIngredientBottomSheet, + child: const Text('Add Ingredient'), + ), + Expanded( + child: ListView.separated( + itemCount: recipe.ingredients.length, + itemBuilder: _ingredientListBuilder, + separatorBuilder: (context, index) => const Divider(), + ), + ) + ], + ), ), ), ), @@ -102,4 +111,29 @@ class _CreateRecipeState extends State { ), ); } + + Future _onWillPop() { + if (recipe.isEmpty) { + return Future( + () { + return true; + }, + ); + } + return showDialog( + 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); + } } diff --git a/lib/services/providers/recipe_provider.dart b/lib/services/providers/recipe_provider.dart index 3bc9f7d..972758d 100644 --- a/lib/services/providers/recipe_provider.dart +++ b/lib/services/providers/recipe_provider.dart @@ -12,13 +12,27 @@ class RecipeProvider extends ChangeNotifier implements Recipe { final List _ingredients = []; final List _steps = []; - void clearRecipe() { + void clearRecipe({silent = false}) { _title = ''; _description = ''; _difficulty = null; _ingredients.clear(); _steps.clear(); - notifyListeners(); + if (!silent) { + notifyListeners(); + } + } + + bool get isEmpty { + return _title.isEmpty && + _description.isEmpty && + _difficulty == null && + _ingredients.isEmpty && + steps.isEmpty; + } + + bool get isNotEmpty { + return !isEmpty; } @override