ability to add recipes to dashboard and navigate to detail view

This commit is contained in:
SomnusVeritas
2023-11-05 22:07:48 +01:00
parent 6271825ccf
commit a53d1363bf
6 changed files with 134 additions and 102 deletions

View File

@@ -4,6 +4,7 @@ import 'package:rezepte/widgets/ingredients_bottomsheet.dart';
import 'package:rezepte/widgets/will_pop_scope.dart';
import '../models/difficulty.dart';
import '../models/ingredient_list_entry.dart';
import '../models/recipe.dart';
import '../services/providers/recipe_list_provider.dart';
import '../services/providers/recipe_provider.dart';
@@ -16,28 +17,35 @@ class CreateRecipe extends StatefulWidget {
}
class _CreateRecipeState extends State<CreateRecipe> {
late RecipeProvider recipeProvider;
late RecipeListProvider recipeListProvider;
late RecipeProvider recipeProvider;
late Recipe recipe;
@override
void initState() {
super.initState();
}
@override
void dispose() {
recipeProvider.disposeRecipe();
super.dispose();
recipeProvider.clearRecipe(silent: true);
}
@override
Widget build(BuildContext context) {
recipeProvider = Provider.of<RecipeProvider>(context);
recipe = Provider.of<RecipeProvider>(context).recipe;
recipeProvider = Provider.of<RecipeProvider>(context, listen: false);
recipeListProvider =
Provider.of<RecipeListProvider>(context, listen: false);
return CustomWillPopScope(
context,
ignore: recipeProvider.isEmpty,
ignore: recipe.isEmpty,
child: Scaffold(
appBar: AppBar(
title: const Text('Create Recipe'),
),
floatingActionButton: recipeProvider.isNotEmpty
floatingActionButton: recipe.isNotEmpty
? FloatingActionButton(
onPressed: _onRecipeSubmitted,
child: const Icon(Icons.save),
@@ -50,7 +58,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
children: [
TextFormField(
onTapOutside: (event) => FocusScope.of(context).unfocus(),
onChanged: (value) => recipeProvider.title = value,
onChanged: (value) => recipe.title = value,
decoration: const InputDecoration(
label: Text('Title'),
),
@@ -61,7 +69,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
onTapOutside: (event) => FocusScope.of(context).unfocus(),
minLines: 1,
maxLines: 4,
onChanged: (value) => recipeProvider.description = value,
onChanged: (value) => recipe.description = value,
decoration: const InputDecoration(
label: Text('Description'),
),
@@ -70,7 +78,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
),
DropdownMenu<Difficulty?>(
dropdownMenuEntries: DifficultyUtil.getDropdownList(),
onSelected: (value) => recipeProvider.difficulty = value,
onSelected: (value) => recipe.difficulty = value,
label: const Text('Difficulty'),
textStyle: TextStyle(
color: Theme.of(context).colorScheme.onBackground),
@@ -81,7 +89,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
),
Expanded(
child: ListView.separated(
itemCount: recipeProvider.ingredients.length,
itemCount: recipe.ingredients.length,
itemBuilder: _ingredientListBuilder,
separatorBuilder: (context, index) => const Divider(),
),
@@ -104,27 +112,27 @@ class _CreateRecipeState extends State<CreateRecipe> {
}
void _onIngredientSubmitted(IngredientListEntry ingredient) => setState(() {
recipeProvider.ingredients.add(ingredient);
recipe.ingredients.add(ingredient);
});
void _onIngredientRemoveTapped(int index) {
final removedIngredient = recipeProvider.ingredients.elementAt(index);
final removedIngredient = recipe.ingredients.elementAt(index);
recipeProvider.removeIngredientAt(index);
recipe.removeIngredientAt(index);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('Ingredient Removed'),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
recipeProvider.addIngredient(removedIngredient);
recipe.addIngredient(removedIngredient);
}),
),
);
}
Widget? _ingredientListBuilder(BuildContext context, int index) {
final ingredient = recipeProvider.ingredients.elementAt(index);
final ingredient = recipe.ingredients.elementAt(index);
return ListTile(
contentPadding: EdgeInsets.zero,
@@ -165,8 +173,8 @@ class _CreateRecipeState extends State<CreateRecipe> {
void _onRecipeSubmitted() {
// TODO implement onRecipeSubmitted
if (recipeProvider.isEmpty) return;
recipeListProvider.addRecipe(recipeProvider.recipe);
if (recipe.isEmpty) return;
recipeListProvider.addRecipe(recipe);
Navigator.of(context).pop();
}
}