Skip to content

Commit

Permalink
Merge pull request #6 from joutvhu/develop
Browse files Browse the repository at this point in the history
Fix group of negative number.
  • Loading branch information
joutvhu authored Apr 28, 2023
2 parents fd4d68a + 9a356cf commit 1038ee0
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0+5

- Fix group of negative numbers.

## 1.0.0+4

- Allow disable fix number.
Expand Down
9 changes: 8 additions & 1 deletion lib/src/filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class TextNumberFilter {
bool limitedNumber = false;
bool limitedInteger = false;
bool? limitedDecimal;
bool isNegative = false;
bool numberStarted = false;
bool hasNumber = false;
bool foundNumbers = false;
Expand Down Expand Up @@ -81,6 +82,7 @@ class TextNumberFilter {
bool filterNext(int value, int index, LookupTextValueEditor state) {
bool allow = false;
if (!numberStarted && options.allowNegative && value == _negative) {
isNegative = true;
allow = true;
} else if (decimalPoint == null) {
if (_number_0 <= value && value <= _number_9) {
Expand Down Expand Up @@ -315,9 +317,14 @@ class TextNumberFilter {
void groupDigits() {
if (options.groupDigits != null) {
var index = integerDigits;
var startPoint = 0;
if (isNegative) {
index += 1;
startPoint = 1;
}
while (true) {
index -= options.groupDigits!;
if (index < 1) {
if (index <= startPoint) {
break;
}
editor.insert(index, groupSeparator);
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: number_text_input_formatter
description: Number Text Input Formatter for Flutter
version: 1.0.0+4
version: 1.0.0+5

homepage: https://github.com/joutvhu/number_text_input_formatter
repository: https://github.com/joutvhu/number_text_input_formatter.git
Expand Down
50 changes: 50 additions & 0 deletions test/number_text_input_formatter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,56 @@ void main() {
expect(result.text, '-2454.41');
});

test('allow_negative_4', () {
var result = NumberTextInputFormatter(
integerDigits: 13,
decimalDigits: 2,
groupDigits: 3,
allowNegative: true,
).formatEditUpdate(
const TextEditingValue(
text: '',
),
const TextEditingValue(
text: '-245.41',
),
);
expect(result.text, '-245.41');
});

test('allow_negative_5', () {
var result = NumberTextInputFormatter(
integerDigits: 13,
decimalDigits: 2,
groupDigits: 3,
allowNegative: true,
).formatEditUpdate(
const TextEditingValue(
text: '',
),
const TextEditingValue(
text: '-2405.41',
),
);
expect(result.text, '-2,405.41');
});

test('allow_negative_6', () {
var result = NumberTextInputFormatter(
integerDigits: 13,
decimalDigits: 2,
allowNegative: true,
).formatEditUpdate(
const TextEditingValue(
text: '',
),
const TextEditingValue(
text: '--245.41',
),
);
expect(result.text, '-245.41');
});

test('insert_decimal_point_1', () {
var result = NumberTextInputFormatter(
integerDigits: 13,
Expand Down

0 comments on commit 1038ee0

Please sign in to comment.