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