From ce20355f888825e391b2424f1e435d44662cad21 Mon Sep 17 00:00:00 2001 From: Gianluca Bettega Date: Tue, 9 Apr 2024 19:55:54 -0300 Subject: [PATCH] 4.5.0 --- CHANGELOG.md | 4 +++ README.md | 10 ++++++ example/lib/input_field.dart | 5 ++- example/lib/main.dart | 25 ++++++++++----- example/pubspec.lock | 52 ++++++++++++++++++++++--------- lib/currency_textfield.dart | 45 +++++++++++++++++++------- pubspec.lock | 50 +++++++++++++++++++++-------- pubspec.yaml | 2 +- test/currency_textfield_test.dart | 19 +++++++++++ 9 files changed, 161 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1140459..c8f9997 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [4.5.0] - 2024-04-09 +- Added `replaceCurrencySymbol` function to the controller. Now it is possible to change the controller's currency symbol. +- Forced `doubleTextWithoutCurrencySymbol` to be '0' when controller.text = ''. + ## [4.4.1] - 2024-02-21 - Fixed `doubleTextWithoutCurrencySymbol`. diff --git a/README.md b/README.md index e3f0118..99cd94f 100644 --- a/README.md +++ b/README.md @@ -98,3 +98,13 @@ controller.forceValue(initDoubleValue: 300.5); // using an int controller.forceValue(initIntValue: 10000); ``` + +### Change the currency symbol of the controller +```dart +final CurrencyTextFieldController controller = CurrencyTextFieldController(); +// keeping the current value: +controller.replaceCurrencySymbol('EUR'); + +// reseting the current value: +controller.replaceCurrencySymbol('EUR', resetValue: true); +``` \ No newline at end of file diff --git a/example/lib/input_field.dart b/example/lib/input_field.dart index a2859d0..bdd9cb6 100644 --- a/example/lib/input_field.dart +++ b/example/lib/input_field.dart @@ -6,7 +6,7 @@ FilteringTextInputFormatter allValues = class BuildInputField extends StatefulWidget { const BuildInputField( - {Key? key, + {super.key, required this.controle, this.mascara, this.textInputButton = TextInputAction.done, @@ -28,8 +28,7 @@ class BuildInputField extends StatefulWidget { this.alinhamento = TextAlign.start, this.disabledColor, this.capitalization}) - : estilo = estilo ?? const TextStyle(fontSize: 16), - super(key: key); + : estilo = estilo ?? const TextStyle(fontSize: 16); final TextEditingController controle; final dynamic mascara; final TextInputAction textInputButton; diff --git a/example/lib/main.dart b/example/lib/main.dart index e609078..292643d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -22,7 +22,7 @@ class MyApp extends StatelessWidget { } class MyHomePage extends StatefulWidget { - const MyHomePage({Key? key, required this.title}) : super(key: key); + const MyHomePage({super.key, required this.title}); final String title; @@ -48,7 +48,7 @@ class _MyHomePageState extends State { child: Column( children: [ const SizedBox( - height: 40, + height: 30, ), BuildInputField( controle: _controller, @@ -57,7 +57,16 @@ class _MyHomePageState extends State { mascara: allValues, ), const SizedBox( - height: 40, + height: 10, + ), + MaterialButton( + onPressed: () { + _controller.replaceCurrencySymbol('EUR', resetValue: true); + }, + child: const Text('Change currency symbol'), + ), + const SizedBox( + height: 30, ), MaterialButton( onPressed: () { @@ -69,7 +78,7 @@ class _MyHomePageState extends State { child: const Text('Controller1 value'), ), const SizedBox( - height: 40, + height: 30, ), BuildInputField( controle: _controller2, @@ -78,7 +87,7 @@ class _MyHomePageState extends State { mascara: allValues, ), const SizedBox( - height: 40, + height: 30, ), MaterialButton( onPressed: () { @@ -88,7 +97,7 @@ class _MyHomePageState extends State { child: const Text('Controller2 value'), ), const SizedBox( - height: 40, + height: 30, ), BuildInputField( controle: _controller3, @@ -97,7 +106,7 @@ class _MyHomePageState extends State { mascara: allValues, ), const SizedBox( - height: 40, + height: 30, ), MaterialButton( onPressed: () { @@ -109,7 +118,7 @@ class _MyHomePageState extends State { child: const Text('Controller3 value'), ), const SizedBox( - height: 40, + height: 30, ), MaterialButton( onPressed: () { diff --git a/example/pubspec.lock b/example/pubspec.lock index b3640fe..321bd06 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -47,7 +47,7 @@ packages: path: ".." relative: true source: path - version: "4.1.0" + version: "4.4.1" fake_async: dependency: transitive description: @@ -79,38 +79,62 @@ packages: url: "https://pub.dev" source: hosted version: "0.18.1" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" + url: "https://pub.dev" + source: hosted + version: "10.0.0" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0 + url: "https://pub.dev" + source: hosted + version: "2.0.1" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 + url: "https://pub.dev" + source: hosted + version: "2.0.1" matcher: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e + sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.11.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" sky_engine: dependency: transitive description: flutter @@ -172,13 +196,13 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: + vm_service: dependency: transitive description: - name: web - sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 + name: vm_service + sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 url: "https://pub.dev" source: hosted - version: "0.3.0" + version: "13.0.0" sdks: - dart: ">=3.2.0-194.0.dev <4.0.0" + dart: ">=3.2.0-0 <4.0.0" diff --git a/lib/currency_textfield.dart b/lib/currency_textfield.dart index f4df578..29722f4 100644 --- a/lib/currency_textfield.dart +++ b/lib/currency_textfield.dart @@ -52,14 +52,11 @@ import 'dart:math'; /// class CurrencyTextFieldController extends TextEditingController { final int _maxDigits, _numberOfDecimals; - final String _currencySymbol, - _decimalSymbol, - _thousandSymbol, - _currencySeparator; + final String _decimalSymbol, _thousandSymbol, _currencySeparator; final bool _currencyOnLeft, _enableNegative; final RegExp _onlyNumbersRegex = RegExp(r'[^\d]'); - late final String _symbolSeparator; final double? _maxValue; + late String _currencySymbol, _symbolSeparator; String _previewsText = ''; double _value = 0.0; @@ -87,10 +84,12 @@ class CurrencyTextFieldController extends TextEditingController { 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, '') - .replaceAll(thousandSymbol, '') - .replaceFirst(decimalSymbol, '.'); + String get doubleTextWithoutCurrencySymbol => text != '' + ? text + .replaceFirst(_symbolSeparator, '') + .replaceAll(thousandSymbol, '') + .replaceFirst(decimalSymbol, '.') + : '0'; CurrencyTextFieldController({ String currencySymbol = 'R\$', @@ -115,9 +114,7 @@ class CurrencyTextFieldController extends TextEditingController { _currencyOnLeft = currencyOnLeft, _enableNegative = enableNegative, _maxValue = maxValue { - _symbolSeparator = currencyOnLeft - ? (_currencySymbol + _currencySeparator) - : (_currencySeparator + _currencySymbol); + _changeSymbol(); forceValue(initDoubleValue: initDoubleValue, initIntValue: initIntValue); addListener(_listener); } @@ -184,6 +181,24 @@ class CurrencyTextFieldController extends TextEditingController { } } + ///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(); + + late final String maskedValue; + if (resetValue) { + _value = 0; + maskedValue = ''; + } else { + maskedValue = _composeCurrency(_applyMaskTo(value: _value)); + } + + _previewsText = maskedValue; + + text = maskedValue; + } + void _updateValue() { if (_value < 0) { if (!_enableNegative) { @@ -219,6 +234,12 @@ class CurrencyTextFieldController extends TextEditingController { return _isNegative; } + void _changeSymbol() { + _symbolSeparator = _currencyOnLeft + ? (_currencySymbol + _currencySeparator) + : (_currencySeparator + _currencySymbol); + } + void _setSelectionBy({required int offset}) { selection = TextSelection.fromPosition(TextPosition(offset: offset)); } diff --git a/pubspec.lock b/pubspec.lock index 84b8305..cfa8653 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -67,6 +67,30 @@ packages: description: flutter source: sdk version: "0.0.0" + leak_tracker: + dependency: transitive + description: + name: leak_tracker + sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa" + url: "https://pub.dev" + source: hosted + version: "10.0.0" + leak_tracker_flutter_testing: + dependency: transitive + description: + name: leak_tracker_flutter_testing + sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0 + url: "https://pub.dev" + source: hosted + version: "2.0.1" + leak_tracker_testing: + dependency: transitive + description: + name: leak_tracker_testing + sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47 + url: "https://pub.dev" + source: hosted + version: "2.0.1" lints: dependency: transitive description: @@ -79,34 +103,34 @@ packages: dependency: transitive description: name: matcher - sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb url: "https://pub.dev" source: hosted - version: "0.12.16" + version: "0.12.16+1" material_color_utilities: dependency: transitive description: name: material_color_utilities - sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a" url: "https://pub.dev" source: hosted - version: "0.5.0" + version: "0.8.0" meta: dependency: transitive description: name: meta - sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e + sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04 url: "https://pub.dev" source: hosted - version: "1.10.0" + version: "1.11.0" path: dependency: transitive description: name: path - sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af" url: "https://pub.dev" source: hosted - version: "1.8.3" + version: "1.9.0" sky_engine: dependency: transitive description: flutter @@ -168,13 +192,13 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.4" - web: + vm_service: dependency: transitive description: - name: web - sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152 + name: vm_service + sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957 url: "https://pub.dev" source: hosted - version: "0.3.0" + version: "13.0.0" sdks: - dart: ">=3.2.0-194.0.dev <4.0.0" + dart: ">=3.2.0-0 <4.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 0dec3d3..0f4643f 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.4.1 +version: 4.5.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 f47a349..2e81771 100644 --- a/test/currency_textfield_test.dart +++ b/test/currency_textfield_test.dart @@ -137,4 +137,23 @@ void main() { controller.forceValue(initDoubleValue: 3500); expect(controller.textWithoutCurrencySymbol, '400,00'); }); + + test('replace_symbol', () { + final controller = CurrencyTextFieldController(initIntValue: 195); + controller.replaceCurrencySymbol('EUR'); + expect(controller.text, 'EUR 1,95'); + expect(controller.doubleValue, 1.95); + expect(controller.intValue, 195); + expect(controller.currencySymbol, 'EUR'); + expect(controller.doubleTextWithoutCurrencySymbol, '1.95'); + expect(controller.textWithoutCurrencySymbol, '1,95'); + + controller.replaceCurrencySymbol('USD', resetValue: true); + expect(controller.text, ''); + expect(controller.doubleValue, 0); + expect(controller.intValue, 0); + expect(controller.currencySymbol, 'USD'); + expect(controller.doubleTextWithoutCurrencySymbol, '0'); + expect(controller.textWithoutCurrencySymbol, ''); + }); }