From e08e3e995b6dd65163534128f0815413159189e5 Mon Sep 17 00:00:00 2001 From: Konstantin Serov Date: Sat, 20 Aug 2022 19:24:19 +0700 Subject: [PATCH] Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/87 --- CHANGELOG.md | 2 ++ example/lib/pages/money_format_page.dart | 1 + lib/flutter_multi_formatter.dart | 2 +- lib/formatters/currency_input_formatter.dart | 9 +++++--- lib/formatters/formatter_utils.dart | 22 +++++++++++--------- pubspec.yaml | 2 +- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46d591c..01b2d60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +## [2.6.1] +- Fixed currency input formatter empty value error https://github.com/caseyryan/flutter_multi_formatter/issues/87 ## [2.6.0] - Made it possible to enter a leading plus https://github.com/caseyryan/flutter_multi_formatter/issues/85 - Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/80 diff --git a/example/lib/pages/money_format_page.dart b/example/lib/pages/money_format_page.dart index a268089..8e8bf09 100644 --- a/example/lib/pages/money_format_page.dart +++ b/example/lib/pages/money_format_page.dart @@ -106,6 +106,7 @@ class _MoneyFormatPageState extends State { inputFormatters: [ CurrencyInputFormatter( trailingSymbol: CurrencySymbols.EURO_SIGN, + mantissaLength: 0, ) ], ), diff --git a/lib/flutter_multi_formatter.dart b/lib/flutter_multi_formatter.dart index a524d45..83b710e 100644 --- a/lib/flutter_multi_formatter.dart +++ b/lib/flutter_multi_formatter.dart @@ -41,4 +41,4 @@ export 'formatters/pos_input_formatter.dart'; export 'utils/bitcoin_validator/bitcoin_validator.dart'; export 'utils/bitcoin_validator/bitcoin_wallet_details.dart'; export 'utils/enum_utils.dart'; -export 'utils/unfocuser.dart'; +export 'utils/unfocuser.dart'; \ No newline at end of file diff --git a/lib/formatters/currency_input_formatter.dart b/lib/formatters/currency_input_formatter.dart index c24e9ee..8e2f786 100644 --- a/lib/formatters/currency_input_formatter.dart +++ b/lib/formatters/currency_input_formatter.dart @@ -103,8 +103,8 @@ class CurrencyInputFormatter extends TextInputFormatter { ) { final trailingLength = _getTrailingLength(); final leadingLength = _getLeadingLength(); - final oldCaretIndex = oldValue.selection.start; - final newCaretIndex = newValue.selection.start; + final oldCaretIndex = max(oldValue.selection.start, oldValue.selection.end); + final newCaretIndex = max(newValue.selection.start, newValue.selection.end); var newText = newValue.text; final newAsNumeric = toNumericString( @@ -124,7 +124,7 @@ class CurrencyInputFormatter extends TextInputFormatter { shorterString: newText, longerString: oldText, )) { - // print('RETURN 2 ${oldValue.text}'); + // print('RETURN 2 ${oldValue.text}, $oldCaretIndex'); return oldValue.copyWith( selection: TextSelection.collapsed( offset: oldCaretIndex - 1, @@ -335,6 +335,9 @@ class CurrencyInputFormatter extends TextInputFormatter { required String oldText, required int oldCaretOffset, }) { + if (mantissaLength < 1) { + return 0; + } final mantissaIndex = oldText.lastIndexOf( _mantissaSeparatorRegexp, ); diff --git a/lib/formatters/formatter_utils.dart b/lib/formatters/formatter_utils.dart index 7082af7..0e2b122 100644 --- a/lib/formatters/formatter_utils.dart +++ b/lib/formatters/formatter_utils.dart @@ -71,7 +71,6 @@ String toNumericString( } try { if (allowPeriod) { - // return double.parse(result).toString(); return _toDoubleString( result, allowPeriod: true, @@ -142,8 +141,15 @@ String _toDoubleString( } else if (temp[0] == period) { temp.insert(0, zero); } + } else { + while (temp.length > 1) { + if (temp.first == zero) { + temp.removeAt(0); + } else { + break; + } + } } - return temp.join(); } @@ -251,8 +257,7 @@ String toCurrencyString( if (isNegative) { var containsMinus = parsed.toString().contains('-'); if (!containsMinus) { - value = - '-${parsed.toStringAsFixed(mantissaLength).replaceFirst('0.', '.')}'; + value = '-${parsed.toStringAsFixed(mantissaLength).replaceFirst('0.', '.')}'; } else { value = '${parsed.toStringAsFixed(mantissaLength)}'; } @@ -317,9 +322,8 @@ String toCurrencyString( } } - mantissa = noShortening - ? _postProcessMantissa(mantissaList.join(''), mantissaLength) - : ''; + mantissa = + noShortening ? _postProcessMantissa(mantissaList.join(''), mantissaLength) : ''; var maxIndex = split.length - 1; if (mantissaSeparatorIndex > 0 && noShortening) { maxIndex = mantissaSeparatorIndex - 1; @@ -337,9 +341,7 @@ String toCurrencyString( } else { if (value.length >= minShorteningLength) { if (!isDigit(split[i])) digitCounter = 1; - if (digitCounter % 3 == 1 && - digitCounter > 1 && - i > (isNegative ? 1 : 0)) { + if (digitCounter % 3 == 1 && digitCounter > 1 && i > (isNegative ? 1 : 0)) { list.add(tSeparator); } } diff --git a/pubspec.yaml b/pubspec.yaml index 5a5083a..12f2c1d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_multi_formatter description: A package of formatters for international phone numbers, credit / debit cards and a masked formatter -version: 2.6.0 +version: 2.6.1 homepage: https://github.com/caseyryan/flutter_multi_formatter environment: