Skip to content

Commit

Permalink
some formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyryan committed Jun 5, 2021
1 parent 160016b commit 7180bc0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 4 additions & 2 deletions example/lib/pages/money_format_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {
inputFormatters: [
MoneyInputFormatter(
leadingSymbol: MoneySymbols.DOLLAR_SIGN,
thousandSeparator: ThousandSeparator.SpaceAndPeriodMantissa,
thousandSeparator:
ThousandSeparator.SpaceAndPeriodMantissa,
// thousandSeparator: ThousandSeparator.None,
useSymbolPadding: true,
)
Expand Down Expand Up @@ -171,7 +172,8 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {
MoneyInputFormatter(
leadingSymbol: MoneySymbols.DOLLAR_SIGN,
useSymbolPadding: true,
thousandSeparator: ThousandSeparator.SpaceAndPeriodMantissa,
thousandSeparator:
ThousandSeparator.SpaceAndPeriodMantissa,
// ThousandSeparator.Comma,
)
],
Expand Down
21 changes: 14 additions & 7 deletions lib/formatters/money_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ class MoneyInputFormatter extends TextInputFormatter {
/// the number is different add this number to the selection offset
var oldSelectionEnd = oldValue.selection.end;
TextEditingValue value = oldSelectionEnd > -1 ? oldValue : newValue;
String oldSubstrBeforeSelection =
oldSelectionEnd > -1 ? value.text.substring(0, value.selection.end) : '';
String oldSubstrBeforeSelection = oldSelectionEnd > -1
? value.text.substring(0, value.selection.end)
: '';
int numThousandSeparatorsInOldSub = _countSymbolsInString(
oldSubstrBeforeSelection,
',',
Expand Down Expand Up @@ -387,6 +388,7 @@ class MoneyInputFormatter extends TextInputFormatter {
wholePartSubStart = _leadingLength;
selectionIndex += _leadingLength;
}

/// The rare case when a string starts with 0 and no
/// mantissa separator after
selectionIndex -= numZeroesRemovedAtStringStart;
Expand All @@ -398,7 +400,8 @@ class MoneyInputFormatter extends TextInputFormatter {
mantissaIndex,
);
if (selectionIndex < mantissaIndex) {
if (wholePartSubstring == '0' || wholePartSubstring == '${leadingSymbol}0') {
if (wholePartSubstring == '0' ||
wholePartSubstring == '${leadingSymbol}0') {
/// if the whole part contains 0 only, then we need
/// to bring the selection after the
/// fractional part right away
Expand Down Expand Up @@ -557,7 +560,8 @@ 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 @@ -622,8 +626,9 @@ 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 @@ -641,7 +646,9 @@ 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
1 change: 0 additions & 1 deletion lib/formatters/phone_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import 'package:flutter/widgets.dart';
import 'formatter_utils.dart';
import 'phone_input_enums.dart';


class PhoneInputFormatter extends TextInputFormatter {
final ValueChanged<PhoneCountryData?>? onCountrySelected;
final bool allowEndlessPhone;
Expand Down

0 comments on commit 7180bc0

Please sign in to comment.