Skip to content

Commit

Permalink
Merge pull request #20 from IsaiasSantana/feature/allowZeroValue
Browse files Browse the repository at this point in the history
Allow insertion of zero value into controller
  • Loading branch information
Macacoazul01 authored Jun 4, 2024
2 parents aee9a1e + ed3a18c commit 3111926
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/currency_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class CurrencyTextFieldController extends TextEditingController {
final int _maxDigits, _numberOfDecimals;
final String _decimalSymbol, _thousandSymbol, _currencySeparator;
final bool _currencyOnLeft, _enableNegative, _resetSeparator;
final bool _allowZeroValue;
final RegExp _onlyNumbersRegex = RegExp(r'[^\d]');
late String _currencySymbol, _symbolSeparator;

Expand Down Expand Up @@ -109,6 +110,7 @@ class CurrencyTextFieldController extends TextEditingController {
bool enableNegative = true,
double? maxValue,
bool startWithSeparator = true,
bool allowZeroValue = false,
}) : assert(thousandSymbol != decimalSymbol,
"thousandSymbol must be different from decimalSymbol."),
assert(numberOfDecimals >= 0,
Expand All @@ -123,7 +125,8 @@ class CurrencyTextFieldController extends TextEditingController {
_enableNegative = enableNegative,
_maxValue = maxValue,
_startWithSeparator = startWithSeparator,
_resetSeparator = !startWithSeparator {
_resetSeparator = !startWithSeparator,
_allowZeroValue = allowZeroValue {
_changeSymbol();
forceValue(initDoubleValue: initDoubleValue, initIntValue: initIntValue);
addListener(_listener);
Expand All @@ -135,7 +138,7 @@ class CurrencyTextFieldController extends TextEditingController {
return;
}

if (text.isEmpty) {
if (!_allowZeroValue && text.isEmpty) {
_zeroValue(resetText: false);
return;
}
Expand All @@ -160,7 +163,7 @@ class CurrencyTextFieldController extends TextEditingController {
}
}

if ((double.tryParse(clearText) ?? 0.0) == 0.0) {
if (!_allowZeroValue && (double.tryParse(clearText) ?? 0.0) == 0.0) {
_zeroValue();
return;
}
Expand Down Expand Up @@ -258,7 +261,7 @@ class CurrencyTextFieldController extends TextEditingController {

void _changeText() {
if (_value == 0) {
_previewsText = '';
_previewsText = _allowZeroValue ? _composeCurrency(_applyMaskTo(value: _value)) : '';
} else {
_previewsText = _composeCurrency(_applyMaskTo(value: _value));
}
Expand All @@ -281,10 +284,13 @@ class CurrencyTextFieldController extends TextEditingController {
_value = 0;
_isNegative = false;
_previewsText = '';

if (resetText) {
_previewsText = _allowZeroValue ? '0' : '';
text = _previewsText;
_setSelectionBy(offset: 0);
}

if (_resetSeparator && _startWithSeparator) {
_startWithSeparator = false;
}
Expand Down
12 changes: 12 additions & 0 deletions test/currency_textfield_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,16 @@ void main() {
controller.replaceMaxValue(500);
expect(controller.textWithoutCurrencySymbol, '500,00');
});

test('test_allowZeroValue_shoulDisplayZeroValueFormatted', () {
final controller = CurrencyTextFieldController(initIntValue: 0, allowZeroValue: true);

expect(controller.text, "R\$ 0,00");
});

test('test_allowZeroValue_whithoutInitialValue_shoulDisplayEmptyString', () {
final controller = CurrencyTextFieldController(allowZeroValue: true);

expect(controller.text, '');
});
}

0 comments on commit 3111926

Please sign in to comment.