filter suggestions by search string

This commit is contained in:
Dieter Plaetinck
2024-05-28 12:18:21 +02:00
parent a4bdf70582
commit 686fb44933
2 changed files with 20 additions and 8 deletions

View File

@@ -168,6 +168,7 @@ class IngredientFormState extends State<IngredientForm> {
final _dateController = TextEditingController(); // optional
final _timeController = TextEditingController(); // optional
final _mealItem = MealItem.empty();
var _searchQuery = ''; // copy from typeahead. for filtering suggestions
@override
void initState() {
@@ -201,10 +202,18 @@ class IngredientFormState extends State<IngredientForm> {
});
}
void updateSearchQuery(String query) {
setState(() {
_searchQuery = query;
});
}
@override
Widget build(BuildContext context) {
final String unit = AppLocalizations.of(context).g;
final queryLower = _searchQuery.toLowerCase();
final suggestions =
widget.recent.where((e) => e.ingredient.name.toLowerCase().contains(queryLower)).toList();
return Container(
margin: const EdgeInsets.all(20),
child: Form(
@@ -218,6 +227,7 @@ class IngredientFormState extends State<IngredientForm> {
test: widget.test,
selectIngredient: selectIngredient,
unSelectIngredient: unSelectIngredient,
updateSearchQuery: updateSearchQuery,
),
Row(
children: [
@@ -370,27 +380,26 @@ class IngredientFormState extends State<IngredientForm> {
Navigator.of(context).pop();
},
),
if (widget.recent.isNotEmpty) const SizedBox(height: 10.0),
if (suggestions.isNotEmpty) const SizedBox(height: 10.0),
Container(
padding: const EdgeInsets.all(10.0),
child: Text(AppLocalizations.of(context).recentlyUsedIngredients),
),
Expanded(
child: ListView.builder(
itemCount: widget.recent.length,
itemCount: suggestions.length,
shrinkWrap: true,
itemBuilder: (context, index) {
return Card(
child: ListTile(
onTap: () {
final ingredient = widget.recent[index].ingredient;
selectIngredient(
ingredient.id, ingredient.name, widget.recent[index].amount);
final ingredient = suggestions[index].ingredient;
selectIngredient(ingredient.id, ingredient.name, suggestions[index].amount);
},
title: Text(
'${widget.recent[index].ingredient.name} (${widget.recent[index].amount.toStringAsFixed(0)}$unit)'),
'${suggestions[index].ingredient.name} (${suggestions[index].amount.toStringAsFixed(0)}$unit)'),
subtitle: Text(getShortNutritionValues(
widget.recent[index].ingredient.nutritionalValues, context)),
suggestions[index].ingredient.nutritionalValues, context)),
trailing: const Icon(Icons.copy),
),
);

View File

@@ -65,6 +65,7 @@ class IngredientTypeahead extends StatefulWidget {
final Function(int id, String name, num? amount) selectIngredient;
final Function() unSelectIngredient;
final Function(String query) updateSearchQuery;
const IngredientTypeahead(
this._ingredientIdController,
@@ -74,6 +75,7 @@ class IngredientTypeahead extends StatefulWidget {
this.barcode = '',
required this.selectIngredient,
required this.unSelectIngredient,
required this.updateSearchQuery,
});
@override
@@ -125,6 +127,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
return null;
},
onChanged: (value) {
widget.updateSearchQuery(value);
// unselect to start a new search
widget.unSelectIngredient();
},