diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ce8c66..339c9a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [4.4.0] - 2024-02-19 +- Added `doubleTextWithoutCurrencySymbol` getter to the controller. Now it is possible to return the number part of the controller as a String, formatted as a double (with `.` as decimal separator). +- Added a check to ensure that `thousandSymbol` and `decimalSymbol` ​​are not the same. + ## [4.3.1] - 2024-02-19 - Added `maxValue` property to the controller. Now it is possible to define the maximum value the user is allowed to input. Everything greater than that will be forced to the maximum value. - Fixed input of `initIntValue` when `numberOfDecimals` was different than 2. diff --git a/example/lib/main.dart b/example/lib/main.dart index b718a01..e609078 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -103,6 +103,8 @@ class _MyHomePageState extends State { onPressed: () { print(_controller3.intValue); print(_controller3.text); + print(_controller3.textWithoutCurrencySymbol); + print(_controller3.doubleTextWithoutCurrencySymbol); }, child: const Text('Controller3 value'), ), diff --git a/lib/currency_textfield.dart b/lib/currency_textfield.dart index d248ad4..452271f 100644 --- a/lib/currency_textfield.dart +++ b/lib/currency_textfield.dart @@ -65,16 +65,31 @@ class CurrencyTextFieldController extends TextEditingController { double _value = 0.0; bool _isNegative = false; + ///return the number part of the controller as a double. double get doubleValue => _value.toPrecision(_numberOfDecimals); + + ///return the currency Symbol of the controller. String get currencySymbol => _currencySymbol; + + ///return the decimal Symbol of the controller. String get decimalSymbol => _decimalSymbol; + + ///return the thousand Symbol of the controller. String get thousandSymbol => _thousandSymbol; + + ///return the number part of the controller as a int. Ex: `1000` for a controller with `R$ 10,00` text. int get intValue => (_isNegative ? -1 : 1) * (int.tryParse(_getOnlyNumbers(string: text) ?? '') ?? 0); + + ///return the number part of the controller as a String. String get textWithoutCurrencySymbol => text.replaceFirst(_symbolSeparator, ''); + ///return the number part of the controller as a String, formatted as a double (with `.` as decimal separator). + String get doubleTextWithoutCurrencySymbol => + text.replaceFirst(_symbolSeparator, '').replaceFirst(decimalSymbol, '.'); + CurrencyTextFieldController({ String currencySymbol = 'R\$', String decimalSymbol = ',', @@ -87,7 +102,9 @@ class CurrencyTextFieldController extends TextEditingController { bool currencyOnLeft = true, bool enableNegative = true, double? maxValue, - }) : _currencySymbol = currencySymbol, + }) : assert(thousandSymbol != decimalSymbol, + "thousandSymbol must be different from decimalSymbol."), + _currencySymbol = currencySymbol, _decimalSymbol = decimalSymbol, _thousandSymbol = thousandSymbol, _currencySeparator = currencySeparator, diff --git a/pubspec.yaml b/pubspec.yaml index 3bb835d..18326ef 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.3.1 +version: 4.4.0 homepage: https://github.com/IsaiasSantana/currency_textfield environment: sdk: '>=3.0.0 <4.0.0'