mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 02:24:30 +01:00
Rework the ingredient form
This makes it more user-friendly and doesn't completely break down when ingredients had longer named units Closes #1390
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
import 'package:wger/core/colors.dart';
|
||||
import 'package:wger/core/consts.dart';
|
||||
import 'package:wger/core/formatting/formatting.dart';
|
||||
import 'package:wger/core/number_input.dart';
|
||||
@@ -32,10 +33,11 @@ import 'package:wger/features/nutrition/models/log.dart';
|
||||
import 'package:wger/features/nutrition/models/meal.dart';
|
||||
import 'package:wger/features/nutrition/models/meal_item.dart';
|
||||
import 'package:wger/features/nutrition/models/nutritional_plan.dart';
|
||||
import 'package:wger/features/nutrition/models/nutritional_values.dart';
|
||||
import 'package:wger/features/nutrition/providers/nutrition_notifier.dart';
|
||||
import 'package:wger/features/nutrition/screens/nutritional_plan_screen.dart';
|
||||
import 'package:wger/features/nutrition/widgets/helpers.dart';
|
||||
import 'package:wger/features/nutrition/widgets/nutrition_tiles.dart';
|
||||
import 'package:wger/features/nutrition/widgets/ingredient_images.dart';
|
||||
import 'package:wger/features/nutrition/widgets/widgets.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
|
||||
@@ -208,6 +210,14 @@ class IngredientFormState extends ConsumerState<IngredientForm> {
|
||||
});
|
||||
}
|
||||
|
||||
void _selectWeightUnit(IngredientWeightUnit? unit) {
|
||||
setState(() {
|
||||
_selectedWeightUnit = unit;
|
||||
_mealItem.weightUnitId = unit?.id;
|
||||
_mealItem.weightUnitObj = unit;
|
||||
});
|
||||
}
|
||||
|
||||
// note: does not reset text search and amount inputs
|
||||
void unSelectIngredient() {
|
||||
setState(() {
|
||||
@@ -216,6 +226,20 @@ class IngredientFormState extends ConsumerState<IngredientForm> {
|
||||
});
|
||||
}
|
||||
|
||||
/// Drops the selected ingredient and everything derived from it, keeping
|
||||
/// only the typed amount.
|
||||
void _clearIngredient() {
|
||||
setState(() {
|
||||
_mealItem.ingredient = null;
|
||||
_mealItem.ingredientId = 0;
|
||||
_ingredientController.text = '';
|
||||
_ingredientIdController.text = '';
|
||||
_searchQuery = '';
|
||||
_weightUnits = [];
|
||||
});
|
||||
_selectWeightUnit(null);
|
||||
}
|
||||
|
||||
void updateSearchQuery(String query) {
|
||||
setState(() {
|
||||
_searchQuery = query;
|
||||
@@ -239,183 +263,214 @@ class IngredientFormState extends ConsumerState<IngredientForm> {
|
||||
margin: const EdgeInsets.all(20),
|
||||
child: Form(
|
||||
key: _form,
|
||||
child: Column(
|
||||
children: [
|
||||
IngredientTypeahead(
|
||||
_ingredientIdController,
|
||||
_ingredientController,
|
||||
barcode: widget.barcode,
|
||||
test: widget.test,
|
||||
selectIngredient: selectIngredient,
|
||||
onDeselectIngredient: unSelectIngredient,
|
||||
onUpdateSearchQuery: updateSearchQuery,
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
key: const Key('field-weight'),
|
||||
decoration: InputDecoration(
|
||||
labelText: i18n.weight,
|
||||
suffix: _weightUnits.isNotEmpty
|
||||
? DropdownButton<int?>(
|
||||
value: _selectedWeightUnit?.id,
|
||||
underline: const SizedBox(),
|
||||
isDense: true,
|
||||
items: [
|
||||
DropdownMenuItem<int?>(
|
||||
value: null,
|
||||
child: Text(i18n.g),
|
||||
),
|
||||
..._weightUnits.map(
|
||||
(unit) => DropdownMenuItem<int?>(
|
||||
value: unit.id,
|
||||
child: Text('${unit.name} (${unit.grams}g)'),
|
||||
),
|
||||
),
|
||||
],
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
if (value == null) {
|
||||
_selectedWeightUnit = null;
|
||||
_mealItem.weightUnitId = null;
|
||||
_mealItem.weightUnitObj = null;
|
||||
} else {
|
||||
_selectedWeightUnit = _weightUnits.firstWhere(
|
||||
(u) => u.id == value,
|
||||
);
|
||||
_mealItem.weightUnitId = value;
|
||||
_mealItem.weightUnitObj = _selectedWeightUnit;
|
||||
}
|
||||
});
|
||||
},
|
||||
)
|
||||
: Text(i18n.g),
|
||||
),
|
||||
controller: _amountController,
|
||||
keyboardType: textInputTypeDecimal,
|
||||
inputFormatters: [
|
||||
LocalizedDecimalInputFormatter(numberFormat.symbols.DECIMAL_SEP),
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
if (_mealItem.ingredient != null && _ingredientIdController.text.isNotEmpty)
|
||||
Padding(
|
||||
key: const Key('selected-ingredient'),
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: Row(
|
||||
spacing: 12,
|
||||
children: [
|
||||
ClipOval(
|
||||
child: ColoredBox(
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
child: SizedBox.square(
|
||||
dimension: 64,
|
||||
child: IngredientImageWidget(image: _mealItem.ingredient!.image),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
_mealItem.ingredient!.name,
|
||||
style: Theme.of(context).textTheme.titleLarge,
|
||||
),
|
||||
Text(
|
||||
'${i18n.kcalValue(_mealItem.ingredient!.energy.toString())} / 100 ${i18n.g}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.info_outline),
|
||||
onPressed: () => showIngredientDetails(context, ref, _mealItem.ingredient!),
|
||||
),
|
||||
IconButton(
|
||||
key: const Key('clear-ingredient'),
|
||||
icon: const Icon(Icons.close),
|
||||
tooltip: MaterialLocalizations.of(context).clearButtonTooltip,
|
||||
onPressed: _clearIngredient,
|
||||
),
|
||||
],
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
final v = numberFormat.tryParse(value);
|
||||
if (v != null) {
|
||||
_mealItem.amount = v;
|
||||
}
|
||||
});
|
||||
},
|
||||
onSaved: (value) {
|
||||
_mealItem.amount = numberFormat.parse(value!);
|
||||
},
|
||||
validator: (value) {
|
||||
final text = value?.trim() ?? '';
|
||||
if (text.isEmpty) {
|
||||
return i18n.enterValue;
|
||||
}
|
||||
final parsed = numberFormat.tryParse(text);
|
||||
if (parsed == null) {
|
||||
return i18n.enterValidNumber;
|
||||
}
|
||||
),
|
||||
)
|
||||
else
|
||||
IngredientTypeahead(
|
||||
_ingredientIdController,
|
||||
_ingredientController,
|
||||
barcode: widget.barcode,
|
||||
test: widget.test,
|
||||
selectIngredient: selectIngredient,
|
||||
onDeselectIngredient: unSelectIngredient,
|
||||
onUpdateSearchQuery: updateSearchQuery,
|
||||
),
|
||||
TextFormField(
|
||||
key: const Key('field-weight'),
|
||||
decoration: InputDecoration(
|
||||
labelText: i18n.weight,
|
||||
suffixText: _selectedWeightUnit != null
|
||||
? '× ${_selectedWeightUnit!.name}'
|
||||
: i18n.g,
|
||||
),
|
||||
controller: _amountController,
|
||||
keyboardType: textInputTypeDecimal,
|
||||
inputFormatters: [
|
||||
LocalizedDecimalInputFormatter(numberFormat.symbols.DECIMAL_SEP),
|
||||
],
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
final v = numberFormat.tryParse(value);
|
||||
if (v != null) {
|
||||
_mealItem.amount = v;
|
||||
}
|
||||
});
|
||||
},
|
||||
onSaved: (value) {
|
||||
_mealItem.amount = numberFormat.parse(value!);
|
||||
},
|
||||
validator: (value) {
|
||||
final text = value?.trim() ?? '';
|
||||
if (text.isEmpty) {
|
||||
return i18n.enterValue;
|
||||
}
|
||||
final parsed = numberFormat.tryParse(text);
|
||||
if (parsed == null) {
|
||||
return i18n.enterValidNumber;
|
||||
}
|
||||
|
||||
if (parsed < 1 || parsed > 1000) {
|
||||
return i18n.formMinMaxValues(1, 1000);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
if (parsed < 1 || parsed > 1000) {
|
||||
return i18n.formMinMaxValues(1, 1000);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
if (_weightUnits.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(
|
||||
spacing: 8,
|
||||
children: [
|
||||
ChoiceChip(
|
||||
label: Text(i18n.g),
|
||||
selected: _selectedWeightUnit == null,
|
||||
onSelected: (_) => _selectWeightUnit(null),
|
||||
),
|
||||
..._weightUnits.map(
|
||||
(unit) => ChoiceChip(
|
||||
label: Text('${unit.name} (${unit.grams}g)'),
|
||||
selected: _selectedWeightUnit?.id == unit.id,
|
||||
onSelected: (_) => _selectWeightUnit(unit),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
if (widget.withDate)
|
||||
Expanded(
|
||||
child: DateInputWidget(
|
||||
value: _date,
|
||||
labelText: i18n.date,
|
||||
firstDate: DateTime(DateTime.now().year - 10),
|
||||
lastDate: DateTime.now(),
|
||||
onChanged: (date) => _date = date,
|
||||
),
|
||||
if (_ingredientIdController.text.isNotEmpty &&
|
||||
_amountController.text.isNotEmpty &&
|
||||
_mealItem.ingredient != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 12, bottom: 4),
|
||||
child: _MacrosSummary(
|
||||
key: const Key('macros-summary'),
|
||||
values: _mealItem.nutritionalValues,
|
||||
grams: _selectedWeightUnit == null
|
||||
? null
|
||||
: _mealItem.amount * _selectedWeightUnit!.grams,
|
||||
),
|
||||
if (widget.withDate)
|
||||
Expanded(
|
||||
child: TimeInputWidget(
|
||||
key: const Key('field-time'),
|
||||
value: _time,
|
||||
labelText: i18n.time,
|
||||
onChanged: (time) => _time = time,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (widget.meals.isNotEmpty)
|
||||
DropdownButtonFormField<String?>(
|
||||
key: const Key('field-meal'),
|
||||
initialValue: _selectedMealId,
|
||||
decoration: InputDecoration(labelText: i18n.meal),
|
||||
items: [
|
||||
DropdownMenuItem<String?>(
|
||||
value: null,
|
||||
child: Text(i18n.otherLogs),
|
||||
),
|
||||
...widget.meals.map(
|
||||
(meal) => DropdownMenuItem<String?>(
|
||||
value: meal.id,
|
||||
child: Text(
|
||||
meal.name.isNotEmpty ? meal.name : meal.time?.format(context) ?? '',
|
||||
),
|
||||
if (widget.withDate)
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: DateInputWidget(
|
||||
value: _date,
|
||||
labelText: i18n.date,
|
||||
firstDate: DateTime(DateTime.now().year - 10),
|
||||
lastDate: DateTime.now(),
|
||||
onChanged: (date) => _date = date,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
onChanged: (value) => setState(() => _selectedMealId = value),
|
||||
),
|
||||
if (ingredientIdController.text.isNotEmpty &&
|
||||
_amountController.text.isNotEmpty &&
|
||||
_mealItem.ingredient != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Column(
|
||||
children: [
|
||||
Text(
|
||||
'Macros preview', // TODO fix l10n
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
MealItemValuesTile(
|
||||
ingredient: _mealItem.ingredient!,
|
||||
nutritionalValues: _mealItem.nutritionalValues,
|
||||
Expanded(
|
||||
child: TimeInputWidget(
|
||||
key: const Key('field-time'),
|
||||
value: _time,
|
||||
labelText: i18n.time,
|
||||
onChanged: (time) => _time = time,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (widget.meals.isNotEmpty)
|
||||
DropdownButtonFormField<String?>(
|
||||
key: const Key('field-meal'),
|
||||
initialValue: _selectedMealId,
|
||||
decoration: InputDecoration(labelText: i18n.meal),
|
||||
items: [
|
||||
DropdownMenuItem<String?>(
|
||||
value: null,
|
||||
child: Text(i18n.otherLogs),
|
||||
),
|
||||
...widget.meals.map(
|
||||
(meal) => DropdownMenuItem<String?>(
|
||||
value: meal.id,
|
||||
child: Text(
|
||||
meal.name.isNotEmpty ? meal.name : meal.time?.format(context) ?? '',
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
onChanged: (value) => setState(() => _selectedMealId = value),
|
||||
),
|
||||
FormSubmitButton(
|
||||
key: const Key(SUBMIT_BUTTON_KEY_NAME),
|
||||
label: AppLocalizations.of(context).save,
|
||||
onPressed: () async {
|
||||
if (!_form.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
_form.currentState!.save();
|
||||
_mealItem.ingredientId = int.parse(_ingredientIdController.text);
|
||||
|
||||
final loggedDate = DateTime(
|
||||
_date.year,
|
||||
_date.month,
|
||||
_date.day,
|
||||
_time.hour,
|
||||
_time.minute,
|
||||
);
|
||||
widget.onSave(context, ref, _mealItem, loggedDate, _selectedMealId);
|
||||
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
FormSubmitButton(
|
||||
key: const Key(SUBMIT_BUTTON_KEY_NAME),
|
||||
label: AppLocalizations.of(context).save,
|
||||
onPressed: () async {
|
||||
if (!_form.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
_form.currentState!.save();
|
||||
_mealItem.ingredientId = int.parse(_ingredientIdController.text);
|
||||
|
||||
final loggedDate = DateTime(
|
||||
_date.year,
|
||||
_date.month,
|
||||
_date.day,
|
||||
_time.hour,
|
||||
_time.minute,
|
||||
);
|
||||
widget.onSave(context, ref, _mealItem, loggedDate, _selectedMealId);
|
||||
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
),
|
||||
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(
|
||||
if (suggestions.isNotEmpty) const SizedBox(height: 10.0),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
child: Text(AppLocalizations.of(context).recentlyUsedIngredients),
|
||||
),
|
||||
ListView.builder(
|
||||
itemCount: suggestions.length,
|
||||
shrinkWrap: true,
|
||||
physics: const NeverScrollableScrollPhysics(),
|
||||
itemBuilder: (context, index) {
|
||||
// ingredient is non-null here, the suggestions list above
|
||||
// filters out items whose ingredient hasn't been hydrated.
|
||||
@@ -463,14 +518,102 @@ class IngredientFormState extends ConsumerState<IngredientForm> {
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Energy of an entry and how it splits into the macronutrients.
|
||||
class _MacrosSummary extends StatelessWidget {
|
||||
const _MacrosSummary({super.key, required this.values, this.grams});
|
||||
|
||||
final NutritionalValues values;
|
||||
|
||||
/// Weight in grams, shown when the amount is entered in another unit
|
||||
final num? grams;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final i18n = AppLocalizations.of(context);
|
||||
final theme = Theme.of(context);
|
||||
// Same color mapping as the macro pie chart
|
||||
final [carbsColor, proteinColor, fatColor] = chartColorPalette(3, theme.colorScheme);
|
||||
final macros = [
|
||||
(i18n.protein, values.protein, values.protein * ENERGY_PROTEIN, proteinColor),
|
||||
(
|
||||
i18n.carbohydrates,
|
||||
values.carbohydrates,
|
||||
values.carbohydrates * ENERGY_CARBOHYDRATES,
|
||||
carbsColor,
|
||||
),
|
||||
(i18n.fat, values.fat, values.fat * ENERGY_FAT, fatColor),
|
||||
];
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
spacing: 8,
|
||||
children: [
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.baseline,
|
||||
textBaseline: TextBaseline.alphabetic,
|
||||
spacing: 6,
|
||||
children: [
|
||||
Text(values.energy.toStringAsFixed(0), style: theme.textTheme.headlineSmall),
|
||||
Text(i18n.kcal, style: theme.textTheme.bodyMedium),
|
||||
if (grams != null)
|
||||
Text(
|
||||
'· ${i18n.gValue(grams!.toStringAsFixed(0))}',
|
||||
style: theme.textTheme.bodyMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
ClipRRect(
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
child: SizedBox(
|
||||
height: 8,
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
spacing: 2,
|
||||
children: [
|
||||
for (final (_, _, energy, color) in macros)
|
||||
if ((energy * 10).round() > 0)
|
||||
Expanded(
|
||||
flex: (energy * 10).round(),
|
||||
child: ColoredBox(color: color),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Wrap(
|
||||
spacing: 14,
|
||||
runSpacing: 4,
|
||||
children: [
|
||||
for (final (label, amount, _, color) in macros)
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
spacing: 6,
|
||||
children: [
|
||||
DecoratedBox(
|
||||
decoration: BoxDecoration(color: color, shape: BoxShape.circle),
|
||||
child: const SizedBox.square(dimension: 10),
|
||||
),
|
||||
Text(
|
||||
'$label ${i18n.gValue(amount.toStringAsFixed(0))}',
|
||||
style: theme.textTheme.bodySmall,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
enum GoalType {
|
||||
meals('From meals'),
|
||||
basic('Basic'),
|
||||
|
||||
@@ -31,6 +31,7 @@ import 'package:shared_preferences_platform_interface/in_memory_shared_preferenc
|
||||
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
|
||||
import 'package:wger/core/consts.dart';
|
||||
import 'package:wger/features/nutrition/models/ingredient.dart';
|
||||
import 'package:wger/features/nutrition/models/ingredient_weight_unit.dart';
|
||||
import 'package:wger/features/nutrition/models/log.dart';
|
||||
import 'package:wger/features/nutrition/models/meal.dart';
|
||||
import 'package:wger/features/nutrition/models/meal_item.dart';
|
||||
@@ -224,6 +225,17 @@ void main() {
|
||||
});
|
||||
|
||||
group('Test logging a single ingredient', () {
|
||||
Ingredient ingredientWithUnits() => Ingredient.fromJson(ingredient.toJson())
|
||||
..weightUnits = [
|
||||
IngredientWeightUnit(
|
||||
id: 1,
|
||||
uuid: 'cc000000-0000-4000-8000-000000000001',
|
||||
ingredientId: ingredient.id,
|
||||
name: 'cup, loosely packed',
|
||||
grams: 158,
|
||||
),
|
||||
];
|
||||
|
||||
testWidgets('the meal can be selected and is logged with the entry', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
@@ -276,6 +288,125 @@ void main() {
|
||||
final log = verify(mockRepo.addLogLocalDrift(captureAny)).captured.single as LogItem;
|
||||
expect(log.mealId, isNull);
|
||||
});
|
||||
|
||||
testWidgets('weight stays editable on a phone with a long weight unit name', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1170, 2532);
|
||||
tester.view.devicePixelRatio = 3.0;
|
||||
addTearDown(tester.view.reset);
|
||||
|
||||
final withUnits = ingredientWithUnits();
|
||||
|
||||
await tester.pumpWidget(createIngredientLogFormScreen(plan1));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final IngredientFormState formState = tester.state(find.byType(IngredientForm));
|
||||
formState.selectIngredient(withUnits, 142);
|
||||
await mockNetworkImagesFor(() => tester.pumpAndSettle());
|
||||
|
||||
// Tapping the field must focus the input, not open the unit menu
|
||||
await tester.tap(find.byKey(const Key('field-weight')));
|
||||
await tester.pumpAndSettle();
|
||||
final input = find.descendant(
|
||||
of: find.byKey(const Key('field-weight')),
|
||||
matching: find.byType(EditableText),
|
||||
);
|
||||
expect(tester.widget<EditableText>(input).focusNode.hasFocus, isTrue);
|
||||
|
||||
await tester.enterText(find.byKey(const Key('field-weight')), '80');
|
||||
await tester.pump();
|
||||
expect(formState.mealItem.amount, 80);
|
||||
});
|
||||
|
||||
testWidgets('the form scrolls instead of overflowing with the keyboard open', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1170, 2532);
|
||||
tester.view.devicePixelRatio = 3.0;
|
||||
tester.view.viewInsets = const FakeViewPadding(bottom: 336 * 3);
|
||||
addTearDown(tester.view.reset);
|
||||
|
||||
await tester.pumpWidget(createIngredientLogFormScreen(plan1));
|
||||
await tester.pumpAndSettle();
|
||||
final IngredientFormState formState = tester.state(find.byType(IngredientForm));
|
||||
formState.selectIngredient(ingredientWithUnits(), 2);
|
||||
await mockNetworkImagesFor(() => tester.pumpAndSettle());
|
||||
|
||||
// A RenderFlex overflow fails the test on its own, the save button
|
||||
// must still be reachable by scrolling.
|
||||
await tester.scrollUntilVisible(
|
||||
find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)),
|
||||
100,
|
||||
scrollable: find
|
||||
.descendant(of: find.byType(IngredientForm), matching: find.byType(Scrollable))
|
||||
.first,
|
||||
);
|
||||
expect(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME)).hitTestable(), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('a unit chip sets the weight unit, the gram chip clears it', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1170, 2532);
|
||||
tester.view.devicePixelRatio = 3.0;
|
||||
addTearDown(tester.view.reset);
|
||||
|
||||
await tester.pumpWidget(createIngredientLogFormScreen(plan1));
|
||||
await tester.pumpAndSettle();
|
||||
final IngredientFormState formState = tester.state(find.byType(IngredientForm));
|
||||
formState.selectIngredient(ingredientWithUnits(), 2);
|
||||
await mockNetworkImagesFor(() => tester.pumpAndSettle());
|
||||
|
||||
await tester.tap(find.widgetWithText(ChoiceChip, 'cup, loosely packed (158g)'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(formState.mealItem.weightUnitId, 1);
|
||||
expect(formState.mealItem.weightUnitObj?.grams, 158);
|
||||
expect(find.text('× cup, loosely packed'), findsOneWidget);
|
||||
final summary = find.byKey(const Key('macros-summary'));
|
||||
Finder inSummary(String text) => find.descendant(of: summary, matching: find.text(text));
|
||||
expect(inSummary('· 316 g'), findsOneWidget);
|
||||
expect(inSummary('1580'), findsOneWidget);
|
||||
expect(inSummary('Protein 16 g'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.widgetWithText(ChoiceChip, 'g'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(formState.mealItem.weightUnitId, isNull);
|
||||
expect(formState.mealItem.weightUnitObj, isNull);
|
||||
expect(inSummary('· 316 g'), findsNothing);
|
||||
expect(inSummary('10'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('clearing the selected ingredient brings back the search', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
tester.view.physicalSize = const Size(1170, 2532);
|
||||
tester.view.devicePixelRatio = 3.0;
|
||||
addTearDown(tester.view.reset);
|
||||
|
||||
await tester.pumpWidget(createIngredientLogFormScreen(plan1));
|
||||
await tester.pumpAndSettle();
|
||||
final IngredientFormState formState = tester.state(find.byType(IngredientForm));
|
||||
formState.selectIngredient(ingredientWithUnits(), 2);
|
||||
await mockNetworkImagesFor(() => tester.pumpAndSettle());
|
||||
await tester.tap(find.widgetWithText(ChoiceChip, 'cup, loosely packed (158g)'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('selected-ingredient')), findsOneWidget);
|
||||
expect(find.byType(TypeAheadField<Ingredient>), findsNothing);
|
||||
|
||||
await tester.tap(find.byKey(const Key('clear-ingredient')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.byKey(const Key('selected-ingredient')), findsNothing);
|
||||
expect(find.byType(TypeAheadField<Ingredient>), findsOneWidget);
|
||||
expect(find.byType(ChoiceChip), findsNothing);
|
||||
expect(formState.ingredientIdController.text, isEmpty);
|
||||
expect(formState.mealItem.ingredient, isNull);
|
||||
expect(formState.mealItem.weightUnitObj, isNull);
|
||||
// The typed amount survives picking a different ingredient
|
||||
expect(formState.mealItem.amount, 2);
|
||||
});
|
||||
});
|
||||
|
||||
group('Test the AlertDialogs for scanning result', () {
|
||||
|
||||
Reference in New Issue
Block a user