From ed3a18c664ec48d5d3574bd05d5b8e372c56368f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isa=C3=ADas=20Santana?= Date: Tue, 4 Jun 2024 00:51:15 -0300 Subject: [PATCH] feat: Allow zero value in the controller --- lib/currency_textfield.dart | 14 ++++++++++---- test/currency_textfield_test.dart | 12 ++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/currency_textfield.dart b/lib/currency_textfield.dart index 346561f..08ccbfe 100644 --- a/lib/currency_textfield.dart +++ b/lib/currency_textfield.dart @@ -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; @@ -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, @@ -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); @@ -135,7 +138,7 @@ class CurrencyTextFieldController extends TextEditingController { return; } - if (text.isEmpty) { + if (!_allowZeroValue && text.isEmpty) { _zeroValue(resetText: false); return; } @@ -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; } @@ -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)); } @@ -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; } diff --git a/test/currency_textfield_test.dart b/test/currency_textfield_test.dart index 5be69bf..de5e7e1 100644 --- a/test/currency_textfield_test.dart +++ b/test/currency_textfield_test.dart @@ -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, ''); + }); }