Skip to content

Commit

Permalink
Fixed #92
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyryan committed Sep 7, 2022
1 parent 2e42b99 commit b4a6a72
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [2.7.4]
- Removed "borderRadius" parameter from CountryDropdown to make it compatible with some older Flutter versions
- Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/92
## [2.7.2]
- Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/93
## [2.7.1]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ PhoneInputFormatter(
)
```
## Using a pre-defined country code

<img src="https://github.com/caseyryan/images/blob/master/multi_formatter/phone_country_dropdown.gif?raw=true" width="240"/>

```dart
PhoneCountryData? _initialCountryData;
...
Expand Down
17 changes: 8 additions & 9 deletions example/lib/pages/money_format_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {
),
inputFormatters: [
CurrencyInputFormatter(
trailingSymbol: '',
thousandSeparator: ThousandSeparator.Period,
mantissaLength: 0,
// trailingSymbol: '',
// thousandSeparator: ThousandSeparator.Period,
// mantissaLength: 0,
thousandSeparator: ThousandSeparator.Space, mantissaLength: 0,
trailingSymbol: "\$",
)
],
),
Expand Down Expand Up @@ -310,8 +312,7 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {
CurrencyInputFormatter(
leadingSymbol: CurrencySymbols.DOLLAR_SIGN,
useSymbolPadding: true,
thousandSeparator:
ThousandSeparator.SpaceAndPeriodMantissa,
thousandSeparator: ThousandSeparator.SpaceAndPeriodMantissa,
// ThousandSeparator.Comma,
)
],
Expand All @@ -336,8 +337,7 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {
),
inputFormatters: [
CurrencyInputFormatter(
thousandSeparator:
ThousandSeparator.SpaceAndPeriodMantissa,
thousandSeparator: ThousandSeparator.SpaceAndPeriodMantissa,
trailingSymbol: ' USD',
// ThousandSeparator.Comma,
)
Expand All @@ -363,8 +363,7 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {
),
inputFormatters: [
CurrencyInputFormatter(
thousandSeparator:
ThousandSeparator.SpaceAndCommaMantissa,
thousandSeparator: ThousandSeparator.SpaceAndCommaMantissa,
trailingSymbol: ' U',
// ThousandSeparator.Comma,
)
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.6.1"
version: "2.7.2"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
39 changes: 26 additions & 13 deletions lib/formatters/currency_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,20 @@ class CurrencyInputFormatter extends TextInputFormatter {

var oldText = oldValue.text;
if (oldValue == newValue) {
// print('RETURN 0 ${oldValue.text}');
return newValue;
}
bool isErasing = newText.length < oldText.length;
if (isErasing) {
if (mantissaLength == 0 && oldCaretIndex == oldValue.text.length) {
if (trailingLength > 0) {
// print('RETURN 1 ${oldValue.text}');
return oldValue.copyWith(
selection: TextSelection.collapsed(
offset: oldCaretIndex - trailingLength,
offset: min(
oldValue.text.length,
oldCaretIndex - trailingLength,
),
),
);
}
Expand All @@ -154,16 +159,19 @@ class CurrencyInputFormatter extends TextInputFormatter {
shorterString: newText,
longerString: oldText,
)) {
print('RETURN 2 ${oldValue.text}');
// print('RETURN 2 ${oldValue.text}');
return oldValue.copyWith(
selection: TextSelection.collapsed(
offset: oldCaretIndex - 1,
offset: min(
oldValue.text.length,
oldCaretIndex - 1,
),
),
);
}
} else {
if (_containsIllegalChars(newText)) {
print('RETURN 3 ${oldValue.text}');
// print('RETURN 3 ${oldValue.text}');
return oldValue;
}
}
Expand All @@ -186,10 +194,13 @@ class CurrencyInputFormatter extends TextInputFormatter {
newText: newText,
oldText: oldText,
)) {
print('RETURN 4 ${oldValue.text}');
// print('RETURN 4 ${oldValue.text.length} $oldCaretIndex');
return oldValue.copyWith(
selection: TextSelection.collapsed(
offset: oldCaretIndex + 1,
offset: min(
oldValue.text.length,
oldCaretIndex + 1,
),
),
);
}
Expand All @@ -200,15 +211,15 @@ class CurrencyInputFormatter extends TextInputFormatter {
oldText: oldText,
caretPosition: newCaretIndex,
)) {
print('RETURN 5 $newAsCurrency');
// print('RETURN 5 $newAsCurrency');
return TextEditingValue(
selection: TextSelection.collapsed(
offset: newCaretIndex,
),
text: newAsCurrency,
);
} else {
print('RETURN 6 $newAsCurrency');
// print('RETURN 6 $newAsCurrency');
int offset = min(
newCaretIndex,
newAsCurrency.length - trailingLength,
Expand All @@ -224,11 +235,14 @@ class CurrencyInputFormatter extends TextInputFormatter {

var initialCaretOffset = leadingLength;
if (_isZeroOrEmpty(newAsNumeric)) {
print('RETURN 7 ${newValue.text}');
// print('RETURN 7 ${newValue.text}');
return newValue.copyWith(
text: newAsCurrency,
selection: TextSelection.collapsed(
offset: initialCaretOffset + 1,
offset: min(
newValue.text.length,
initialCaretOffset + 1,
),
),
);
}
Expand All @@ -253,7 +267,7 @@ class CurrencyInputFormatter extends TextInputFormatter {
initialCaretOffset += 1;
}
}
print('RETURN 8 $newAsCurrency');
// print('RETURN 8 $newAsCurrency');
return TextEditingValue(
selection: TextSelection.collapsed(
offset: initialCaretOffset,
Expand Down Expand Up @@ -351,8 +365,7 @@ class CurrencyInputFormatter extends TextInputFormatter {
var nextChar = '';
if (caretPosition < newText.length - 1) {
nextChar = newText[caretPosition];
if (!isDigit(nextChar, positiveOnly: true) ||
int.tryParse(nextChar) == 0) {
if (!isDigit(nextChar, positiveOnly: true) || int.tryParse(nextChar) == 0) {
return true;
}
}
Expand Down
3 changes: 0 additions & 3 deletions lib/widgets/country_dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class CountryDropdown extends StatefulWidget {
final double? menuMaxHeight;
final bool? enableFeedback;
final AlignmentGeometry alignment;
final BorderRadius? borderRadius;

/// [selectedItemBuilder] use this if you want to make
/// the selected item look the way you want
Expand Down Expand Up @@ -62,7 +61,6 @@ class CountryDropdown extends StatefulWidget {
this.menuMaxHeight,
this.enableFeedback,
this.alignment = AlignmentDirectional.centerStart,
this.borderRadius,
}) : super(key: key);

@override
Expand Down Expand Up @@ -173,7 +171,6 @@ class _CountryDropdownState extends State<CountryDropdown> {
autovalidateMode: widget.autovalidateMode,
menuMaxHeight: widget.menuMaxHeight,
enableFeedback: widget.enableFeedback,
borderRadius: widget.borderRadius,
icon: widget.icon,
isExpanded: true,
elevation: widget.elevation,
Expand Down

0 comments on commit b4a6a72

Please sign in to comment.