Reputation: 1
Im trying to filter recipes that have a single string of ingredients that have all the ingredients from resultData which is a list of strings. Cant figure how to do it. The current code rn only filters the recipe that have any ingredients from resultData.
here is the full code:
import 'package:flutter/material.dart';
import 'package:recipe_page_new/data_repository/dbHelper.dart';
import 'package:recipe_page_new/models/recipe_model.dart';
import 'package:recipe_page_new/ui/widgets/recipe_widget.dart';
// ignore: must_be_immutable
class ShowRecipeWithIngredients extends StatefulWidget {
final List<RecipeModel> recipes;
List<RecipeModel> filteredRecipes = [];
final List<String> resultData;
ShowRecipeWithIngredients({super.key, required this.recipes, required this.resultData}) {
filteredRecipes = recipes;
}
@override
State<ShowRecipeWithIngredients> createState() => _SearchRecipeScreenState();
}
class _SearchRecipeScreenState extends State<ShowRecipeWithIngredients> {
void filterRecipe(String value) {
setState(() {
widget.filteredRecipes = widget.recipes
.where((recipe) =>
recipe.name.toLowerCase().contains(value.toLowerCase()))
.toList();
});
}
void _filterRecipes(List<String> resultData) {
setState(() {
widget.filteredRecipes = widget.recipes.where((recipe) {
// Check if all ingredients in resultData are in the recipe's ingredients
return resultData.any((ingredient) =>
recipe.ingredients.contains(ingredient.toLowerCase())
);
}).toList();
// test whel!
print(resultData);
});
}
@override
void initState() {
_filterRecipes(widget.resultData);
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TextField(
onChanged: (value) {
filterRecipe(value);
},
decoration: const InputDecoration(
icon: Icon(
Icons.search,
color: Colors.white,
),
hintText: "Search Recipe",
hintStyle: TextStyle(color: Colors.white),
),
),
actions: <Widget>[
IconButton(
icon: const Icon(Icons.cancel),
onPressed: () {
Navigator.pop(context);
},
)
],
),
body: Container(
padding: const EdgeInsets.all(10),
child: widget.filteredRecipes.isNotEmpty
? ListView.builder(
itemCount: widget.filteredRecipes.length,
itemBuilder: (BuildContext context, int index) {
return RecipeWidget(widget.filteredRecipes[index]);
},
)
: const Center(
child: Text('Recipe not found...'),
),
),
);
}
}
help and answers please
Upvotes: 0
Views: 18