From dee6c12ceacaadb2db43908bf55fa7d41da03398 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 24 Feb 2026 21:04:29 +0100 Subject: [PATCH] Fixed i18n and added a max allowed weight as well (but this should probably be done everywhere) --- lib/widgets/nutrition/forms.dart | 21 +++++---- .../nutritional_meal_item_form_test.dart | 43 +++++++++---------- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/lib/widgets/nutrition/forms.dart b/lib/widgets/nutrition/forms.dart index 01de25110..3b30dd811 100644 --- a/lib/widgets/nutrition/forms.dart +++ b/lib/widgets/nutrition/forms.dart @@ -231,6 +231,7 @@ class IngredientFormState extends State { final dateFormat = DateFormat.yMd(languageCode); final timeFormat = DateFormat.Hm(languageCode); final dateTimeFormat = DateFormat.yMd(languageCode).add_Hm(); + final i18n = AppLocalizations.of(context); if (_dateController.text.isEmpty) { _dateController.text = dateFormat.format(DateTime.now()); @@ -240,7 +241,7 @@ class IngredientFormState extends State { _timeController.text = timeFormat.format(DateTime.now()); } - final String unit = AppLocalizations.of(context).g; + final String unit = i18n.g; final queryLower = _searchQuery.toLowerCase(); final suggestions = widget.recent .where((e) => e.ingredient.name.toLowerCase().contains(queryLower)) @@ -268,9 +269,7 @@ class IngredientFormState extends State { child: TextFormField( key: const Key('field-weight'), // needed ? - decoration: InputDecoration( - labelText: AppLocalizations.of(context).weight, - ), + decoration: InputDecoration(labelText: i18n.weight), controller: _amountController, keyboardType: textInputTypeDecimal, onChanged: (value) { @@ -287,15 +286,15 @@ class IngredientFormState extends State { validator: (value) { final text = value?.trim() ?? ''; if (text.isEmpty) { - return 'Please enter a weight.'; + return i18n.enterValue; } final parsed = double.tryParse(text); if (parsed == null) { - return AppLocalizations.of(context).enterValidNumber; + return i18n.enterValidNumber; } - // Must be at least 1 - if (parsed < 1) { - return 'Weight must be at least 1.'; + + if (parsed < 1 || parsed > 1000) { + return i18n.formMinMaxValues(1, 1000); } return null; }, @@ -307,7 +306,7 @@ class IngredientFormState extends State { readOnly: true, // Stop keyboard from appearing decoration: InputDecoration( - labelText: AppLocalizations.of(context).date, + labelText: i18n.date, // suffixIcon: const Icon(Icons.calendar_today), ), enableInteractiveSelection: false, @@ -335,7 +334,7 @@ class IngredientFormState extends State { child: TextFormField( key: const Key('field-time'), decoration: InputDecoration( - labelText: AppLocalizations.of(context).time, + labelText: i18n.time, //suffixIcon: const Icon(Icons.punch_clock) ), controller: _timeController, diff --git a/test/nutrition/nutritional_meal_item_form_test.dart b/test/nutrition/nutritional_meal_item_form_test.dart index 6531e1454..4683805b9 100644 --- a/test/nutrition/nutritional_meal_item_form_test.dart +++ b/test/nutrition/nutritional_meal_item_form_test.dart @@ -1,3 +1,21 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 - 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + import 'dart:convert'; import 'package:flutter/material.dart'; @@ -186,7 +204,7 @@ void main() { await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME))); await tester.pumpAndSettle(); - expect(find.text('Please enter a valid number'), findsOneWidget); + expect(find.text('Please enter a value'), findsOneWidget); }); testWidgets('add correct weight type', (WidgetTester tester) async { @@ -253,8 +271,7 @@ void main() { await tester.pumpAndSettle(); expect(find.text('Please select an ingredient'), findsOneWidget); - - expect(find.text('Please enter a valid number'), findsOneWidget); + expect(find.text('Please enter a value'), findsOneWidget); }); testWidgets('save ingredient without weight', (WidgetTester tester) async { @@ -273,25 +290,7 @@ void main() { await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME))); await tester.pumpAndSettle(); - expect(find.text('Please enter a valid number'), findsOneWidget); - }); - - //TODO: isn't this test just a duplicate of the above one? can be removed? - testWidgets('save ingredient with incorrect weight input type', (WidgetTester tester) async { - await tester.pumpWidget(createMealItemFormScreen(meal1, '123', true)); - - await tester.tap(find.byKey(const Key('scan-button'))); - await tester.pumpAndSettle(); - - expect(find.byKey(const Key('ingredient-scan-result-dialog')), findsOneWidget); - - await tester.tap(find.byKey(const Key('ingredient-scan-result-dialog-confirm-button'))); - await tester.pumpAndSettle(); - - await tester.tap(find.byKey(const Key(SUBMIT_BUTTON_KEY_NAME))); - await tester.pumpAndSettle(); - - expect(find.text('Please enter a valid number'), findsOneWidget); + expect(find.text('Please enter a value'), findsOneWidget); }); testWidgets(