Skip to content

Commit

Permalink
4.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Macacoazul01 committed Jun 8, 2024
1 parent 678cdb3 commit a669dd6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.11.0] - 2024-06-07
- Added `forceCursorToEnd` parameter: now you can define if the controller will always force the user to input the numbers on the end of the string.
- Fixed If enableNegative + showZeroValue properties are true, then negative #s cannot be entered! [22](https://github.com/IsaiasSantana/currency_textfield/issues/22).

## [4.10.1] - 2024-06-04
- Added ios folder to example.

Expand Down
2 changes: 1 addition & 1 deletion example/lib/input_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class _BuildInputFieldState extends State<BuildInputField> {
style: widget.estilo,
maxLines: widget.lines,
obscureText: widget.oculto,
cursorColor: Colors.transparent,
cursorColor: Colors.red,
controller: widget.controle,
inputFormatters: [widget.mascara ?? allValues],
autocorrect: false,
Expand Down
51 changes: 31 additions & 20 deletions lib/currency_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,68 @@ import 'dart:math';
///
/// `currencySymbol` is your currency symbol.
///
/// Default `R\$`
/// Default: `R\$`
///
/// `decimalSymbol` is the decimal separator symbol.
///
/// Default `,`
/// Default: `,`
///
/// `thousandSymbol` is the thousand separator symbol.
///
/// @Default `.`
/// @Default: `.`
///
/// `initDoubleValue` is the optional initial value in double format.
///
/// Default `null`
/// Default: `null`
///
/// `initIntValue` is the optional initial value in int format. It'll be divided by 100 before being presented.
///
/// Default `null`
/// Default: `null`
///
/// `numberOfDecimals` lets you define the max number of decimal digits to be presented.
///
/// Default `2`
/// Default: `2`
///
/// `maxDigits` lets you define the max number of digits to be presented.
///
/// Default `15`
/// Default: `15`
///
/// `currencyOnLeft` lets you define if the symbol will be on left or right of the value.
///
/// Default `true`
/// Default: `true`
///
/// `enableNegative` lets you define if the user can set negative values.
///
/// Default `true`
/// Default: `true`
///
/// `currencySeparator` lets you define the separator between the symbol and the value.
///
/// Default `' '`
/// Default: `' '`
///
/// `maxValue` lets you define the maximum allowed value of the controller.
///
/// Default `null`
/// Default: `null`
///
/// `startWithSeparator` lets you define if the controller starts with decimals activated.
///
/// Default `true`
/// Default: `true`
///
/// `showZeroValue` lets you define if the controller will show the 0 value.
///
/// Default `false`
/// Default: `false`
///
/// `forceCursorToEnd` lets you define if the controller will always force the user to input the numbers on the end of the string.
///
/// Default: `true`
///
class CurrencyTextFieldController extends TextEditingController {
final int _maxDigits, _numberOfDecimals;
final String _decimalSymbol, _thousandSymbol, _currencySeparator;
final bool _currencyOnLeft, _enableNegative, _resetSeparator;
final bool _showZeroValue;
final bool _currencyOnLeft,
_enableNegative,
_resetSeparator,
_showZeroValue,
_forceCursorToEnd;
final RegExp _onlyNumbersRegex = RegExp(r'[^\d]');
late String _currencySymbol, _symbolSeparator;

Expand Down Expand Up @@ -115,6 +122,7 @@ class CurrencyTextFieldController extends TextEditingController {
double? maxValue,
bool startWithSeparator = true,
bool showZeroValue = false,
bool forceCursorToEnd = true,
}) : assert(thousandSymbol != decimalSymbol,
"thousandSymbol must be different from decimalSymbol."),
assert(numberOfDecimals >= 0,
Expand All @@ -130,15 +138,18 @@ class CurrencyTextFieldController extends TextEditingController {
_maxValue = maxValue,
_startWithSeparator = startWithSeparator,
_resetSeparator = !startWithSeparator,
_showZeroValue = showZeroValue {
_showZeroValue = showZeroValue,
_forceCursorToEnd = forceCursorToEnd {
_changeSymbol();
forceValue(initDoubleValue: initDoubleValue, initIntValue: initIntValue);
addListener(_listener);
}

void _listener() {
if (_previewsText == text) {
_setSelectionBy(offset: text.length);
if (_forceCursorToEnd) {
_setSelectionBy(offset: text.length);
}
return;
}

Expand Down Expand Up @@ -168,7 +179,7 @@ class CurrencyTextFieldController extends TextEditingController {
}

if ((double.tryParse(clearText) ?? 0.0) == 0.0) {
_zeroValue();
_zeroValue(forceNegative: text.endsWith('-'));
return;
}

Expand Down Expand Up @@ -284,9 +295,9 @@ class CurrencyTextFieldController extends TextEditingController {
}

///resets the controller to 0.
void _zeroValue({bool resetText = true}) {
void _zeroValue({bool resetText = true, bool forceNegative = false}) {
_value = 0;
_isNegative = false;
_isNegative = forceNegative;

if (resetText || _showZeroValue) {
_changeText();
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.10.1
version: 4.11.0
homepage: https://github.com/IsaiasSantana/currency_textfield
environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down
10 changes: 10 additions & 0 deletions test/currency_textfield_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ void main() {
expect(controller.text, '');
expect(controller2.text, '');
});

test('enableNegative + showZeroValue bug fix', () {
final controller = CurrencyTextFieldController(showZeroValue: true);
controller.text = "R\$ 0,00-";

expect(controller.text, '-R\$ 0,00');
controller.clear();
controller.text = "R\$ 7,00-";
expect(controller.text, "R\$ 7,00");
});
}

0 comments on commit a669dd6

Please sign in to comment.