Skip to content

Commit

Permalink
4.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Macacoazul01 committed Jun 4, 2024
1 parent 3111926 commit 2bf7a72
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
27 changes: 15 additions & 12 deletions lib/currency_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -138,7 +142,7 @@ class CurrencyTextFieldController extends TextEditingController {
return;
}

if (!_allowZeroValue && text.isEmpty) {
if (text.isEmpty) {
_zeroValue(resetText: false);
return;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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));
}
Expand All @@ -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) {
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: 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'
Expand Down
6 changes: 4 additions & 2 deletions test/currency_textfield_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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, '');
});
}

0 comments on commit 2bf7a72

Please sign in to comment.