refactored recipe provider
This commit is contained in:
@@ -8,4 +8,10 @@ class IngredientListEntry {
|
||||
final bool optional;
|
||||
|
||||
IngredientListEntry(this.ingredient, this.amount, this.unit, this.optional);
|
||||
|
||||
@override
|
||||
operator ==(Object other) {
|
||||
final i = other as IngredientListEntry;
|
||||
return ingredient == i.ingredient;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import 'difficulty.dart';
|
||||
import 'ingredient.dart';
|
||||
import 'cooking_step.dart';
|
||||
import 'ingredient_list_entry.dart';
|
||||
|
||||
class Recipe {
|
||||
final String title;
|
||||
final String description;
|
||||
final Difficulty? difficulty;
|
||||
final List<Ingredient> ingredients = [];
|
||||
final List<IngredientListEntry> ingredients = [];
|
||||
final List<CookingStep> steps = [];
|
||||
|
||||
Recipe({
|
||||
|
||||
@@ -14,12 +14,11 @@ class CreateRecipe extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _CreateRecipeState extends State<CreateRecipe> {
|
||||
late RecipeProvider recipeProvider;
|
||||
final List<IngredientListEntry> ingredientEntries = [];
|
||||
late RecipeProvider recipe;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
recipeProvider = Provider.of<RecipeProvider>(context);
|
||||
recipe = Provider.of<RecipeProvider>(context);
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Create Recipe'),
|
||||
@@ -30,20 +29,20 @@ class _CreateRecipeState extends State<CreateRecipe> {
|
||||
child: Column(
|
||||
children: [
|
||||
TextFormField(
|
||||
onChanged: (value) => recipeProvider.title = value,
|
||||
onChanged: (value) => recipe.title = value,
|
||||
decoration: const InputDecoration(
|
||||
label: Text('Title'),
|
||||
),
|
||||
),
|
||||
TextFormField(
|
||||
onChanged: (value) => recipeProvider.description = value,
|
||||
onChanged: (value) => recipe.description = value,
|
||||
decoration: const InputDecoration(
|
||||
label: Text('Description'),
|
||||
),
|
||||
),
|
||||
DropdownMenu<Difficulty?>(
|
||||
dropdownMenuEntries: DifficultyUtil.getDropdownList(),
|
||||
onSelected: (value) => recipeProvider.difficulty = value,
|
||||
onSelected: (value) => recipe.difficulty = value,
|
||||
label: const Text('Difficulty'),
|
||||
),
|
||||
ElevatedButton(
|
||||
@@ -52,7 +51,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
|
||||
),
|
||||
Expanded(
|
||||
child: ListView.separated(
|
||||
itemCount: ingredientEntries.length,
|
||||
itemCount: recipe.ingredients.length,
|
||||
itemBuilder: _ingredientListBuilder,
|
||||
separatorBuilder: (context, index) => const Divider(),
|
||||
),
|
||||
@@ -74,11 +73,11 @@ class _CreateRecipeState extends State<CreateRecipe> {
|
||||
}
|
||||
|
||||
void _onIngredientSubmitted(IngredientListEntry ingredient) => setState(() {
|
||||
ingredientEntries.add(ingredient);
|
||||
recipe.ingredients.add(ingredient);
|
||||
});
|
||||
|
||||
Widget? _ingredientListBuilder(BuildContext context, int index) {
|
||||
final ingredient = ingredientEntries.elementAt(index);
|
||||
final ingredient = recipe.ingredients.elementAt(index);
|
||||
final textTheme = Theme.of(context).textTheme.titleMedium;
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 10),
|
||||
@@ -103,8 +102,4 @@ class _CreateRecipeState extends State<CreateRecipe> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _seperatorBuilder(BuildContext context, int index) {
|
||||
return const Divider(indent: 20, endIndent: 20);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rezepte/models/difficulty.dart';
|
||||
import 'package:rezepte/models/ingredient.dart';
|
||||
import 'package:rezepte/models/cooking_step.dart';
|
||||
import '../../models/difficulty.dart';
|
||||
import '../../models/cooking_step.dart';
|
||||
import '../../models/ingredient_list_entry.dart';
|
||||
|
||||
import '../../models/recipe.dart';
|
||||
|
||||
@@ -9,7 +9,7 @@ class RecipeProvider extends ChangeNotifier implements Recipe {
|
||||
String _title = '';
|
||||
String _description = '';
|
||||
Difficulty? _difficulty;
|
||||
final List<Ingredient> _ingredients = [];
|
||||
final List<IngredientListEntry> _ingredients = [];
|
||||
final List<CookingStep> _steps = [];
|
||||
|
||||
void clearRecipe() {
|
||||
@@ -46,9 +46,9 @@ class RecipeProvider extends ChangeNotifier implements Recipe {
|
||||
}
|
||||
|
||||
@override
|
||||
List<Ingredient> get ingredients => _ingredients;
|
||||
List<IngredientListEntry> get ingredients => _ingredients;
|
||||
|
||||
void addIngredient(Ingredient ingredient) {
|
||||
void addIngredient(IngredientListEntry ingredient) {
|
||||
_ingredients.add(ingredient);
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -63,7 +63,7 @@ class RecipeProvider extends ChangeNotifier implements Recipe {
|
||||
if (!silent) notifyListeners();
|
||||
}
|
||||
|
||||
void removeIngredient(Ingredient ingredient, {silent = false}) {
|
||||
void removeIngredient(IngredientListEntry ingredient, {silent = false}) {
|
||||
ingredients.removeWhere((element) => element == ingredient);
|
||||
if (!silent) notifyListeners();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user