recipes can be added to db from createpage
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:rezepte/services/providers/db/dbhelper.dart';
|
||||||
import 'package:rezepte/widgets/ingredients_bottomsheet.dart';
|
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';
|
||||||
@@ -34,10 +35,17 @@ class _CreateRecipeState extends State<CreateRecipe> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
recipe = Provider.of<RecipeProvider>(context).recipe;
|
recipeProvider = Provider.of<RecipeProvider>(context, listen: true);
|
||||||
recipeProvider = Provider.of<RecipeProvider>(context, listen: false);
|
|
||||||
recipeListProvider =
|
recipeListProvider =
|
||||||
Provider.of<RecipeListProvider>(context, listen: false);
|
Provider.of<RecipeListProvider>(context, listen: false);
|
||||||
|
|
||||||
|
if (recipeProvider.recipe == null) {
|
||||||
|
recipe = Recipe(id: DbHelper.nextRecipeId, title: '');
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
||||||
|
recipeProvider.recipe = recipe;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return CustomWillPopScope(
|
return CustomWillPopScope(
|
||||||
context,
|
context,
|
||||||
ignore: recipe.isEmpty,
|
ignore: recipe.isEmpty,
|
||||||
|
|||||||
@@ -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_provider.dart';
|
import 'package:rezepte/services/providers/recipe_provider.dart';
|
||||||
|
|
||||||
|
import '../models/recipe.dart';
|
||||||
|
|
||||||
class RecipeDetail extends StatelessWidget {
|
class RecipeDetail extends StatelessWidget {
|
||||||
const RecipeDetail({super.key});
|
const RecipeDetail({super.key});
|
||||||
static const routeName = '/recipeDetail';
|
static const routeName = '/recipeDetail';
|
||||||
@@ -10,6 +12,9 @@ class RecipeDetail extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final recipe = Provider.of<RecipeProvider>(context, listen: false).recipe;
|
final recipe = Provider.of<RecipeProvider>(context, listen: false).recipe;
|
||||||
|
|
||||||
|
if (recipe == null) Navigator.of(context).pop();
|
||||||
|
recipe as Recipe;
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(),
|
appBar: AppBar(),
|
||||||
body: Center(
|
body: Center(
|
||||||
|
|||||||
@@ -30,4 +30,6 @@ class DbHelper {
|
|||||||
static bool deleteRecipe(Recipe recipe) {
|
static bool deleteRecipe(Recipe recipe) {
|
||||||
return _isar.write((isar) => isar.recipes.delete(recipe.id));
|
return _isar.write((isar) => isar.recipes.delete(recipe.id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Stream<void> get recipesChangedStream => _isar.recipes.watchLazy();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ import 'package:rezepte/services/providers/db/dbhelper.dart';
|
|||||||
import '../../models/recipe.dart';
|
import '../../models/recipe.dart';
|
||||||
|
|
||||||
class RecipeListProvider extends ChangeNotifier {
|
class RecipeListProvider extends ChangeNotifier {
|
||||||
|
RecipeListProvider() {
|
||||||
|
DbHelper.recipesChangedStream.listen((event) {
|
||||||
|
notifyListeners();
|
||||||
|
});
|
||||||
|
}
|
||||||
// final List<Recipe> _recipes = [];
|
// final List<Recipe> _recipes = [];
|
||||||
|
|
||||||
// set recipes(List<Recipe> recipes) {
|
// set recipes(List<Recipe> recipes) {
|
||||||
@@ -20,8 +25,7 @@ class RecipeListProvider extends ChangeNotifier {
|
|||||||
// if (!silent) notifyListeners();
|
// if (!silent) notifyListeners();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
void addRecipe(Recipe recipe, {silent = false}) {
|
void addRecipe(Recipe recipe) {
|
||||||
DbHelper.putRecipe(recipe);
|
DbHelper.putRecipe(recipe);
|
||||||
if (!silent) notifyListeners();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import '../../models/recipe.dart';
|
|||||||
class RecipeProvider extends ChangeNotifier {
|
class RecipeProvider extends ChangeNotifier {
|
||||||
Recipe? _recipe;
|
Recipe? _recipe;
|
||||||
|
|
||||||
Recipe get recipe => _recipe ??= Recipe(id: 0, title: '');
|
Recipe? get recipe => _recipe;
|
||||||
|
|
||||||
set recipe(Recipe recipe) {
|
set recipe(Recipe? recipe) {
|
||||||
_recipe = recipe;
|
_recipe = recipe;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user