mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
filter suggestions by search string
This commit is contained in:
@@ -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),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -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();
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user