Correctly parse the localized weight values in the form

This commit is contained in:
Roland Geider
2025-08-13 19:54:32 +02:00
parent 63373af884
commit bea40c9a3c
2 changed files with 16 additions and 11 deletions

View File

@@ -22,6 +22,7 @@ import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/json.dart';
import 'package:wger/helpers/misc.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
import 'package:wger/models/body_weight/weight_entry.dart';
import 'package:wger/providers/body_weight.dart';
@@ -35,7 +36,7 @@ class WeightForm extends StatelessWidget {
WeightForm([WeightEntry? weightEntry]) {
_weightEntry = weightEntry ?? WeightEntry(date: DateTime.now());
weightController.text = _weightEntry.weight == 0 ? '' : _weightEntry.weight.toString();
weightController.text = '';
dateController.text = dateToYYYYMMDD(_weightEntry.date)!;
}
@@ -43,6 +44,10 @@ class WeightForm extends StatelessWidget {
Widget build(BuildContext context) {
final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString());
if (weightController.text.isEmpty && _weightEntry.weight != 0) {
weightController.text = numberFormat.format(_weightEntry.weight);
}
return Form(
key: _form,
child: Column(
@@ -69,7 +74,7 @@ class WeightForm extends StatelessWidget {
lastDate: DateTime.now(),
selectableDayPredicate: (day) {
// Always allow the current initial date
if (day == _weightEntry.date) {
if (day.isSameDayAs(_weightEntry.date)) {
return true;
}
@@ -101,8 +106,8 @@ class WeightForm extends StatelessWidget {
icon: const FaIcon(FontAwesomeIcons.circleMinus),
onPressed: () {
try {
final num newValue = num.parse(weightController.text) - 1;
weightController.text = newValue.toString();
final newValue = numberFormat.parse(weightController.text) - 1;
weightController.text = numberFormat.format(newValue);
} on FormatException {}
},
),
@@ -111,8 +116,8 @@ class WeightForm extends StatelessWidget {
icon: const FaIcon(FontAwesomeIcons.minus),
onPressed: () {
try {
final num newValue = num.parse(weightController.text) - 0.1;
weightController.text = newValue.toStringAsFixed(1);
final newValue = numberFormat.parse(weightController.text) - 0.1;
weightController.text = numberFormat.format(newValue);
} on FormatException {}
},
),
@@ -126,8 +131,8 @@ class WeightForm extends StatelessWidget {
icon: const FaIcon(FontAwesomeIcons.plus),
onPressed: () {
try {
final num newValue = num.parse(weightController.text) + 0.1;
weightController.text = newValue.toStringAsFixed(1);
final newValue = numberFormat.parse(weightController.text) + 0.1;
weightController.text = numberFormat.format(newValue);
} on FormatException {}
},
),
@@ -136,8 +141,8 @@ class WeightForm extends StatelessWidget {
icon: const FaIcon(FontAwesomeIcons.circlePlus),
onPressed: () {
try {
final num newValue = num.parse(weightController.text) + 1;
weightController.text = newValue.toString();
final newValue = numberFormat.parse(weightController.text) + 1;
weightController.text = numberFormat.format(newValue);
} on FormatException {}
},
),

View File

@@ -56,7 +56,7 @@ void main() {
expect(find.text('79.9'), findsOneWidget);
await tester.tap(find.byKey(const Key('quickPlusSmall')));
expect(find.text('80.0'), findsOneWidget);
expect(find.text('80'), findsOneWidget);
});
testWidgets("Entering garbage doesn't break the quick-change", (WidgetTester tester) async {