Skip to content

Commit

Permalink
Merge pull request #4 from joutvhu/develop
Browse files Browse the repository at this point in the history
Allow disable fix number.
  • Loading branch information
joutvhu authored Apr 22, 2023
2 parents 283d715 + 109dfd6 commit fd4d68a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 27 deletions.
17 changes: 10 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
## 1.0.0
## 1.0.0+4

- Initial library
- Support NumberTextInputFormatter, CurrencyTextInputFormatter and PercentageTextInputFormatter.
- Allow disable fix number.

## 1.0.0+1
## 1.0.0+3

- Check isNotEmpty before insert prefix or suffix.
- Fix maxDecimal issue.

## 1.0.0+2

- Fix maxValue issue.

## 1.0.0+1

## 1.0.0+3
- Check isNotEmpty before insert prefix or suffix.

- Fix maxDecimal issue.
## 1.0.0

- Initial library
- Support NumberTextInputFormatter, CurrencyTextInputFormatter and PercentageTextInputFormatter.
43 changes: 24 additions & 19 deletions lib/src/filter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ class TextNumberFilter {

TextValueEditor filter() {
editor.forEach(filterNext, filterComplete);
afterFilter();
return editor;
}

Expand Down Expand Up @@ -118,9 +117,20 @@ class TextNumberFilter {
state.remove(startPosition, length);
}

if (hasNumber && integerDigits == 0) {
integerDigits = 1;
state.prefix('0');
insertIntegerDigits();

if (editor.isNotEmpty) {
if (decimalPoint != null) {
if (decimalDigits == 0) {
insertDecimalDigits();
}
} else {
if (hasDecimalPoint) {
insertDecimalPoint();
}
}

groupDigits();
}
}

Expand Down Expand Up @@ -174,8 +184,10 @@ class TextNumberFilter {
if (!allowing && startPosition < state.index) {
state.remove(startPosition, state.index);
}
state.prefix('0');
integerDigits = 1;
if (options.fixNumber) {
state.prefix('0');
integerDigits = 1;
}
allowing = true;
startPosition = state.index;
}
Expand Down Expand Up @@ -254,25 +266,18 @@ class TextNumberFilter {
return allow;
}

void afterFilter() {
if (editor.isNotEmpty) {
if (decimalPoint != null) {
if (decimalDigits == 0) {
insertDecimalDigits();
}
} else {
if (hasDecimalPoint) {
insertDecimalPoint();
}
void insertIntegerDigits() {
if ((decimalPoint != null || hasNumber) && integerDigits == 0) {
if (!removing && options.fixNumber) {
integerDigits = 1;
editor.prefix('0');
}

groupDigits();
}
}

void insertDecimalDigits() {
decimalPoint ??= editor.length - 1;
if (!removing) {
if (!removing && (options.fixNumber || options.insertDecimalDigits)) {
decimalDigits = options.decimalDigits ?? 1;
editor.suffix('0' * decimalDigits);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/src/formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class NumberTextInputFormatter extends TextInputFormatter {
/// Allow input of negative numbers?
final bool allowNegative;

/// Insert 0 before the decimal point if the number starts with a decimal point
/// Insert 0 after the decimal point if the number ends with a decimal point
final bool fixNumber;

/// Automatically insert decimal point.
final bool insertDecimalPoint;

Expand All @@ -57,6 +61,7 @@ class NumberTextInputFormatter extends TextInputFormatter {
this.groupSeparator = ',',
this.allowNegative = false,
this.overrideDecimalPoint = false,
this.fixNumber = true,
this.insertDecimalPoint = false,
this.insertDecimalDigits = false,
}) : assert(integerDigits == null || integerDigits > 0),
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+3
version: 1.0.0+4

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

test('override_decimal_point_10', () {
var result = NumberTextInputFormatter(
integerDigits: 2,
decimalDigits: 2,
insertDecimalPoint: true,
).formatEditUpdate(
const TextEditingValue(
text: '',
),
const TextEditingValue(
text: '58',
),
);
expect(result.text, '0.58');
});

test('filter_other_decimal_point_1', () {
var result = NumberTextInputFormatter(
integerDigits: 13,
Expand Down Expand Up @@ -787,6 +803,38 @@ void main() {
expect(result.text, '3.66');
});

test('filter_fix_number_1', () {
var result = NumberTextInputFormatter(
integerDigits: 12,
groupDigits: 3,
fixNumber: false
).formatEditUpdate(
const TextEditingValue(
text: '11',
),
const TextEditingValue(
text: '11.',
),
);
expect(result.text, '11.');
});

test('filter_fix_number_2', () {
var result = NumberTextInputFormatter(
integerDigits: 12,
groupDigits: 3,
fixNumber: false
).formatEditUpdate(
const TextEditingValue(
text: '11',
),
const TextEditingValue(
text: '.11',
),
);
expect(result.text, '.11');
});

test('currency_text_input_formatter_1', () {
var result = CurrencyTextInputFormatter(
prefix: '\$'
Expand Down

0 comments on commit fd4d68a

Please sign in to comment.