@@ -32,6 +32,7 @@ import 'package:wger/models/exercises/ingredient_api.dart';
32
32
import 'package:wger/models/nutrition/ingredient.dart' ;
33
33
import 'package:wger/models/nutrition/log.dart' ;
34
34
import 'package:wger/models/nutrition/nutritional_plan.dart' ;
35
+ import 'package:wger/models/nutrition/nutritional_values.dart' ;
35
36
import 'package:wger/providers/nutrition.dart' ;
36
37
import 'package:wger/widgets/core/core.dart' ;
37
38
import 'package:wger/widgets/nutrition/helpers.dart' ;
@@ -62,12 +63,17 @@ class IngredientTypeahead extends StatefulWidget {
62
63
final bool ? test;
63
64
final bool showScanner;
64
65
66
+ final Function (int id, String name, num ? amount) selectIngredient;
67
+ final Function () unSelectIngredient;
68
+
65
69
const IngredientTypeahead (
66
70
this ._ingredientIdController,
67
71
this ._ingredientController, {
68
72
this .showScanner = true ,
69
73
this .test = false ,
70
74
this .barcode = '' ,
75
+ required this .selectIngredient,
76
+ required this .unSelectIngredient,
71
77
});
72
78
73
79
@override
@@ -118,6 +124,10 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
118
124
}
119
125
return null ;
120
126
},
127
+ onChanged: (value) {
128
+ // unselect to start a new search
129
+ widget.unSelectIngredient ();
130
+ },
121
131
decoration: InputDecoration (
122
132
prefixIcon: const Icon (Icons .search),
123
133
labelText: AppLocalizations .of (context).searchIngredient,
@@ -126,7 +136,8 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
126
136
);
127
137
},
128
138
suggestionsCallback: (pattern) {
129
- if (pattern == '' ) {
139
+ // don't do search if user has already loaded a specific item
140
+ if (pattern == '' || widget._ingredientIdController.text.isNotEmpty) {
130
141
return null ;
131
142
}
132
143
@@ -151,10 +162,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
151
162
child: child,
152
163
),
153
164
onSelected: (suggestion) {
154
- //SuggestionsController.of(context).;
155
-
156
- widget._ingredientIdController.text = suggestion.data.id.toString ();
157
- widget._ingredientController.text = suggestion.value;
165
+ widget.selectIngredient (suggestion.data.id, suggestion.value, null );
158
166
},
159
167
),
160
168
if (Localizations .localeOf (context).languageCode != LANGUAGE_SHORT_ENGLISH )
@@ -188,6 +196,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
188
196
}
189
197
final result = await Provider .of <NutritionPlansProvider >(context, listen: false )
190
198
.searchIngredientWithCode (barcode);
199
+ // TODO: show spinner...
191
200
if (! mounted) {
192
201
return ;
193
202
}
@@ -198,14 +207,22 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
198
207
builder: (ctx) => AlertDialog (
199
208
key: const Key ('found-dialog' ),
200
209
title: Text (AppLocalizations .of (context).productFound),
201
- content: Text (AppLocalizations .of (context).productFoundDescription (result.name)),
210
+ content: Column (
211
+ mainAxisSize: MainAxisSize .min,
212
+ children: [
213
+ Text (AppLocalizations .of (context).productFoundDescription (result.name)),
214
+ MealItemTile (
215
+ ingredient: result,
216
+ nutritionalValues: result.nutritionalValues,
217
+ ),
218
+ ],
219
+ ),
202
220
actions: [
203
221
TextButton (
204
222
key: const Key ('found-dialog-confirm-button' ),
205
223
child: Text (MaterialLocalizations .of (context).continueButtonLabel),
206
224
onPressed: () {
207
- widget._ingredientController.text = result.name;
208
- widget._ingredientIdController.text = result.id.toString ();
225
+ widget.selectIngredient (result.id, result.name, null );
209
226
Navigator .of (ctx).pop ();
210
227
},
211
228
),
@@ -357,3 +374,23 @@ class IngredientAvatar extends StatelessWidget {
357
374
: const CircleIconAvatar (Icon (Icons .image, color: Colors .grey));
358
375
}
359
376
}
377
+
378
+ class MealItemTile extends StatelessWidget {
379
+ final Ingredient ingredient;
380
+ final NutritionalValues nutritionalValues;
381
+
382
+ const MealItemTile ({
383
+ super .key,
384
+ required this .ingredient,
385
+ required this .nutritionalValues,
386
+ });
387
+
388
+ @override
389
+ Widget build (BuildContext context) {
390
+ return ListTile (
391
+ leading: IngredientAvatar (ingredient: ingredient),
392
+ title: Text (getShortNutritionValues (nutritionalValues, context)),
393
+ // subtitle: Text(ingredient.id.toString()),
394
+ );
395
+ }
396
+ }
0 commit comments