From 2bf7a72abaa638c74f73c6b9fc5d40e80177783e Mon Sep 17 00:00:00 2001 From: Gianluca Bettega Date: Tue, 4 Jun 2024 09:50:37 -0300 Subject: [PATCH] 4.10.0 --- CHANGELOG.md | 3 +++ lib/currency_textfield.dart | 27 +++++++++++++++------------ pubspec.yaml | 2 +- test/currency_textfield_test.dart | 6 ++++-- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f91f398..c458283 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [4.10.0] - 2024-06-04 +- Added `showZeroValue` parameter: now you can define if the controller will show the 0 value.[19](https://github.com/IsaiasSantana/currency_textfield/issues/19) + ## [4.9.1] - 2024-06-02 - Fixed a bug that caused minus sign to not disappear when the value of the controller is 0.[18](https://github.com/IsaiasSantana/currency_textfield/issues/18). - Removed unnecessary call to the listener. diff --git a/lib/currency_textfield.dart b/lib/currency_textfield.dart index 08ccbfe..232840b 100644 --- a/lib/currency_textfield.dart +++ b/lib/currency_textfield.dart @@ -53,12 +53,16 @@ import 'dart:math'; /// `startWithSeparator` lets you define if the controller starts with decimals activated. /// /// Default `true` +/// +/// `showZeroValue` lets you define if the controller will show the 0 value. +/// +/// Default `false` /// class CurrencyTextFieldController extends TextEditingController { final int _maxDigits, _numberOfDecimals; final String _decimalSymbol, _thousandSymbol, _currencySeparator; final bool _currencyOnLeft, _enableNegative, _resetSeparator; - final bool _allowZeroValue; + final bool _showZeroValue; final RegExp _onlyNumbersRegex = RegExp(r'[^\d]'); late String _currencySymbol, _symbolSeparator; @@ -110,7 +114,7 @@ class CurrencyTextFieldController extends TextEditingController { bool enableNegative = true, double? maxValue, bool startWithSeparator = true, - bool allowZeroValue = false, + bool showZeroValue = false, }) : assert(thousandSymbol != decimalSymbol, "thousandSymbol must be different from decimalSymbol."), assert(numberOfDecimals >= 0, @@ -126,7 +130,7 @@ class CurrencyTextFieldController extends TextEditingController { _maxValue = maxValue, _startWithSeparator = startWithSeparator, _resetSeparator = !startWithSeparator, - _allowZeroValue = allowZeroValue { + _showZeroValue = showZeroValue { _changeSymbol(); forceValue(initDoubleValue: initDoubleValue, initIntValue: initIntValue); addListener(_listener); @@ -138,7 +142,7 @@ class CurrencyTextFieldController extends TextEditingController { return; } - if (!_allowZeroValue && text.isEmpty) { + if (text.isEmpty) { _zeroValue(resetText: false); return; } @@ -163,7 +167,7 @@ class CurrencyTextFieldController extends TextEditingController { } } - if (!_allowZeroValue && (double.tryParse(clearText) ?? 0.0) == 0.0) { + if ((double.tryParse(clearText) ?? 0.0) == 0.0) { _zeroValue(); return; } @@ -260,8 +264,8 @@ class CurrencyTextFieldController extends TextEditingController { } void _changeText() { - if (_value == 0) { - _previewsText = _allowZeroValue ? _composeCurrency(_applyMaskTo(value: _value)) : ''; + if (_value == 0 && !_showZeroValue) { + _previewsText = ''; } else { _previewsText = _composeCurrency(_applyMaskTo(value: _value)); } @@ -283,12 +287,11 @@ class CurrencyTextFieldController extends TextEditingController { void _zeroValue({bool resetText = true}) { _value = 0; _isNegative = false; - _previewsText = ''; - if (resetText) { - _previewsText = _allowZeroValue ? '0' : ''; - text = _previewsText; - _setSelectionBy(offset: 0); + if (resetText || _showZeroValue) { + _changeText(); + } else { + _previewsText = ''; } if (_resetSeparator && _startWithSeparator) { diff --git a/pubspec.yaml b/pubspec.yaml index 1a6c65c..3628e9f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: currency_textfield description: A flutter package that implements a Controller for currency text input. -version: 4.9.1 +version: 4.10.0 homepage: https://github.com/IsaiasSantana/currency_textfield environment: sdk: '>=3.0.0 <4.0.0' diff --git a/test/currency_textfield_test.dart b/test/currency_textfield_test.dart index de5e7e1..0845c97 100644 --- a/test/currency_textfield_test.dart +++ b/test/currency_textfield_test.dart @@ -182,14 +182,16 @@ void main() { }); test('test_allowZeroValue_shoulDisplayZeroValueFormatted', () { - final controller = CurrencyTextFieldController(initIntValue: 0, allowZeroValue: true); + final controller = CurrencyTextFieldController(initIntValue: 0, showZeroValue: true); expect(controller.text, "R\$ 0,00"); }); test('test_allowZeroValue_whithoutInitialValue_shoulDisplayEmptyString', () { - final controller = CurrencyTextFieldController(allowZeroValue: true); + final controller = CurrencyTextFieldController(showZeroValue: true); + final controller2 = CurrencyTextFieldController(initIntValue: 0); expect(controller.text, ''); + expect(controller2.text, ''); }); }