diff --git a/lib/models/currency.dart b/lib/models/currency.dart index f2e8e4fd6..5d4f46eea 100644 --- a/lib/models/currency.dart +++ b/lib/models/currency.dart @@ -62,11 +62,11 @@ class BitcoinCurrency extends Object { RegExp get whitelistedPattern { switch (tickerSymbol) { case "BTC": - return RegExp("^\\d+\\.?\\d{0,8}"); + return RegExp("^\\d+[.,]?\\d{0,8}"); case "SAT": return RegExp(r'\d+'); default: - return RegExp("^\\d+\\.?\\d{0,8}"); + return RegExp("^\\d+[.,]?\\d{0,8}"); } } diff --git a/lib/utils/fiat_conversion.dart b/lib/utils/fiat_conversion.dart index 93cc160d3..4d3c54c22 100644 --- a/lib/utils/fiat_conversion.dart +++ b/lib/utils/fiat_conversion.dart @@ -29,6 +29,10 @@ class FiatConversion { return (fiatAmount / exchangeRate * 100000000).round(); } + RegExp get whitelistedPattern => currencyData.info.fractionSize == 0 + ? RegExp(r'\d+') + : RegExp("^\\d+[.,]?\\d{0,${currencyData.info.fractionSize}}"); + double satToFiat(int satoshies) { return satoshies.toDouble() / 100000000 * exchangeRate; } diff --git a/lib/widgets/amount_form_field/amount_form_field.dart b/lib/widgets/amount_form_field/amount_form_field.dart index 27a2400af..05499e398 100644 --- a/lib/widgets/amount_form_field/amount_form_field.dart +++ b/lib/widgets/amount_form_field/amount_form_field.dart @@ -78,7 +78,14 @@ class AmountFormField extends TextFormField { enabled: enabled, controller: controller, inputFormatters: bitcoinCurrency != BitcoinCurrency.SAT - ? [FilteringTextInputFormatter.allow(RegExp(r'\d+\.?\d*'))] + ? [ + FilteringTextInputFormatter.allow(bitcoinCurrency.whitelistedPattern), + TextInputFormatter.withFunction( + (_, newValue) => newValue.copyWith( + text: newValue.text.replaceAll(',', '.'), + ), + ), + ] : [SatAmountFormFieldFormatter()], onFieldSubmitted: onFieldSubmitted, onSaved: onSaved, diff --git a/lib/widgets/amount_form_field/currency_converter_dialog.dart b/lib/widgets/amount_form_field/currency_converter_dialog.dart index bca429b8a..24980461a 100644 --- a/lib/widgets/amount_form_field/currency_converter_dialog.dart +++ b/lib/widgets/amount_form_field/currency_converter_dialog.dart @@ -177,7 +177,6 @@ class CurrencyConverterDialogState extends State } final fiatConversion = FiatConversion(fiatCurrency, fiatExchangeRate); - final int fractionSize = fiatCurrency.info.fractionSize; final borderColor = themeData.isLightTheme ? Colors.red : themeData.colorScheme.error; return Column( @@ -212,10 +211,14 @@ class CurrencyConverterDialogState extends State style: themeData.dialogTheme.contentTextStyle, ), ), - // Do not allow '.' when fractionSize is 0 and only allow fiat currencies fractionSize number of digits after decimal point inputFormatters: [ FilteringTextInputFormatter.allow( - fractionSize == 0 ? RegExp(r'\d+') : RegExp("^\\d+\\.?\\d{0,$fractionSize}"), + fiatConversion.whitelistedPattern, + ), + TextInputFormatter.withFunction( + (_, newValue) => newValue.copyWith( + text: newValue.text.replaceAll(',', '.'), + ), ), ], keyboardType: const TextInputType.numberWithOptions(decimal: true),