diff --git a/lib/widgets/nutrition/forms.dart b/lib/widgets/nutrition/forms.dart index 7ba3443e..247d8abc 100644 --- a/lib/widgets/nutrition/forms.dart +++ b/lib/widgets/nutrition/forms.dart @@ -181,6 +181,27 @@ class IngredientFormState extends State { MealItem get mealItem => _mealItem; +// note: make sure to set _mealItem.ingredient (before/after calling this) + void selectIngredient(int id, String name, num? amount) { + setState(() { + _mealItem.ingredientId = id; + _ingredientController.text = name; + _ingredientIdController.text = id.toString(); + if (amount != null) { + _amountController.text = amount.toStringAsFixed(0); + _mealItem.amount = amount; + } + }); + } + +// note: does not reset text search and amount inputs + void unSelectIngredient() { + setState(() { + _mealItem.ingredientId = 0; + _ingredientIdController.text = ''; + }); + } + @override Widget build(BuildContext context) { final String unit = AppLocalizations.of(context).g; @@ -196,6 +217,8 @@ class IngredientFormState extends State { _ingredientController, barcode: widget.barcode, test: widget.test, + selectIngredient: selectIngredient, + unSelectIngredient: unSelectIngredient, ), Row( children: [ @@ -286,7 +309,7 @@ class IngredientFormState extends State { ), ], ), - if (ingredientIdController.text.isNotEmpty) + if (ingredientIdController.text.isNotEmpty && _amountController.text.isNotEmpty) Padding( padding: const EdgeInsets.all(8.0), child: Column( @@ -363,14 +386,9 @@ class IngredientFormState extends State { return Card( child: ListTile( onTap: () { - setState(() { - _ingredientController.text = widget.recent[index].ingredient.name; - _ingredientIdController.text = - widget.recent[index].ingredient.id.toString(); - _amountController.text = widget.recent[index].amount.toStringAsFixed(0); - _mealItem.ingredientId = widget.recent[index].ingredientId; - _mealItem.amount = widget.recent[index].amount; - }); + final ingredient = widget.recent[index].ingredient; + selectIngredient( + ingredient.id, ingredient.name, widget.recent[index].amount); }, title: Text( '${widget.recent[index].ingredient.name} (${widget.recent[index].amount.toStringAsFixed(0)}$unit)'), diff --git a/lib/widgets/nutrition/widgets.dart b/lib/widgets/nutrition/widgets.dart index 074d5713..4b2f9d12 100644 --- a/lib/widgets/nutrition/widgets.dart +++ b/lib/widgets/nutrition/widgets.dart @@ -63,12 +63,17 @@ class IngredientTypeahead extends StatefulWidget { final bool? test; final bool showScanner; + final Function(int id, String name, num? amount) selectIngredient; + final Function() unSelectIngredient; + const IngredientTypeahead( this._ingredientIdController, this._ingredientController, { this.showScanner = true, this.test = false, this.barcode = '', + required this.selectIngredient, + required this.unSelectIngredient, }); @override @@ -120,9 +125,8 @@ class _IngredientTypeaheadState extends State { return null; }, onChanged: (value) { - // if user changes the pattern, it means they want to drop the - // currently loaded ingredient (if any) and start a new search - widget._ingredientIdController.text = ''; + // unselect to start a new search + widget.unSelectIngredient(); }, decoration: InputDecoration( prefixIcon: const Icon(Icons.search), @@ -158,10 +162,7 @@ class _IngredientTypeaheadState extends State { child: child, ), onSelected: (suggestion) { - //SuggestionsController.of(context).; - - widget._ingredientIdController.text = suggestion.data.id.toString(); - widget._ingredientController.text = suggestion.value; + widget.selectIngredient(suggestion.data.id, suggestion.value, null); }, ), if (Localizations.localeOf(context).languageCode != LANGUAGE_SHORT_ENGLISH) @@ -221,8 +222,7 @@ class _IngredientTypeaheadState extends State { key: const Key('found-dialog-confirm-button'), child: Text(MaterialLocalizations.of(context).continueButtonLabel), onPressed: () { - widget._ingredientController.text = result.name; - widget._ingredientIdController.text = result.id.toString(); + widget.selectIngredient(result.id, result.name, null); Navigator.of(ctx).pop(); }, ),