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 '../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<CreateRecipe> {
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<RecipeProvider>(context);
recipeProvider = Provider.of<RecipeProvider>(context);
recipeListProvider =
Provider.of<RecipeListProvider>(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<CreateRecipe> {
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<CreateRecipe> {
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<CreateRecipe> {
),
DropdownMenu<Difficulty?>(
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<CreateRecipe> {
),
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<CreateRecipe> {
}
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<CreateRecipe> {
void _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: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(),
);
}
}

View File

@@ -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<Recipe> _recipes = [];
final List<Recipe> _recipes = kDebugMode ? e.exampleRecipes : [];
set recipes(List<Recipe> recipes) {
_recipes.clear();

View File

@@ -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<IngredientListEntry> _ingredients = [];
final List<CookingStep> _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<IngredientListEntry> 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<CookingStep> 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();

View File

@@ -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<RecipeListProvider>(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),
),
);
}
}