added willpopscope
This commit is contained in:
@@ -16,47 +16,56 @@ 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(
|
||||||
appBar: AppBar(
|
onWillPop: _onWillPop,
|
||||||
title: const Text('Create Recipe'),
|
child: Scaffold(
|
||||||
),
|
appBar: AppBar(
|
||||||
body: Form(
|
title: const Text('Create Recipe'),
|
||||||
child: Padding(
|
),
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 20),
|
body: Form(
|
||||||
child: Column(
|
child: Padding(
|
||||||
children: [
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
||||||
TextFormField(
|
child: Column(
|
||||||
onChanged: (value) => recipe.title = value,
|
children: [
|
||||||
decoration: const InputDecoration(
|
TextFormField(
|
||||||
label: Text('Title'),
|
onChanged: (value) => recipe.title = value,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
label: Text('Title'),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
TextFormField(
|
||||||
TextFormField(
|
onChanged: (value) => recipe.description = value,
|
||||||
onChanged: (value) => recipe.description = value,
|
decoration: const InputDecoration(
|
||||||
decoration: const InputDecoration(
|
label: Text('Description'),
|
||||||
label: Text('Description'),
|
),
|
||||||
),
|
),
|
||||||
),
|
DropdownMenu<Difficulty?>(
|
||||||
DropdownMenu<Difficulty?>(
|
dropdownMenuEntries: DifficultyUtil.getDropdownList(),
|
||||||
dropdownMenuEntries: DifficultyUtil.getDropdownList(),
|
onSelected: (value) => recipe.difficulty = value,
|
||||||
onSelected: (value) => recipe.difficulty = value,
|
label: const Text('Difficulty'),
|
||||||
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(),
|
|
||||||
),
|
),
|
||||||
)
|
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<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,13 +12,27 @@ 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();
|
||||||
notifyListeners();
|
if (!silent) {
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get isEmpty {
|
||||||
|
return _title.isEmpty &&
|
||||||
|
_description.isEmpty &&
|
||||||
|
_difficulty == null &&
|
||||||
|
_ingredients.isEmpty &&
|
||||||
|
steps.isEmpty;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get isNotEmpty {
|
||||||
|
return !isEmpty;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
Reference in New Issue
Block a user