From 6271825ccfff67d99dfb3a82af00174856befe08 Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Sun, 5 Nov 2023 21:12:33 +0100 Subject: [PATCH] changed the way recipeprovider works --- lib/pages/create_recipe_page.dart | 35 +++++++++++-------- lib/pages/dashboard_page.dart | 2 ++ .../providers/recipe_list_provider.dart | 5 +-- lib/services/providers/recipe_provider.dart | 20 +++++------ lib/widgets/recipe_list.dart | 13 +++++-- 5 files changed, 44 insertions(+), 31 deletions(-) diff --git a/lib/pages/create_recipe_page.dart b/lib/pages/create_recipe_page.dart index 4873995..05c1edb 100644 --- a/lib/pages/create_recipe_page.dart +++ b/lib/pages/create_recipe_page.dart @@ -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 '../services/providers/recipe_list_provider.dart'; import '../services/providers/recipe_provider.dart'; class CreateRecipe extends StatefulWidget { @@ -15,25 +16,28 @@ class CreateRecipe extends StatefulWidget { } class _CreateRecipeState extends State { - late RecipeProvider recipe; + late RecipeProvider recipeProvider; + late RecipeListProvider recipeListProvider; @override void dispose() { super.dispose(); - recipe.clearRecipe(silent: true); + recipeProvider.clearRecipe(silent: true); } @override Widget build(BuildContext context) { - recipe = Provider.of(context); + recipeProvider = Provider.of(context); + recipeListProvider = + Provider.of(context, listen: false); return CustomWillPopScope( context, - ignore: recipe.isEmpty, + ignore: recipeProvider.isEmpty, child: Scaffold( appBar: AppBar( title: const Text('Create Recipe'), ), - floatingActionButton: recipe.isNotEmpty + floatingActionButton: recipeProvider.isNotEmpty ? FloatingActionButton( onPressed: _onRecipeSubmitted, child: const Icon(Icons.save), @@ -46,7 +50,7 @@ class _CreateRecipeState extends State { children: [ TextFormField( onTapOutside: (event) => FocusScope.of(context).unfocus(), - onChanged: (value) => recipe.title = value, + onChanged: (value) => recipeProvider.title = value, decoration: const InputDecoration( label: Text('Title'), ), @@ -57,7 +61,7 @@ class _CreateRecipeState extends State { onTapOutside: (event) => FocusScope.of(context).unfocus(), minLines: 1, maxLines: 4, - onChanged: (value) => recipe.description = value, + onChanged: (value) => recipeProvider.description = value, decoration: const InputDecoration( label: Text('Description'), ), @@ -66,7 +70,7 @@ class _CreateRecipeState extends State { ), DropdownMenu( dropdownMenuEntries: DifficultyUtil.getDropdownList(), - onSelected: (value) => recipe.difficulty = value, + onSelected: (value) => recipeProvider.difficulty = value, label: const Text('Difficulty'), textStyle: TextStyle( color: Theme.of(context).colorScheme.onBackground), @@ -77,7 +81,7 @@ class _CreateRecipeState extends State { ), Expanded( child: ListView.separated( - itemCount: recipe.ingredients.length, + itemCount: recipeProvider.ingredients.length, itemBuilder: _ingredientListBuilder, separatorBuilder: (context, index) => const Divider(), ), @@ -100,27 +104,27 @@ class _CreateRecipeState extends State { } void _onIngredientSubmitted(IngredientListEntry ingredient) => setState(() { - recipe.ingredients.add(ingredient); + recipeProvider.ingredients.add(ingredient); }); void _onIngredientRemoveTapped(int index) { - final removedIngredient = recipe.ingredients.elementAt(index); + final removedIngredient = recipeProvider.ingredients.elementAt(index); - recipe.removeIngredientAt(index); + recipeProvider.removeIngredientAt(index); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: const Text('Ingredient Removed'), action: SnackBarAction( label: 'Undo', onPressed: () { - recipe.addIngredient(removedIngredient); + recipeProvider.addIngredient(removedIngredient); }), ), ); } Widget? _ingredientListBuilder(BuildContext context, int index) { - final ingredient = recipe.ingredients.elementAt(index); + final ingredient = recipeProvider.ingredients.elementAt(index); return ListTile( contentPadding: EdgeInsets.zero, @@ -161,5 +165,8 @@ class _CreateRecipeState extends State { void _onRecipeSubmitted() { // TODO implement onRecipeSubmitted + if (recipeProvider.isEmpty) return; + recipeListProvider.addRecipe(recipeProvider.recipe); + Navigator.of(context).pop(); } } diff --git a/lib/pages/dashboard_page.dart b/lib/pages/dashboard_page.dart index 63679b9..5348354 100644 --- a/lib/pages/dashboard_page.dart +++ b/lib/pages/dashboard_page.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:rezepte/pages/create_recipe_page.dart'; +import 'package:rezepte/widgets/recipe_list.dart'; class Dashboard extends StatelessWidget { const Dashboard({super.key}); @@ -19,6 +20,7 @@ class Dashboard extends StatelessWidget { onPressed: () => _onAddTapped(context), child: const Icon(Icons.add), ), + body: const RecipeList(), ); } } diff --git a/lib/services/providers/recipe_list_provider.dart b/lib/services/providers/recipe_list_provider.dart index f007a97..b400d12 100644 --- a/lib/services/providers/recipe_list_provider.dart +++ b/lib/services/providers/recipe_list_provider.dart @@ -1,9 +1,10 @@ -import 'package:flutter/material.dart'; +import 'package:flutter/foundation.dart'; import '../../models/recipe.dart'; +import 'package:rezepte/example_data.dart' as e; class RecipeListProvider extends ChangeNotifier { - final List _recipes = []; + final List _recipes = kDebugMode ? e.exampleRecipes : []; set recipes(List recipes) { _recipes.clear(); diff --git a/lib/services/providers/recipe_provider.dart b/lib/services/providers/recipe_provider.dart index 97375de..4cfb5f4 100644 --- a/lib/services/providers/recipe_provider.dart +++ b/lib/services/providers/recipe_provider.dart @@ -5,13 +5,20 @@ import '../../models/ingredient_list_entry.dart'; import '../../models/recipe.dart'; -class RecipeProvider extends ChangeNotifier implements Recipe { +class RecipeProvider extends ChangeNotifier { String _title = ''; String _description = ''; Difficulty? _difficulty; final List _ingredients = []; final List _steps = []; + Recipe get recipe => Recipe( + title: _title, + description: _description, + difficulty: _difficulty, + ingredients: _ingredients, + steps: _steps); + void clearRecipe({silent = false}) { _title = ''; _description = ''; @@ -35,7 +42,6 @@ class RecipeProvider extends ChangeNotifier implements Recipe { return !isEmpty; } - @override String get description => _description; set description(String description) { @@ -43,7 +49,6 @@ class RecipeProvider extends ChangeNotifier implements Recipe { notifyListeners(); } - @override String get title => _title; set title(String title) { @@ -51,7 +56,6 @@ class RecipeProvider extends ChangeNotifier implements Recipe { notifyListeners(); } - @override Difficulty? get difficulty => _difficulty; set difficulty(Difficulty? difficulty) { @@ -59,43 +63,35 @@ class RecipeProvider extends ChangeNotifier implements Recipe { notifyListeners(); } - @override List get ingredients => _ingredients; - @override void addIngredient(IngredientListEntry ingredient) { _ingredients.add(ingredient); notifyListeners(); } - @override void clearIngredients({silent = false}) { ingredients.clear(); if (!silent) notifyListeners(); } - @override void removeIngredientAt(int index, {silent = false}) { ingredients.removeAt(index); if (!silent) notifyListeners(); } - @override void removeIngredient(IngredientListEntry ingredient, {silent = false}) { ingredients.removeWhere((element) => element == ingredient); if (!silent) notifyListeners(); } - @override List get steps => _steps; - @override void addStep(CookingStep step, {silent = false}) { steps.add(step); if (!silent) notifyListeners(); } - @override void removeStepAt(int index, {silent = false}) { steps.removeAt(index); if (!silent) notifyListeners(); diff --git a/lib/widgets/recipe_list.dart b/lib/widgets/recipe_list.dart index a58f00e..08a0b0c 100644 --- a/lib/widgets/recipe_list.dart +++ b/lib/widgets/recipe_list.dart @@ -2,6 +2,8 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:rezepte/services/providers/recipe_list_provider.dart'; +import '../models/recipe.dart'; + class RecipeList extends StatelessWidget { const RecipeList({super.key}); @@ -11,11 +13,16 @@ class RecipeList extends StatelessWidget { Provider.of(context, listen: true).recipes; return ListView.builder( itemCount: recipes.length, - itemBuilder: _recipeListBuilder, + itemBuilder: (context, index) => + _recipeListBuilder(context, index, recipes.elementAt(index)), ); } - Widget? _recipeListBuilder(BuildContext context, int index) { - return Card(); + Widget? _recipeListBuilder(BuildContext context, int index, Recipe entry) { + return Card( + child: ListTile( + title: Text(entry.title), + ), + ); } }