@@ -168,6 +168,7 @@ class IngredientFormState extends State<IngredientForm> {
168
168
final _dateController = TextEditingController (); // optional
169
169
final _timeController = TextEditingController (); // optional
170
170
final _mealItem = MealItem .empty ();
171
+ var _searchQuery = '' ; // copy from typeahead. for filtering suggestions
171
172
172
173
@override
173
174
void initState () {
@@ -201,10 +202,18 @@ class IngredientFormState extends State<IngredientForm> {
201
202
});
202
203
}
203
204
205
+ void updateSearchQuery (String query) {
206
+ setState (() {
207
+ _searchQuery = query;
208
+ });
209
+ }
210
+
204
211
@override
205
212
Widget build (BuildContext context) {
206
213
final String unit = AppLocalizations .of (context).g;
207
-
214
+ final queryLower = _searchQuery.toLowerCase ();
215
+ final suggestions =
216
+ widget.recent.where ((e) => e.ingredient.name.toLowerCase ().contains (queryLower)).toList ();
208
217
return Container (
209
218
margin: const EdgeInsets .all (20 ),
210
219
child: Form (
@@ -218,6 +227,7 @@ class IngredientFormState extends State<IngredientForm> {
218
227
test: widget.test,
219
228
selectIngredient: selectIngredient,
220
229
unSelectIngredient: unSelectIngredient,
230
+ updateSearchQuery: updateSearchQuery,
221
231
),
222
232
Row (
223
233
children: [
@@ -370,27 +380,26 @@ class IngredientFormState extends State<IngredientForm> {
370
380
Navigator .of (context).pop ();
371
381
},
372
382
),
373
- if (widget.recent .isNotEmpty) const SizedBox (height: 10.0 ),
383
+ if (suggestions .isNotEmpty) const SizedBox (height: 10.0 ),
374
384
Container (
375
385
padding: const EdgeInsets .all (10.0 ),
376
386
child: Text (AppLocalizations .of (context).recentlyUsedIngredients),
377
387
),
378
388
Expanded (
379
389
child: ListView .builder (
380
- itemCount: widget.recent .length,
390
+ itemCount: suggestions .length,
381
391
shrinkWrap: true ,
382
392
itemBuilder: (context, index) {
383
393
return Card (
384
394
child: ListTile (
385
395
onTap: () {
386
- final ingredient = widget.recent[index].ingredient;
387
- selectIngredient (
388
- ingredient.id, ingredient.name, widget.recent[index].amount);
396
+ final ingredient = suggestions[index].ingredient;
397
+ selectIngredient (ingredient.id, ingredient.name, suggestions[index].amount);
389
398
},
390
399
title: Text (
391
- '${widget . recent [index ].ingredient .name } (${widget . recent [index ].amount .toStringAsFixed (0 )}$unit )' ),
400
+ '${suggestions [index ].ingredient .name } (${suggestions [index ].amount .toStringAsFixed (0 )}$unit )' ),
392
401
subtitle: Text (getShortNutritionValues (
393
- widget.recent [index].ingredient.nutritionalValues, context)),
402
+ suggestions [index].ingredient.nutritionalValues, context)),
394
403
trailing: const Icon (Icons .copy),
395
404
),
396
405
);
0 commit comments