From 1256e9fe6dc7ca4a8c60ae790d3e3d66cb36d00e Mon Sep 17 00:00:00 2001 From: enrique-lozano Date: Sat, 9 Nov 2024 16:11:35 +0100 Subject: [PATCH] Allow more decimals in the exchange rate form --- lib/app/accounts/account_form.dart | 2 +- lib/app/currencies/exchange_rate_form.dart | 2 +- .../form/transaction_form.page.dart | 2 +- .../filter_sheet_modal.dart | 4 ++-- lib/core/utils/text_field_utils.dart | 19 ++++++++++++++----- 5 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/app/accounts/account_form.dart b/lib/app/accounts/account_form.dart index c3eeebea..e6229210 100644 --- a/lib/app/accounts/account_form.dart +++ b/lib/app/accounts/account_form.dart @@ -284,7 +284,7 @@ class _AccountFormPageState extends State { keyboardType: TextInputType.number, enabled: !(widget.account != null && widget.account!.isClosed), - inputFormatters: decimalDigitFormatter, + inputFormatters: twoDecimalDigitFormatter, validator: (value) => fieldValidator(value, validator: ValidatorType.double, isRequired: true), autovalidateMode: AutovalidateMode.onUserInteraction, diff --git a/lib/app/currencies/exchange_rate_form.dart b/lib/app/currencies/exchange_rate_form.dart index 2c03de9d..2a4da55c 100644 --- a/lib/app/currencies/exchange_rate_form.dart +++ b/lib/app/currencies/exchange_rate_form.dart @@ -207,7 +207,7 @@ class _ExchangeRateFormDialogState extends State { }, autovalidateMode: AutovalidateMode.onUserInteraction, keyboardType: TextInputType.number, - inputFormatters: decimalDigitFormatter, + inputFormatters: decimalDigitFormatter(6), decoration: InputDecoration( labelText: '${t.currencies.exchange_rate} *', hintText: 'Ex.: 2.14', diff --git a/lib/app/transactions/form/transaction_form.page.dart b/lib/app/transactions/form/transaction_form.page.dart index a640fe6a..8b01a7bc 100644 --- a/lib/app/transactions/form/transaction_form.page.dart +++ b/lib/app/transactions/form/transaction_form.page.dart @@ -391,7 +391,7 @@ class _TransactionFormPageState extends State isDense: false, ), keyboardType: TextInputType.number, - inputFormatters: decimalDigitFormatter, + inputFormatters: twoDecimalDigitFormatter, validator: (value) { final defaultNumberValidatorResult = fieldValidator(value, isRequired: false, validator: ValidatorType.double); diff --git a/lib/core/presentation/widgets/transaction_filter/filter_sheet_modal.dart b/lib/core/presentation/widgets/transaction_filter/filter_sheet_modal.dart index 2be517f2..b7dd7750 100644 --- a/lib/core/presentation/widgets/transaction_filter/filter_sheet_modal.dart +++ b/lib/core/presentation/widgets/transaction_filter/filter_sheet_modal.dart @@ -383,7 +383,7 @@ class _FilterSheetModalState extends State { autovalidateMode: AutovalidateMode.onUserInteraction, textInputAction: TextInputAction.next, - inputFormatters: decimalDigitFormatter, + inputFormatters: twoDecimalDigitFormatter, initialValue: (filtersToReturn.minValue ?? 0) .toStringAsFixed(2), onChanged: (value) { @@ -430,7 +430,7 @@ class _FilterSheetModalState extends State { autovalidateMode: AutovalidateMode.onUserInteraction, textInputAction: TextInputAction.next, - inputFormatters: decimalDigitFormatter, + inputFormatters: twoDecimalDigitFormatter, initialValue: filtersToReturn.maxValue?.toStringAsFixed(2), onChanged: (value) { diff --git a/lib/core/utils/text_field_utils.dart b/lib/core/utils/text_field_utils.dart index 4c4b50a6..b7f7fa39 100644 --- a/lib/core/utils/text_field_utils.dart +++ b/lib/core/utils/text_field_utils.dart @@ -1,11 +1,20 @@ import 'package:flutter/services.dart'; import 'package:monekin/i18n/translations.g.dart'; -/// A text input formatter that only allow two decimal places, replacing incorrect decimal symbols -List get decimalDigitFormatter => [ - FilteringTextInputFormatter.deny(',', replacementString: '.'), - FilteringTextInputFormatter.allow(RegExp(r'(^\d*\.?\d{0,2})')), - ]; +/// A text input formatter that allows up to two decimal places, +/// replacing or disabling any invalid symbols +List get twoDecimalDigitFormatter => + decimalDigitFormatter(2); + +/// A text input formatter that allows up to N decimal places, +/// replacing or disabling any invalid symbols +List decimalDigitFormatter(int decimalPlaces) { + return [ + FilteringTextInputFormatter.deny(',', replacementString: '.'), + FilteringTextInputFormatter.allow( + RegExp(r'(^\d*\.?\d{0,' + decimalPlaces.toString() + r'})')), + ]; +} enum ValidatorType { text,