Skip to content

Commit

Permalink
added "remove filters" button
Browse files Browse the repository at this point in the history
  • Loading branch information
judemont committed Apr 28, 2024
1 parent c53de55 commit 9fd7d75
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
25 changes: 20 additions & 5 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class _HomePageState extends State<HomePage> {
loadRecipes();
loadTags().then((value) {
selectedTagsId.clear();
print(tags.toString() + "NNNN");
selectedTagsId.addAll(tags.map((e) => e.id!).toList());
});

Expand Down Expand Up @@ -81,6 +80,9 @@ class _HomePageState extends State<HomePage> {
body: SingleChildScrollView(
child: Column(
children: [
const SizedBox(
height: 20,
),
Visibility(
visible: displaySearchField,
child: SizedBox(
Expand All @@ -94,6 +96,22 @@ class _HomePageState extends State<HomePage> {
loadRecipes(searchQuery: value);
},
))),
const SizedBox(
height: 20,
),
Visibility(
visible: selectedTagsId.length != tags.length,
child: ElevatedButton(
child: const Text("Remove filters"),
onPressed: () {
setState(() {
selectedTagsId.clear();
selectedTagsId.addAll(tags.map((e) => e.id!).toList());
});
loadRecipes();
},
),
),
RecipeListView(
reloadRecipes: loadRecipes,
recipes: recipes,
Expand All @@ -105,7 +123,6 @@ class _HomePageState extends State<HomePage> {
}

Future<void> loadRecipes({searchQuery = ""}) async {
print(selectedTagsId);
if (selectedTagsId.length == tags.length) {
DatabaseService.getRecipes(searchQuery: searchQuery)
.then((List<Recipe> result) {
Expand All @@ -114,7 +131,6 @@ class _HomePageState extends State<HomePage> {
});
});
} else {
print("FILTERRRR");
setState(() {
recipes = [];
});
Expand All @@ -123,7 +139,6 @@ class _HomePageState extends State<HomePage> {
DatabaseService.getRecipesFromTag(tagId, searchQuery: searchQuery)
.then((values) {
for (var recipe in values) {
print(recipe.title ?? "" + "UUU");
if (!recipes.contains(recipe)) {
setState(() {
recipes.add(recipe);
Expand All @@ -144,10 +159,10 @@ class _HomePageState extends State<HomePage> {
}

Future<void> onTagsSelectionUpdate(List<int> values) async {
print(values);
setState(() {
selectedTagsId = values;
});
print(selectedTagsId.toString() + " tags selected");
loadRecipes();
}

Expand Down
5 changes: 2 additions & 3 deletions lib/screens/recipe_editor.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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';
Expand Down Expand Up @@ -124,7 +123,7 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
),
),
SizedBox(height: fieldsMargin),
Container(
SizedBox(
height: 50,
child: Row(
children: [
Expand Down Expand Up @@ -212,7 +211,7 @@ class _RecipeEditorPageState extends State<RecipeEditorPage> {
Future<void> onTagsSelectionUpdate(List<int> values) async {
setState(() {
selectedTagsId = values;
print(selectedTagsId.toString() + "çççççççççççççççç");
print("$selectedTagsIdçççççççççççççççç");
});
}
}
2 changes: 1 addition & 1 deletion lib/utilities/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class DatabaseService {
List<Map<String, dynamic>> tag =
await db.query('Tags', where: "id = ${tagLink['tagId']}");

recipeTags.add(Tag.fromMap(tag[0]));
tag.isNotEmpty ? recipeTags.add(Tag.fromMap(tag[0])) : null;
}

return recipeTags;
Expand Down
1 change: 0 additions & 1 deletion lib/widgets/recipeTagSelector.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/widgets/newTagDialog.dart';
Expand Down
10 changes: 5 additions & 5 deletions lib/widgets/tagsSelector.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:reciper/models/tag.dart';
import 'package:reciper/utilities/database.dart';
import 'package:reciper/widgets/newTagDialog.dart';
import 'package:reciper/widgets/tagActionsDialog.dart';

Expand All @@ -12,12 +11,11 @@ class TagsSelector extends StatefulWidget {
final Function onTagsUpdate;

const TagsSelector(
{Key? key,
{super.key,
required this.tags,
required this.onTagsUpdate,
required this.selectedTagsId,
required this.onTagsSelectionUpdate})
: super(key: key);
required this.onTagsSelectionUpdate});

@override
State<TagsSelector> createState() => _TagsSelectorState();
Expand Down Expand Up @@ -89,7 +87,9 @@ class _TagsSelectorState extends State<TagsSelector> {
onChanged: (value) {
setState(() {
if (value ?? false) {
widget.selectedTagsId.add(widget.tags[index].id!);
!widget.selectedTagsId.contains(widget.tags[index].id!)
? widget.selectedTagsId.add(widget.tags[index].id!)
: null;
} else {
widget.selectedTagsId.remove(widget.tags[index].id);
}
Expand Down

0 comments on commit 9fd7d75

Please sign in to comment.