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

View File

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