Skip to content

Commit

Permalink
4.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Macacoazul01 committed Apr 9, 2024
1 parent 862c2a0 commit ce20355
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
5 changes: 2 additions & 3 deletions example/lib/input_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
25 changes: 17 additions & 8 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -48,7 +48,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: Column(
children: [
const SizedBox(
height: 40,
height: 30,
),
BuildInputField(
controle: _controller,
Expand All @@ -57,7 +57,16 @@ class _MyHomePageState extends State<MyHomePage> {
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: () {
Expand All @@ -69,7 +78,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: const Text('Controller1 value'),
),
const SizedBox(
height: 40,
height: 30,
),
BuildInputField(
controle: _controller2,
Expand All @@ -78,7 +87,7 @@ class _MyHomePageState extends State<MyHomePage> {
mascara: allValues,
),
const SizedBox(
height: 40,
height: 30,
),
MaterialButton(
onPressed: () {
Expand All @@ -88,7 +97,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: const Text('Controller2 value'),
),
const SizedBox(
height: 40,
height: 30,
),
BuildInputField(
controle: _controller3,
Expand All @@ -97,7 +106,7 @@ class _MyHomePageState extends State<MyHomePage> {
mascara: allValues,
),
const SizedBox(
height: 40,
height: 30,
),
MaterialButton(
onPressed: () {
Expand All @@ -109,7 +118,7 @@ class _MyHomePageState extends State<MyHomePage> {
child: const Text('Controller3 value'),
),
const SizedBox(
height: 40,
height: 30,
),
MaterialButton(
onPressed: () {
Expand Down
52 changes: 38 additions & 14 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ packages:
path: ".."
relative: true
source: path
version: "4.1.0"
version: "4.4.1"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
45 changes: 33 additions & 12 deletions lib/currency_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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\$',
Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
Expand Down
50 changes: 37 additions & 13 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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"
Loading

0 comments on commit ce20355

Please sign in to comment.