Skip to content

Commit 864c6fc

Browse files
committed
properly set/unset value via helper functions. fix #591
1 parent b21c19a commit 864c6fc

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

lib/widgets/nutrition/forms.dart

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,27 @@ class IngredientFormState extends State<IngredientForm> {
181181

182182
MealItem get mealItem => _mealItem;
183183

184+
// note: make sure to set _mealItem.ingredient (before/after calling this)
185+
void selectIngredient(int id, String name, num? amount) {
186+
setState(() {
187+
_mealItem.ingredientId = id;
188+
_ingredientController.text = name;
189+
_ingredientIdController.text = id.toString();
190+
if (amount != null) {
191+
_amountController.text = amount.toStringAsFixed(0);
192+
_mealItem.amount = amount;
193+
}
194+
});
195+
}
196+
197+
// note: does not reset text search and amount inputs
198+
void unSelectIngredient() {
199+
setState(() {
200+
_mealItem.ingredientId = 0;
201+
_ingredientIdController.text = '';
202+
});
203+
}
204+
184205
@override
185206
Widget build(BuildContext context) {
186207
final String unit = AppLocalizations.of(context).g;
@@ -196,6 +217,8 @@ class IngredientFormState extends State<IngredientForm> {
196217
_ingredientController,
197218
barcode: widget.barcode,
198219
test: widget.test,
220+
selectIngredient: selectIngredient,
221+
unSelectIngredient: unSelectIngredient,
199222
),
200223
Row(
201224
children: [
@@ -286,7 +309,7 @@ class IngredientFormState extends State<IngredientForm> {
286309
),
287310
],
288311
),
289-
if (ingredientIdController.text.isNotEmpty)
312+
if (ingredientIdController.text.isNotEmpty && _amountController.text.isNotEmpty)
290313
Padding(
291314
padding: const EdgeInsets.all(8.0),
292315
child: Column(
@@ -363,14 +386,9 @@ class IngredientFormState extends State<IngredientForm> {
363386
return Card(
364387
child: ListTile(
365388
onTap: () {
366-
setState(() {
367-
_ingredientController.text = widget.recent[index].ingredient.name;
368-
_ingredientIdController.text =
369-
widget.recent[index].ingredient.id.toString();
370-
_amountController.text = widget.recent[index].amount.toStringAsFixed(0);
371-
_mealItem.ingredientId = widget.recent[index].ingredientId;
372-
_mealItem.amount = widget.recent[index].amount;
373-
});
389+
final ingredient = widget.recent[index].ingredient;
390+
selectIngredient(
391+
ingredient.id, ingredient.name, widget.recent[index].amount);
374392
},
375393
title: Text(
376394
'${widget.recent[index].ingredient.name} (${widget.recent[index].amount.toStringAsFixed(0)}$unit)'),

lib/widgets/nutrition/widgets.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,17 @@ class IngredientTypeahead extends StatefulWidget {
6363
final bool? test;
6464
final bool showScanner;
6565

66+
final Function(int id, String name, num? amount) selectIngredient;
67+
final Function() unSelectIngredient;
68+
6669
const IngredientTypeahead(
6770
this._ingredientIdController,
6871
this._ingredientController, {
6972
this.showScanner = true,
7073
this.test = false,
7174
this.barcode = '',
75+
required this.selectIngredient,
76+
required this.unSelectIngredient,
7277
});
7378

7479
@override
@@ -120,9 +125,8 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
120125
return null;
121126
},
122127
onChanged: (value) {
123-
// if user changes the pattern, it means they want to drop the
124-
// currently loaded ingredient (if any) and start a new search
125-
widget._ingredientIdController.text = '';
128+
// unselect to start a new search
129+
widget.unSelectIngredient();
126130
},
127131
decoration: InputDecoration(
128132
prefixIcon: const Icon(Icons.search),
@@ -158,10 +162,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
158162
child: child,
159163
),
160164
onSelected: (suggestion) {
161-
//SuggestionsController.of(context).;
162-
163-
widget._ingredientIdController.text = suggestion.data.id.toString();
164-
widget._ingredientController.text = suggestion.value;
165+
widget.selectIngredient(suggestion.data.id, suggestion.value, null);
165166
},
166167
),
167168
if (Localizations.localeOf(context).languageCode != LANGUAGE_SHORT_ENGLISH)
@@ -221,8 +222,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
221222
key: const Key('found-dialog-confirm-button'),
222223
child: Text(MaterialLocalizations.of(context).continueButtonLabel),
223224
onPressed: () {
224-
widget._ingredientController.text = result.name;
225-
widget._ingredientIdController.text = result.id.toString();
225+
widget.selectIngredient(result.id, result.name, null);
226226
Navigator.of(ctx).pop();
227227
},
228228
),

0 commit comments

Comments
 (0)