Skip to content

Commit 686fb44

Browse files
committed
filter suggestions by search string
1 parent a4bdf70 commit 686fb44

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

lib/widgets/nutrition/forms.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ class IngredientFormState extends State<IngredientForm> {
168168
final _dateController = TextEditingController(); // optional
169169
final _timeController = TextEditingController(); // optional
170170
final _mealItem = MealItem.empty();
171+
var _searchQuery = ''; // copy from typeahead. for filtering suggestions
171172

172173
@override
173174
void initState() {
@@ -201,10 +202,18 @@ class IngredientFormState extends State<IngredientForm> {
201202
});
202203
}
203204

205+
void updateSearchQuery(String query) {
206+
setState(() {
207+
_searchQuery = query;
208+
});
209+
}
210+
204211
@override
205212
Widget build(BuildContext context) {
206213
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();
208217
return Container(
209218
margin: const EdgeInsets.all(20),
210219
child: Form(
@@ -218,6 +227,7 @@ class IngredientFormState extends State<IngredientForm> {
218227
test: widget.test,
219228
selectIngredient: selectIngredient,
220229
unSelectIngredient: unSelectIngredient,
230+
updateSearchQuery: updateSearchQuery,
221231
),
222232
Row(
223233
children: [
@@ -370,27 +380,26 @@ class IngredientFormState extends State<IngredientForm> {
370380
Navigator.of(context).pop();
371381
},
372382
),
373-
if (widget.recent.isNotEmpty) const SizedBox(height: 10.0),
383+
if (suggestions.isNotEmpty) const SizedBox(height: 10.0),
374384
Container(
375385
padding: const EdgeInsets.all(10.0),
376386
child: Text(AppLocalizations.of(context).recentlyUsedIngredients),
377387
),
378388
Expanded(
379389
child: ListView.builder(
380-
itemCount: widget.recent.length,
390+
itemCount: suggestions.length,
381391
shrinkWrap: true,
382392
itemBuilder: (context, index) {
383393
return Card(
384394
child: ListTile(
385395
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);
389398
},
390399
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)'),
392401
subtitle: Text(getShortNutritionValues(
393-
widget.recent[index].ingredient.nutritionalValues, context)),
402+
suggestions[index].ingredient.nutritionalValues, context)),
394403
trailing: const Icon(Icons.copy),
395404
),
396405
);

lib/widgets/nutrition/widgets.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class IngredientTypeahead extends StatefulWidget {
6565

6666
final Function(int id, String name, num? amount) selectIngredient;
6767
final Function() unSelectIngredient;
68+
final Function(String query) updateSearchQuery;
6869

6970
const IngredientTypeahead(
7071
this._ingredientIdController,
@@ -74,6 +75,7 @@ class IngredientTypeahead extends StatefulWidget {
7475
this.barcode = '',
7576
required this.selectIngredient,
7677
required this.unSelectIngredient,
78+
required this.updateSearchQuery,
7779
});
7880

7981
@override
@@ -125,6 +127,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
125127
return null;
126128
},
127129
onChanged: (value) {
130+
widget.updateSearchQuery(value);
128131
// unselect to start a new search
129132
widget.unSelectIngredient();
130133
},

0 commit comments

Comments
 (0)