Skip to content

Commit

Permalink
Fixed #87
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyryan committed Aug 20, 2022
1 parent aed514d commit e08e3e9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions example/lib/pages/money_format_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {
inputFormatters: [
CurrencyInputFormatter(
trailingSymbol: CurrencySymbols.EURO_SIGN,
mantissaLength: 0,
)
],
),
Expand Down
2 changes: 1 addition & 1 deletion lib/flutter_multi_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
9 changes: 6 additions & 3 deletions lib/formatters/currency_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -335,6 +335,9 @@ class CurrencyInputFormatter extends TextInputFormatter {
required String oldText,
required int oldCaretOffset,
}) {
if (mantissaLength < 1) {
return 0;
}
final mantissaIndex = oldText.lastIndexOf(
_mantissaSeparatorRegexp,
);
Expand Down
22 changes: 12 additions & 10 deletions lib/formatters/formatter_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ String toNumericString(
}
try {
if (allowPeriod) {
// return double.parse(result).toString();
return _toDoubleString(
result,
allowPeriod: true,
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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)}';
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit e08e3e9

Please sign in to comment.