changed the way recipeprovider works

This commit is contained in:
SomnusVeritas
2023-11-05 21:12:33 +01:00
parent b17078cdef
commit 6271825ccf
5 changed files with 44 additions and 31 deletions

View File

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

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:rezepte/pages/create_recipe_page.dart'; import 'package:rezepte/pages/create_recipe_page.dart';
import 'package:rezepte/widgets/recipe_list.dart';
class Dashboard extends StatelessWidget { class Dashboard extends StatelessWidget {
const Dashboard({super.key}); const Dashboard({super.key});
@@ -19,6 +20,7 @@ class Dashboard extends StatelessWidget {
onPressed: () => _onAddTapped(context), onPressed: () => _onAddTapped(context),
child: const Icon(Icons.add), child: const Icon(Icons.add),
), ),
body: const RecipeList(),
); );
} }
} }

View File

@@ -1,9 +1,10 @@
import 'package:flutter/material.dart'; import 'package:flutter/foundation.dart';
import '../../models/recipe.dart'; import '../../models/recipe.dart';
import 'package:rezepte/example_data.dart' as e;
class RecipeListProvider extends ChangeNotifier { class RecipeListProvider extends ChangeNotifier {
final List<Recipe> _recipes = []; final List<Recipe> _recipes = kDebugMode ? e.exampleRecipes : [];
set recipes(List<Recipe> recipes) { set recipes(List<Recipe> recipes) {
_recipes.clear(); _recipes.clear();

View File

@@ -5,13 +5,20 @@ import '../../models/ingredient_list_entry.dart';
import '../../models/recipe.dart'; import '../../models/recipe.dart';
class RecipeProvider extends ChangeNotifier implements Recipe { class RecipeProvider extends ChangeNotifier {
String _title = ''; String _title = '';
String _description = ''; String _description = '';
Difficulty? _difficulty; Difficulty? _difficulty;
final List<IngredientListEntry> _ingredients = []; final List<IngredientListEntry> _ingredients = [];
final List<CookingStep> _steps = []; final List<CookingStep> _steps = [];
Recipe get recipe => Recipe(
title: _title,
description: _description,
difficulty: _difficulty,
ingredients: _ingredients,
steps: _steps);
void clearRecipe({silent = false}) { void clearRecipe({silent = false}) {
_title = ''; _title = '';
_description = ''; _description = '';
@@ -35,7 +42,6 @@ class RecipeProvider extends ChangeNotifier implements Recipe {
return !isEmpty; return !isEmpty;
} }
@override
String get description => _description; String get description => _description;
set description(String description) { set description(String description) {
@@ -43,7 +49,6 @@ class RecipeProvider extends ChangeNotifier implements Recipe {
notifyListeners(); notifyListeners();
} }
@override
String get title => _title; String get title => _title;
set title(String title) { set title(String title) {
@@ -51,7 +56,6 @@ class RecipeProvider extends ChangeNotifier implements Recipe {
notifyListeners(); notifyListeners();
} }
@override
Difficulty? get difficulty => _difficulty; Difficulty? get difficulty => _difficulty;
set difficulty(Difficulty? difficulty) { set difficulty(Difficulty? difficulty) {
@@ -59,43 +63,35 @@ class RecipeProvider extends ChangeNotifier implements Recipe {
notifyListeners(); notifyListeners();
} }
@override
List<IngredientListEntry> get ingredients => _ingredients; List<IngredientListEntry> get ingredients => _ingredients;
@override
void addIngredient(IngredientListEntry ingredient) { void addIngredient(IngredientListEntry ingredient) {
_ingredients.add(ingredient); _ingredients.add(ingredient);
notifyListeners(); notifyListeners();
} }
@override
void clearIngredients({silent = false}) { void clearIngredients({silent = false}) {
ingredients.clear(); ingredients.clear();
if (!silent) notifyListeners(); if (!silent) notifyListeners();
} }
@override
void removeIngredientAt(int index, {silent = false}) { void removeIngredientAt(int index, {silent = false}) {
ingredients.removeAt(index); ingredients.removeAt(index);
if (!silent) notifyListeners(); if (!silent) notifyListeners();
} }
@override
void removeIngredient(IngredientListEntry ingredient, {silent = false}) { void removeIngredient(IngredientListEntry ingredient, {silent = false}) {
ingredients.removeWhere((element) => element == ingredient); ingredients.removeWhere((element) => element == ingredient);
if (!silent) notifyListeners(); if (!silent) notifyListeners();
} }
@override
List<CookingStep> get steps => _steps; List<CookingStep> get steps => _steps;
@override
void addStep(CookingStep step, {silent = false}) { void addStep(CookingStep step, {silent = false}) {
steps.add(step); steps.add(step);
if (!silent) notifyListeners(); if (!silent) notifyListeners();
} }
@override
void removeStepAt(int index, {silent = false}) { void removeStepAt(int index, {silent = false}) {
steps.removeAt(index); steps.removeAt(index);
if (!silent) notifyListeners(); if (!silent) notifyListeners();

View File

@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:rezepte/services/providers/recipe_list_provider.dart'; import 'package:rezepte/services/providers/recipe_list_provider.dart';
import '../models/recipe.dart';
class RecipeList extends StatelessWidget { class RecipeList extends StatelessWidget {
const RecipeList({super.key}); const RecipeList({super.key});
@@ -11,11 +13,16 @@ class RecipeList extends StatelessWidget {
Provider.of<RecipeListProvider>(context, listen: true).recipes; Provider.of<RecipeListProvider>(context, listen: true).recipes;
return ListView.builder( return ListView.builder(
itemCount: recipes.length, itemCount: recipes.length,
itemBuilder: _recipeListBuilder, itemBuilder: (context, index) =>
_recipeListBuilder(context, index, recipes.elementAt(index)),
); );
} }
Widget? _recipeListBuilder(BuildContext context, int index) { Widget? _recipeListBuilder(BuildContext context, int index, Recipe entry) {
return Card(); return Card(
child: ListTile(
title: Text(entry.title),
),
);
} }
} }