From 6f62a13ad380c46f4126de3365305653437852be Mon Sep 17 00:00:00 2001 From: Gianluca Bettega Date: Wed, 6 Nov 2024 17:04:40 -0300 Subject: [PATCH] 4.13.0 --- CHANGELOG.md | 4 +++ lib/currency_textfield.dart | 41 ++++++++++++++++++++++--------- pubspec.yaml | 2 +- test/currency_textfield_test.dart | 12 +++++++++ 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c6626a..2b6f643 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/currency_textfield.dart b/lib/currency_textfield.dart index 994b24d..6c79745 100644 --- a/lib/currency_textfield.dart +++ b/lib/currency_textfield.dart @@ -66,6 +66,10 @@ 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; @@ -73,7 +77,8 @@ class CurrencyTextFieldController extends TextEditingController { _enableNegative, _resetSeparator, _showZeroValue, - _forceCursorToEnd; + _forceCursorToEnd, + _removeSymbol; final RegExp _onlyNumbersRegex = RegExp(r'[^\d]'); late String _currencySymbol, _symbolSeparator; @@ -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, @@ -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); } @@ -210,7 +220,8 @@ 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(); @@ -218,15 +229,17 @@ class CurrencyTextFieldController extends TextEditingController { _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; @@ -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}) { diff --git a/pubspec.yaml b/pubspec.yaml index 6cfca93..b84711f 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.12.1 +version: 4.13.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 8df2c89..c86a2ba 100644 --- a/test/currency_textfield_test.dart +++ b/test/currency_textfield_test.dart @@ -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'); + }); }