Skip to content

Commit c9799b1

Browse files
committed
Check isNotEmpty before insert prefix or suffix.
1 parent 430eca7 commit c9799b1

File tree

6 files changed

+41
-17
lines changed

6 files changed

+41
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22

33
- Initial library
44
- Support NumberTextInputFormatter, CurrencyTextInputFormatter and PercentageTextInputFormatter.
5+
6+
## 1.0.0+1
7+
8+
- Check isNotEmpty before insert prefix or suffix.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Number Text Input Formatter for Flutter.
77
```yaml
88
# Add into pubspec.yaml
99
dependencies:
10-
number_text_input_formatter: ^1.0.0
10+
number_text_input_formatter: ^1.0.0+1
1111
```
1212
1313
## Usage

lib/src/editor.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ abstract class TextValueEditor {
1515

1616
String get text;
1717

18+
bool get isEmpty;
19+
20+
bool get isNotEmpty;
21+
1822
TextEditingValue get inputValue;
1923

2024
MutableTextRange? get selection;
@@ -55,6 +59,12 @@ class DefaultTextValueEditor implements TextValueEditor {
5559
@override
5660
int get length => codeUnits.length;
5761

62+
@override
63+
bool get isEmpty => codeUnits.isEmpty;
64+
65+
@override
66+
bool get isNotEmpty => codeUnits.isNotEmpty;
67+
5868
DefaultTextValueEditor(this.inputValue)
5969
: selection = MutableTextRange.fromTextSelection(inputValue.selection),
6070
composingRegion = MutableTextRange.fromComposingRange(inputValue.composing),
@@ -195,11 +205,17 @@ class LookupTextValueEditor implements TextValueEditor {
195205
return editor[index];
196206
}
197207

208+
@override
209+
String get text => editor.text;
210+
198211
@override
199212
int get length => editor.length;
200213

201214
@override
202-
String get text => editor.text;
215+
bool get isEmpty => editor.isEmpty;
216+
217+
@override
218+
bool get isNotEmpty => editor.isNotEmpty;
203219

204220
int get currentCode => editor[index];
205221

lib/src/filter.dart

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,19 @@ class TextNumberFilter {
249249
}
250250

251251
void afterFilter() {
252-
if (decimalPoint != null) {
253-
if (decimalDigits == 0) {
254-
insertDecimalDigits();
255-
}
256-
} else {
257-
if (hasDecimalPoint) {
258-
insertDecimalPoint();
252+
if (editor.isNotEmpty) {
253+
if (decimalPoint != null) {
254+
if (decimalDigits == 0) {
255+
insertDecimalDigits();
256+
}
257+
} else {
258+
if (hasDecimalPoint) {
259+
insertDecimalPoint();
260+
}
259261
}
260-
}
261262

262-
groupDigits();
263+
groupDigits();
264+
}
263265
}
264266

265267
void insertDecimalDigits() {

lib/src/formatter.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,13 @@ class NumberTextInputFormatter extends TextInputFormatter {
117117
..prepare({'removing': oldValue.text.length - 1 == newValue.text.length})
118118
..filter();
119119

120-
if (prefix != null) {
121-
state.prefix(prefix!);
122-
}
123-
if (suffix != null) {
124-
state.suffix(suffix!);
120+
if (state.isNotEmpty) {
121+
if (prefix != null) {
122+
state.prefix(prefix!);
123+
}
124+
if (suffix != null) {
125+
state.suffix(suffix!);
126+
}
125127
}
126128

127129
return state.finalize();

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: number_text_input_formatter
22
description: Number Text Input Formatter for Flutter
3-
version: 1.0.0
3+
version: 1.0.0+1
44

55
homepage: https://github.com/joutvhu/number_text_input_formatter
66
repository: https://github.com/joutvhu/number_text_input_formatter.git

0 commit comments

Comments
 (0)