Skip to content

Commit

Permalink
Merge pull request #239 from enrique-lozano/hot-fixes
Browse files Browse the repository at this point in the history
Only two decimls were allowed in the exchange rate form
  • Loading branch information
enrique-lozano authored Nov 9, 2024
2 parents e938be1 + 1256e9f commit c8868df
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/app/accounts/account_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class _AccountFormPageState extends State<AccountFormPage> {
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,
Expand Down
2 changes: 1 addition & 1 deletion lib/app/currencies/exchange_rate_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class _ExchangeRateFormDialogState extends State<ExchangeRateFormDialog> {
},
autovalidateMode: AutovalidateMode.onUserInteraction,
keyboardType: TextInputType.number,
inputFormatters: decimalDigitFormatter,
inputFormatters: decimalDigitFormatter(6),
decoration: InputDecoration(
labelText: '${t.currencies.exchange_rate} *',
hintText: 'Ex.: 2.14',
Expand Down
2 changes: 1 addition & 1 deletion lib/app/transactions/form/transaction_form.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class _TransactionFormPageState extends State<TransactionFormPage>
isDense: false,
),
keyboardType: TextInputType.number,
inputFormatters: decimalDigitFormatter,
inputFormatters: twoDecimalDigitFormatter,
validator: (value) {
final defaultNumberValidatorResult = fieldValidator(value,
isRequired: false, validator: ValidatorType.double);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ class _FilterSheetModalState extends State<FilterSheetModal> {
autovalidateMode:
AutovalidateMode.onUserInteraction,
textInputAction: TextInputAction.next,
inputFormatters: decimalDigitFormatter,
inputFormatters: twoDecimalDigitFormatter,
initialValue: (filtersToReturn.minValue ?? 0)
.toStringAsFixed(2),
onChanged: (value) {
Expand Down Expand Up @@ -430,7 +430,7 @@ class _FilterSheetModalState extends State<FilterSheetModal> {
autovalidateMode:
AutovalidateMode.onUserInteraction,
textInputAction: TextInputAction.next,
inputFormatters: decimalDigitFormatter,
inputFormatters: twoDecimalDigitFormatter,
initialValue:
filtersToReturn.maxValue?.toStringAsFixed(2),
onChanged: (value) {
Expand Down
19 changes: 14 additions & 5 deletions lib/core/utils/text_field_utils.dart
Original file line number Diff line number Diff line change
@@ -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<FilteringTextInputFormatter> 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<FilteringTextInputFormatter> get twoDecimalDigitFormatter =>
decimalDigitFormatter(2);

/// A text input formatter that allows up to N decimal places,
/// replacing or disabling any invalid symbols
List<FilteringTextInputFormatter> decimalDigitFormatter(int decimalPlaces) {
return [
FilteringTextInputFormatter.deny(',', replacementString: '.'),
FilteringTextInputFormatter.allow(
RegExp(r'(^\d*\.?\d{0,' + decimalPlaces.toString() + r'})')),
];
}

enum ValidatorType {
text,
Expand Down

0 comments on commit c8868df

Please sign in to comment.