Skip to content

Commit

Permalink
4.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Macacoazul01 committed Nov 6, 2024
1 parent 67d33f8 commit 6f62a13
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.13.0] - 2024-11-07
- Added `removeSymbol` parameter to let you define that controller will only show the formatted number.
- Fixed tests.

## [4.12.1] - 2024-11-06
- Mini improvement to `forceValue` function.

Expand Down
41 changes: 29 additions & 12 deletions lib/currency_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ import 'dart:math';
///
/// Default: `true`
///
/// `removeSymbol` lets you define that controller will only show the formatted number.
///
/// Default: `false`
///
class CurrencyTextFieldController extends TextEditingController {
final int _maxDigits, _numberOfDecimals;
final String _decimalSymbol, _thousandSymbol, _currencySeparator;
final bool _currencyOnLeft,
_enableNegative,
_resetSeparator,
_showZeroValue,
_forceCursorToEnd;
_forceCursorToEnd,
_removeSymbol;
final RegExp _onlyNumbersRegex = RegExp(r'[^\d]');
late String _currencySymbol, _symbolSeparator;

Expand Down Expand Up @@ -129,6 +134,7 @@ class CurrencyTextFieldController extends TextEditingController {
bool startWithSeparator = true,
bool showZeroValue = false,
bool forceCursorToEnd = true,
bool removeSymbol = false,
}) : assert(thousandSymbol != decimalSymbol,
"thousandSymbol must be different from decimalSymbol."),
assert(numberOfDecimals >= 0,
Expand All @@ -146,9 +152,13 @@ class CurrencyTextFieldController extends TextEditingController {
_startWithSeparator = startWithSeparator,
_resetSeparator = !startWithSeparator,
_showZeroValue = showZeroValue,
_forceCursorToEnd = forceCursorToEnd {
_changeSymbol();
forceValue(initDoubleValue: initDoubleValue, initIntValue: initIntValue);
_forceCursorToEnd = forceCursorToEnd,
_removeSymbol = removeSymbol {
_changeSymbolSeparator();
forceValue(
initDoubleValue: initDoubleValue,
initIntValue: initIntValue,
init: true);
addListener(_listener);
}

Expand Down Expand Up @@ -210,23 +220,26 @@ class CurrencyTextFieldController extends TextEditingController {
}

///Force a value to the text controller. If initDoubleValue and initIntValue are both null, 0 will be forced.
void forceValue({double? initDoubleValue, int? initIntValue}) {
void forceValue(
{double? initDoubleValue, int? initIntValue, bool init = false}) {
if (initDoubleValue != null) {
_value = initDoubleValue;
_updateValue();
} else if (initIntValue != null) {
_value = initIntValue / pow(10, _numberOfDecimals);
_updateValue();
} else {
_value = 0;
_updateValue();
if (!init) {
_value = 0;
_updateValue();
}
}
}

///Replace the current currency symbol by the defined value. If `resetValue = true` the controller will be reseted to 0.
void replaceCurrencySymbol(String newSymbol, {bool resetValue = false}) {
_currencySymbol = newSymbol;
_changeSymbol();
_changeSymbolSeparator();

if (resetValue) {
_value = 0;
Expand Down Expand Up @@ -316,10 +329,14 @@ class CurrencyTextFieldController extends TextEditingController {
_setSelectionBy(offset: text.length);
}

void _changeSymbol() {
_symbolSeparator = _currencyOnLeft
? (_currencySymbol + _currencySeparator)
: (_currencySeparator + _currencySymbol);
void _changeSymbolSeparator() {
if (!_removeSymbol) {
_symbolSeparator = _currencyOnLeft
? (_currencySymbol + _currencySeparator)
: (_currencySeparator + _currencySymbol);
} else {
_symbolSeparator = '';
}
}

void _setSelectionBy({required int offset}) {
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.12.1
version: 4.13.0
homepage: https://github.com/IsaiasSantana/currency_textfield
environment:
sdk: '>=3.0.0 <4.0.0'
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 @@ -227,4 +227,16 @@ void main() {
controller.forceValue(initDoubleValue: 50);
expect(controller.textWithoutCurrencySymbol, '50,00');
});

test('remove_symbol', () {
final controller =
CurrencyTextFieldController(initDoubleValue: 300, removeSymbol: true);

expect(controller.textWithoutCurrencySymbol, '300,00');
expect(controller.text, '300,00');
expect(controller.currencySymbol, 'R\$');
expect(controller.doubleValue, 300);
expect(controller.intValue, 30000);
expect(controller.doubleTextWithoutCurrencySymbol, '300.00');
});
}

0 comments on commit 6f62a13

Please sign in to comment.