Files
flutter/lib/widgets/core/decimal_input.dart
Roland Geider 848ba17d54 Format number inputs according to locale
Due to some idiosyncrasies of the on-screen keyboards where we can neither control nor
know what separator it shows, and the way intl parses work (in the "de" locale 80.5 is
parsed to 805.0), we need to roll our own
2026-05-21 14:51:05 +02:00

82 lines
2.9 KiB
Dart

/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* 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 <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/number_input.dart';
import 'package:wger/l10n/generated/app_localizations.dart';
/// Locale-aware decimal text field.
///
/// Renders [value] with the active locale's number format and reports edits
/// through [onChanged] as a parsed [num], or null when the field is empty.
/// Display and parsing always go through the same NumberFormat, so a value can
/// never be mis-read because of a decimal-separator mismatch between locales.
class DecimalInputWidget extends StatelessWidget {
const DecimalInputWidget({
required this.value,
required this.onChanged,
required this.labelText,
this.suffixText,
this.isRequired = false,
super.key,
});
/// The current value, or null when unset.
final num? value;
/// Called with the parsed value on every edit, or null when the field is
/// cleared or its contents cannot be parsed.
final ValueChanged<num?> onChanged;
final String labelText;
final String? suffixText;
/// When true, an empty field fails validation instead of being accepted.
final bool isRequired;
@override
Widget build(BuildContext context) {
final i18n = AppLocalizations.of(context);
final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString());
return TextFormField(
initialValue: value == null ? '' : numberFormat.format(value),
decoration: InputDecoration(labelText: labelText, suffixText: suffixText),
keyboardType: textInputTypeDecimal,
inputFormatters: [LocalizedDecimalInputFormatter(numberFormat.symbols.DECIMAL_SEP)],
onChanged: (text) {
final trimmed = text.trim();
onChanged(trimmed.isEmpty ? null : numberFormat.tryParse(trimmed));
},
validator: (text) {
final trimmed = text?.trim() ?? '';
if (trimmed.isEmpty) {
return isRequired ? i18n.enterValue : null;
}
if (numberFormat.tryParse(trimmed) == null) {
return i18n.enterValidNumber;
}
return null;
},
);
}
}