mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
properly set/unset value via helper functions. fix #591
This commit is contained in:
@@ -181,6 +181,27 @@ class IngredientFormState extends State<IngredientForm> {
|
||||
|
||||
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<IngredientForm> {
|
||||
_ingredientController,
|
||||
barcode: widget.barcode,
|
||||
test: widget.test,
|
||||
selectIngredient: selectIngredient,
|
||||
unSelectIngredient: unSelectIngredient,
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
@@ -286,7 +309,7 @@ class IngredientFormState extends State<IngredientForm> {
|
||||
),
|
||||
],
|
||||
),
|
||||
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<IngredientForm> {
|
||||
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)'),
|
||||
|
||||
@@ -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<IngredientTypeahead> {
|
||||
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<IngredientTypeahead> {
|
||||
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<IngredientTypeahead> {
|
||||
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();
|
||||
},
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user