ability to add recipes to dashboard and navigate to detail view

This commit is contained in:
SomnusVeritas
2023-11-05 22:07:48 +01:00
parent 6271825ccf
commit a53d1363bf
6 changed files with 134 additions and 102 deletions

View File

@@ -5,6 +5,7 @@ import 'package:rezepte/pages/dashboard_page.dart';
import 'package:rezepte/services/providers/recipe_list_provider.dart'; import 'package:rezepte/services/providers/recipe_list_provider.dart';
import 'package:rezepte/services/providers/recipe_provider.dart'; import 'package:rezepte/services/providers/recipe_provider.dart';
import 'pages/recipe_detail_page.dart';
import 'theme.dart'; import 'theme.dart';
void main() { void main() {
@@ -30,6 +31,7 @@ class MyApp extends StatelessWidget {
routes: { routes: {
Dashboard.routeName: (_) => const Dashboard(), Dashboard.routeName: (_) => const Dashboard(),
CreateRecipe.routeName: (_) => const CreateRecipe(), CreateRecipe.routeName: (_) => const CreateRecipe(),
RecipeDetail.routeName: (_) => const RecipeDetail(),
}, },
); );
} }

View File

@@ -3,18 +3,16 @@ import 'cooking_step.dart';
import 'ingredient_list_entry.dart'; import 'ingredient_list_entry.dart';
class Recipe { class Recipe {
final String title; String title;
final String description; String description;
final Difficulty? difficulty; Difficulty? difficulty;
final List<IngredientListEntry> ingredients; final List<IngredientListEntry> ingredients = [];
final List<CookingStep> steps; final List<CookingStep> steps = [];
Recipe({ Recipe({
required this.title, required this.title,
this.description = '', this.description = '',
this.difficulty, this.difficulty,
this.ingredients = const [],
this.steps = const [],
}); });
void addIngredient(IngredientListEntry ingredient) => void addIngredient(IngredientListEntry ingredient) =>
@@ -33,4 +31,24 @@ class Recipe {
void addStep(CookingStep step) => steps.add(step); void addStep(CookingStep step) => steps.add(step);
void removeStepAt(int index) => steps.removeAt(index); void removeStepAt(int index) => steps.removeAt(index);
bool get isEmpty {
return title.isEmpty &&
description.isEmpty &&
difficulty == null &&
ingredients.isEmpty &&
steps.isEmpty;
}
bool get isNotEmpty {
return !isEmpty;
}
void clear() {
title = '';
description = '';
difficulty = null;
ingredients.clear();
steps.clear();
}
} }

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 '../models/recipe.dart';
import '../services/providers/recipe_list_provider.dart'; import '../services/providers/recipe_list_provider.dart';
import '../services/providers/recipe_provider.dart'; import '../services/providers/recipe_provider.dart';
@@ -16,28 +17,35 @@ class CreateRecipe extends StatefulWidget {
} }
class _CreateRecipeState extends State<CreateRecipe> { class _CreateRecipeState extends State<CreateRecipe> {
late RecipeProvider recipeProvider;
late RecipeListProvider recipeListProvider; late RecipeListProvider recipeListProvider;
late RecipeProvider recipeProvider;
late Recipe recipe;
@override
void initState() {
super.initState();
}
@override @override
void dispose() { void dispose() {
recipeProvider.disposeRecipe();
super.dispose(); super.dispose();
recipeProvider.clearRecipe(silent: true);
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
recipeProvider = Provider.of<RecipeProvider>(context); recipe = Provider.of<RecipeProvider>(context).recipe;
recipeProvider = Provider.of<RecipeProvider>(context, listen: false);
recipeListProvider = recipeListProvider =
Provider.of<RecipeListProvider>(context, listen: false); Provider.of<RecipeListProvider>(context, listen: false);
return CustomWillPopScope( return CustomWillPopScope(
context, context,
ignore: recipeProvider.isEmpty, ignore: recipe.isEmpty,
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Create Recipe'), title: const Text('Create Recipe'),
), ),
floatingActionButton: recipeProvider.isNotEmpty floatingActionButton: recipe.isNotEmpty
? FloatingActionButton( ? FloatingActionButton(
onPressed: _onRecipeSubmitted, onPressed: _onRecipeSubmitted,
child: const Icon(Icons.save), child: const Icon(Icons.save),
@@ -50,7 +58,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
children: [ children: [
TextFormField( TextFormField(
onTapOutside: (event) => FocusScope.of(context).unfocus(), onTapOutside: (event) => FocusScope.of(context).unfocus(),
onChanged: (value) => recipeProvider.title = value, onChanged: (value) => recipe.title = value,
decoration: const InputDecoration( decoration: const InputDecoration(
label: Text('Title'), label: Text('Title'),
), ),
@@ -61,7 +69,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) => recipeProvider.description = value, onChanged: (value) => recipe.description = value,
decoration: const InputDecoration( decoration: const InputDecoration(
label: Text('Description'), label: Text('Description'),
), ),
@@ -70,7 +78,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
), ),
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'),
textStyle: TextStyle( textStyle: TextStyle(
color: Theme.of(context).colorScheme.onBackground), color: Theme.of(context).colorScheme.onBackground),
@@ -81,7 +89,7 @@ class _CreateRecipeState extends State<CreateRecipe> {
), ),
Expanded( Expanded(
child: ListView.separated( child: ListView.separated(
itemCount: recipeProvider.ingredients.length, itemCount: recipe.ingredients.length,
itemBuilder: _ingredientListBuilder, itemBuilder: _ingredientListBuilder,
separatorBuilder: (context, index) => const Divider(), separatorBuilder: (context, index) => const Divider(),
), ),
@@ -104,27 +112,27 @@ class _CreateRecipeState extends State<CreateRecipe> {
} }
void _onIngredientSubmitted(IngredientListEntry ingredient) => setState(() { void _onIngredientSubmitted(IngredientListEntry ingredient) => setState(() {
recipeProvider.ingredients.add(ingredient); recipe.ingredients.add(ingredient);
}); });
void _onIngredientRemoveTapped(int index) { void _onIngredientRemoveTapped(int index) {
final removedIngredient = recipeProvider.ingredients.elementAt(index); final removedIngredient = recipe.ingredients.elementAt(index);
recipeProvider.removeIngredientAt(index); recipe.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: () {
recipeProvider.addIngredient(removedIngredient); recipe.addIngredient(removedIngredient);
}), }),
), ),
); );
} }
Widget? _ingredientListBuilder(BuildContext context, int index) { Widget? _ingredientListBuilder(BuildContext context, int index) {
final ingredient = recipeProvider.ingredients.elementAt(index); final ingredient = recipe.ingredients.elementAt(index);
return ListTile( return ListTile(
contentPadding: EdgeInsets.zero, contentPadding: EdgeInsets.zero,
@@ -165,8 +173,8 @@ class _CreateRecipeState extends State<CreateRecipe> {
void _onRecipeSubmitted() { void _onRecipeSubmitted() {
// TODO implement onRecipeSubmitted // TODO implement onRecipeSubmitted
if (recipeProvider.isEmpty) return; if (recipe.isEmpty) return;
recipeListProvider.addRecipe(recipeProvider.recipe); recipeListProvider.addRecipe(recipe);
Navigator.of(context).pop(); Navigator.of(context).pop();
} }
} }

View File

@@ -0,0 +1,20 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:rezepte/services/providers/recipe_provider.dart';
class RecipeDetail extends StatelessWidget {
const RecipeDetail({super.key});
static const routeName = '/recipeDetail';
@override
Widget build(BuildContext context) {
final recipe = Provider.of<RecipeProvider>(context, listen: false).recipe;
return Scaffold(
appBar: AppBar(),
body: Center(
child: Text(recipe.title),
),
);
}
}

View File

@@ -1,99 +1,71 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../../models/difficulty.dart';
import '../../models/cooking_step.dart';
import '../../models/ingredient_list_entry.dart';
import '../../models/recipe.dart'; import '../../models/recipe.dart';
class RecipeProvider extends ChangeNotifier { class RecipeProvider extends ChangeNotifier {
String _title = ''; Recipe? _recipe;
String _description = '';
Difficulty? _difficulty;
final List<IngredientListEntry> _ingredients = [];
final List<CookingStep> _steps = [];
Recipe get recipe => Recipe( Recipe get recipe => _recipe ??= Recipe(title: '');
title: _title,
description: _description,
difficulty: _difficulty,
ingredients: _ingredients,
steps: _steps);
void clearRecipe({silent = false}) { set recipe(Recipe recipe) {
_title = ''; _recipe = recipe;
_description = '';
_difficulty = null;
_ingredients.clear();
_steps.clear();
if (!silent) {
notifyListeners();
}
}
bool get isEmpty {
return _title.isEmpty &&
_description.isEmpty &&
_difficulty == null &&
_ingredients.isEmpty &&
steps.isEmpty;
}
bool get isNotEmpty {
return !isEmpty;
}
String get description => _description;
set description(String description) {
_description = description;
notifyListeners(); notifyListeners();
} }
String get title => _title; void disposeRecipe() {
_recipe = null;
set title(String title) {
_title = title;
notifyListeners();
} }
Difficulty? get difficulty => _difficulty; // set description(String description) {
// _description = description;
// notifyListeners();
// }
set difficulty(Difficulty? difficulty) { // String get title => _title;
_difficulty = difficulty;
notifyListeners();
}
List<IngredientListEntry> get ingredients => _ingredients; // set title(String title) {
// _title = title;
// notifyListeners();
// }
void addIngredient(IngredientListEntry ingredient) { // Difficulty? get difficulty => _difficulty;
_ingredients.add(ingredient);
notifyListeners();
}
void clearIngredients({silent = false}) { // set difficulty(Difficulty? difficulty) {
ingredients.clear(); // _difficulty = difficulty;
if (!silent) notifyListeners(); // notifyListeners();
} // }
void removeIngredientAt(int index, {silent = false}) { // List<IngredientListEntry> get ingredients => _ingredients;
ingredients.removeAt(index);
if (!silent) notifyListeners();
}
void removeIngredient(IngredientListEntry ingredient, {silent = false}) { // void addIngredient(IngredientListEntry ingredient) {
ingredients.removeWhere((element) => element == ingredient); // _ingredients.add(ingredient);
if (!silent) notifyListeners(); // notifyListeners();
} // }
List<CookingStep> get steps => _steps; // void clearIngredients({silent = false}) {
// ingredients.clear();
// if (!silent) notifyListeners();
// }
void addStep(CookingStep step, {silent = false}) { // void removeIngredientAt(int index, {silent = false}) {
steps.add(step); // ingredients.removeAt(index);
if (!silent) notifyListeners(); // if (!silent) notifyListeners();
} // }
void removeStepAt(int index, {silent = false}) { // void removeIngredient(IngredientListEntry ingredient, {silent = false}) {
steps.removeAt(index); // ingredients.removeWhere((element) => element == ingredient);
if (!silent) notifyListeners(); // if (!silent) notifyListeners();
} // }
// List<CookingStep> get steps => _steps;
// void addStep(CookingStep step, {silent = false}) {
// steps.add(step);
// if (!silent) notifyListeners();
// }
// void removeStepAt(int index, {silent = false}) {
// steps.removeAt(index);
// if (!silent) notifyListeners();
// }
} }

View File

@@ -1,6 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:rezepte/pages/recipe_detail_page.dart';
import 'package:rezepte/services/providers/recipe_list_provider.dart'; import 'package:rezepte/services/providers/recipe_list_provider.dart';
import 'package:rezepte/services/providers/recipe_provider.dart';
import '../models/recipe.dart'; import '../models/recipe.dart';
@@ -11,6 +13,7 @@ class RecipeList extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final recipes = final recipes =
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: (context, index) => itemBuilder: (context, index) =>
@@ -20,9 +23,18 @@ class RecipeList extends StatelessWidget {
Widget? _recipeListBuilder(BuildContext context, int index, Recipe entry) { Widget? _recipeListBuilder(BuildContext context, int index, Recipe entry) {
return Card( return Card(
child: InkWell(
borderRadius: BorderRadius.circular(12),
onTap: () => _onRecipeTapped(context, entry),
child: ListTile( child: ListTile(
title: Text(entry.title), title: Text(entry.title),
), ),
),
); );
} }
void _onRecipeTapped(BuildContext context, Recipe recipe) {
Provider.of<RecipeProvider>(context, listen: false).recipe = recipe;
Navigator.of(context).pushNamed(RecipeDetail.routeName);
}
} }