Skip to content

Commit

Permalink
recipe selector
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed Apr 28, 2024
1 parent e1b79f5 commit e95cbdb
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 31 deletions.
2 changes: 2 additions & 0 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class _HomePageState extends State<HomePage> {
void initState() {
loadRecipes();
loadTags();

super.initState();
}

Expand Down Expand Up @@ -119,6 +120,7 @@ class _HomePageState extends State<HomePage> {
Future<void> onTagsSelectionUpdate(List<int> values) async {
setState(() {
selectedTagsId = values;
print(selectedTagsId);
});
}

Expand Down
71 changes: 66 additions & 5 deletions lib/screens/recipe_editor.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/screens/pages_layout.dart';
import 'package:reciper/utilities/database.dart';
import 'package:reciper/models/recipe.dart';
import 'package:reciper/screens/home.dart';
import 'package:reciper/widgets/extract_recipe_button.dart';
import 'package:reciper/widgets/recipeTagSelector.dart';

class RecipeEditorPage extends StatefulWidget {
// final String initialTitle;
Expand All @@ -28,6 +31,20 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
String servings = "";
String source = "";

List<Tag> tags = [];
List<int> selectedTagsId = [];

@override
void initState() {
loadTags();
if (widget.initialRecipe != null) {
DatabaseService.getTagsFromRecipe(widget.initialRecipe!.id!).then((tags) {
selectedTagsId = tags.map((e) => e.id!).toList();
});
}
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -44,12 +61,25 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {

if (!widget.isUpdate) {
DatabaseService.createRecipe(Recipe(
title: title,
servings: servings,
steps: steps,
ingredients: ingredients,
source: source));
title: title,
servings: servings,
steps: steps,
ingredients: ingredients,
source: source))
.then((recipeId) {
for (var tag in tags) {
DatabaseService.createTagLink(tag.id!, recipeId);
}
});
} else {
DatabaseService.removeTagLink(
recipeId: widget.initialRecipe!.id)
.then((value) {
for (var tag in tags) {
DatabaseService.createTagLink(
tag.id!, widget.initialRecipe!.id!);
}
});
DatabaseService.updateRecipe(Recipe(
id: widget.initialRecipe!.id,
servings: servings,
Expand Down Expand Up @@ -94,6 +124,22 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
),
),
SizedBox(height: fieldsMargin),
Container(
height: 50,
child: Row(
children: [
const Text("Tags : "),
const SizedBox(
width: 20,
),
RecipeTagSelector(
tags: tags,
onTagsUpdate: loadTags,
selectedTagsId: selectedTagsId,
onTagsSelectionUpdate: onTagsSelectionUpdate)
],
)),
SizedBox(height: fieldsMargin),
TextFormField(
initialValue: widget.initialRecipe?.servings,
onSaved: (value) {
Expand Down Expand Up @@ -153,4 +199,19 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
),
)));
}

Future<void> loadTags() async {
DatabaseService.getTags().then((List<Tag> result) {
setState(() {
tags = result;
});
});
}

Future<void> onTagsSelectionUpdate(List<int> values) async {
setState(() {
selectedTagsId = values;
print(selectedTagsId);
});
}
}
5 changes: 2 additions & 3 deletions lib/utilities/database.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:convert';

import 'package:reciper/models/tagLink.dart';
import 'package:reciper/models/tag.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
Expand Down Expand Up @@ -60,7 +59,7 @@ class DatabaseService {
db.execute("""
CREATE TABLE TagsLinks(
recipeId INTEGER,
tagId INTEGER,
tagId INTEGER
)
""");
}
Expand Down Expand Up @@ -88,7 +87,7 @@ class DatabaseService {
await database.execute("""
CREATE TABLE TagsLinks(
recipeId INTEGER,
tagId INTEGER,
tagId INTEGER
)
""");
}
Expand Down
35 changes: 35 additions & 0 deletions lib/widgets/newTagDialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/utilities/database.dart';

class NewTagDialog extends StatelessWidget {
const NewTagDialog({super.key});

@override
Widget build(BuildContext context) {
TextEditingController newTagInputController = TextEditingController();

return AlertDialog(
title: const Text("New Tag"),
content: TextField(
controller: newTagInputController,
decoration: const InputDecoration(hintText: "Tag name"),
),
actions: <Widget>[
TextButton(
child: const Text("cancel"),
onPressed: () {
Navigator.pop(context);
}),
TextButton(
child: const Text("save"),
onPressed: () {
DatabaseService.createTag(Tag(name: newTagInputController.text));

Navigator.pop(context);
},
),
]);
}
}
99 changes: 99 additions & 0 deletions lib/widgets/recipeTagSelector.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/widgets/newTagDialog.dart';

class RecipeTagSelector extends StatefulWidget {
final List<Tag> tags;
final List<int> selectedTagsId;

final Function onTagsSelectionUpdate;
final Function onTagsUpdate;

const RecipeTagSelector(
{super.key,
required this.tags,
required this.onTagsUpdate,
required this.selectedTagsId,
required this.onTagsSelectionUpdate});

@override
State<RecipeTagSelector> createState() => _RecipeTagSelectorState();
}

class _RecipeTagSelectorState extends State<RecipeTagSelector> {
@override
Widget build(BuildContext context) {
return Row(children: [
ListView.builder(
itemCount: widget.tags.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemBuilder: ((context, index) {
return FilledButton(
style: FilledButton.styleFrom(
backgroundColor:
widget.selectedTagsId.contains(widget.tags[index].id)
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.secondary),
// :
// ? Theme.of(context).primaryColor
// : Colors.transparent,
child: Row(children: [
widget.selectedTagsId.contains(widget.tags[index].id)
? const Icon(Icons.check)
: const Text(""),
Text(widget.tags[index].name ?? "")
]),
onPressed: () {
bool value =
!widget.selectedTagsId.contains(widget.tags[index].id);
if (value) {
widget.selectedTagsId.add(widget.tags[index].id!);
} else {
widget.selectedTagsId.remove(widget.tags[index].id);
}
widget.onTagsSelectionUpdate(widget.selectedTagsId);
widget.onTagsUpdate(widget.tags);
});
})),
IconButton(
icon: const Icon(
Icons.add,
),
onPressed: () {
showDialog(
context: context,
builder: (context) => const NewTagDialog())
.then((value) => widget.onTagsUpdate());
})
]);
}
}

/*
DropdownButton(
onChanged: (value) {},
items: widget.tags.map<DropdownMenuItem<String>>((Tag tag) {
return DropdownMenuItem<String>(
value: tag.id.toString(),
child: Row(
children: [
Checkbox(
value: widget.selectedTagsId.contains(tag.id),
onChanged: (value) {
if (value ?? false) {
widget.selectedTagsId.add(tag.id!);
} else {
widget.selectedTagsId.remove(tag.id);
}
widget.onTagsSelectionUpdate(widget.selectedTagsId);
widget.onTagsUpdate(widget.tags);
}),
Text(tag.name ?? "")
],
),
);
}).toList(),
);
*/
33 changes: 10 additions & 23 deletions lib/widgets/tagsSelector.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/utilities/database.dart';
import 'package:reciper/widgets/newTagDialog.dart';

class TagsSelector extends StatefulWidget {
final List<Tag> tags;
Expand All @@ -25,9 +26,14 @@ class _TagsSelectorState extends State<TagsSelector> {
bool isAllChecked = true;

@override
Widget build(BuildContext context) {
TextEditingController newTagInputController = TextEditingController();
void initState() {
print(widget.tags.map((e) => e.id!).toList().toString() + "INITSTATE");
widget.onTagsSelectionUpdate(widget.tags.map((e) => e.id!).toList());
super.initState();
}

@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
Expand Down Expand Up @@ -85,27 +91,8 @@ class _TagsSelectorState extends State<TagsSelector> {
leading: const Icon(Icons.add),
title: const Text('New Tag'),
onTap: () {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text("New Tag"),
content: TextField(
controller: newTagInputController,
decoration:
const InputDecoration(hintText: "Tag name"),
),
actions: <Widget>[
TextButton(
child: const Text("save"),
onPressed: () {
DatabaseService.createTag(
Tag(name: newTagInputController.text));

widget.onTagsUpdate();
Navigator.pop(context);
},
)
]));
showDialog(context: context, builder: (context) => NewTagDialog())
.then((value) => widget.onTagsUpdate());
},
),
],
Expand Down

0 comments on commit e95cbdb

Please sign in to comment.