Skip to content

Commit

Permalink
fiexed #97
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyryan committed Oct 20, 2022
1 parent 3160674 commit fe2f925
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## [2.8.2]
- Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/97
## [2.8.1]
- Added a utility method PinyinUtils.simplifyPinyin();
## [2.8.0]
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 @@ -16,12 +16,12 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {

@override
Widget build(BuildContext context) {
print(toCurrencyString(
'33422220',
trailingSymbol: '',
thousandSeparator: ThousandSeparator.Period,
mantissaLength: 0,
));
// print(toCurrencyString(
// 1000.0.toString(),
// trailingSymbol: '',
// thousandSeparator: ThousandSeparator.Period,
// mantissaLength: 0,
// ));
return Unfocuser(
child: Scaffold(
appBar: AppBar(
Expand Down Expand Up @@ -59,11 +59,10 @@ class _MoneyFormatPageState extends State<MoneyFormatPage> {
),
inputFormatters: [
CurrencyInputFormatter(
// trailingSymbol: '',
// thousandSeparator: ThousandSeparator.Period,
// thousandSeparator: ThousandSeparator.Comma,
// mantissaLength: 0,
thousandSeparator: ThousandSeparator.Space,
mantissaLength: 0,
mantissaLength: 2,
trailingSymbol: "\$",
)
],
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.8.1"
version: "2.8.2"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
32 changes: 18 additions & 14 deletions lib/formatters/currency_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class CurrencyInputFormatter extends TextInputFormatter {
final oldCaretIndex = max(oldValue.selection.start, oldValue.selection.end);
final newCaretIndex = max(newValue.selection.start, newValue.selection.end);
var newText = newValue.text;

// print(newText);
final newAsNumeric = toNumericString(
newText,
allowPeriod: true,
Expand All @@ -137,14 +137,14 @@ class CurrencyInputFormatter extends TextInputFormatter {

var oldText = oldValue.text;
if (oldValue == newValue) {
// print('RETURN 0 ${oldValue.text}');
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}');
print('RETURN 1 ${oldValue.text}');
return oldValue.copyWith(
selection: TextSelection.collapsed(
offset: min(
Expand All @@ -159,7 +159,7 @@ 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: min(
Expand All @@ -171,7 +171,7 @@ class CurrencyInputFormatter extends TextInputFormatter {
}
} else {
if (_containsIllegalChars(newText)) {
// print('RETURN 3 ${oldValue.text}');
print('RETURN 3 ${oldValue.text}');
return oldValue;
}
}
Expand All @@ -194,7 +194,7 @@ class CurrencyInputFormatter extends TextInputFormatter {
newText: newText,
oldText: oldText,
)) {
// print('RETURN 4 ${oldValue.text.length} $oldCaretIndex');
print('RETURN 4 ${oldValue.text.length} $oldCaretIndex');
return oldValue.copyWith(
selection: TextSelection.collapsed(
offset: min(
Expand All @@ -211,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 @@ -235,14 +235,18 @@ class CurrencyInputFormatter extends TextInputFormatter {

var initialCaretOffset = leadingLength;
if (_isZeroOrEmpty(newAsNumeric)) {
// print('RETURN 7 ${newValue.text}');
print('RETURN 7 ${newValue.text}');
int offset = min(
newValue.text.length,
initialCaretOffset + 1,
);
if (newValue.text == '') {
offset = 1;
}
return newValue.copyWith(
text: newAsCurrency,
selection: TextSelection.collapsed(
offset: min(
newValue.text.length,
initialCaretOffset + 1,
),
offset: offset,
),
);
}
Expand All @@ -267,7 +271,7 @@ class CurrencyInputFormatter extends TextInputFormatter {
initialCaretOffset += 1;
}
}
// print('RETURN 8 $newAsCurrency');
print('RETURN 8 $newAsCurrency');
return TextEditingValue(
selection: TextSelection.collapsed(
offset: initialCaretOffset,
Expand Down
8 changes: 8 additions & 0 deletions lib/formatters/formatter_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ String toCurrencyString(
}
// print(thousandSeparator);
value = value.replaceAll(_repeatingDotsRegExp, '.');
if (mantissaLength == 0) {
if (value.contains('.')) {
value = value.substring(
0,
value.indexOf('.'),
);
}
}
value = toNumericString(
value,
allowPeriod: mantissaLength > 0,
Expand Down
8 changes: 6 additions & 2 deletions lib/utils/pinyin_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ class PinyinUtils {

static String splitToSyllablesBySeparator(
String value, [
String separator = '\'',
String separator = " ",
]) {
return splitToSyllables(value).join(separator);
final result = splitToSyllables(value)
.join(" ")
.replaceAll(RegExp(r"[']+"), " ")
.replaceAll(RegExp(r"\s+"), separator);
return result;
}

/// [value] a string to split into pinyin syllables
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.8.1
version: 2.8.2
homepage: https://github.com/caseyryan/flutter_multi_formatter

environment:
Expand Down

0 comments on commit fe2f925

Please sign in to comment.