Skip to content

Commit

Permalink
4.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Macacoazul01 committed Feb 17, 2024
1 parent 8451259 commit ee9d051
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## [4.1.0] - 2024-02-16
- Fixed incorrect handling of negative values.
- Updated android sample and added web.
- Added `forceValue()` function to the controller. Now it is possible to change the controller's value from outside the textfield.
- `checkNegative()` function now returns true if number is negative and false if it is positive.
- Bumped flutter_lints to 3.0.1

## [4.0.0]
- Bumped to dart 3

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2006-2023
Copyright (c) 2006-2024
All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
23 changes: 23 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ class _MyHomePageState extends State<MyHomePage> {
},
child: const Text('Controller3 value'),
),
const SizedBox(
height: 40,
),
MaterialButton(
onPressed: () {
print(
'initial value: ${_controller3.text}, initial double value: ${_controller3.doubleValue}');
print('trying to change controller.text to 4000');
_controller.text = '4000';
print(
'text value: ${_controller3.text}, double value: ${_controller3.doubleValue}');
print('trying to change controller.text to R\$ 4000.00');
_controller.text = 'R\$ 4000.00';
print(
'text value: ${_controller3.text}, double value: ${_controller3.doubleValue}');
print(
'changing controller.text using _controller3.forceValue() function');
_controller3.forceValue(initDoubleValue: 300);
print(
'final value: ${_controller3.text}, final double value: ${_controller3.doubleValue}');
},
child: const Text('Force controller 3 value'),
),
],
),
),
Expand Down
42 changes: 25 additions & 17 deletions lib/currency_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,15 @@ class CurrencyTextFieldController extends TextEditingController {
int numberOfDecimals = 2,
bool currencyOnLeft = true,
bool enableNegative = true,
}) : assert(
!(initDoubleValue != null && initIntValue != null),
"You must set either 'initDoubleValue' or 'initIntValue' parameter",
),
_currencySymbol = currencySymbol,
}) : _currencySymbol = currencySymbol,
_decimalSymbol = decimalSymbol,
_thousandSymbol = thousandSymbol,
_currencySeparator = currencySeparator,
_maxDigits = maxDigits,
_numberOfDecimals = numberOfDecimals,
_currencyOnLeft = currencyOnLeft,
_enableNegative = enableNegative {
if (initDoubleValue != null) {
_value = initDoubleValue;
initValue();
} else if (initIntValue != null) {
_value = initIntValue / 100;
initValue();
}
forceValue(initDoubleValue: initDoubleValue, initIntValue: initIntValue);
addListener(_listener);
}

Expand All @@ -122,7 +112,7 @@ class CurrencyTextFieldController extends TextEditingController {
}

if (clearText.isEmpty) {
zeroValue();
_zeroValue();
return;
}

Expand All @@ -132,7 +122,7 @@ class CurrencyTextFieldController extends TextEditingController {
}

if ((double.tryParse(clearText) ?? 0.0) == 0.0) {
zeroValue();
_zeroValue();
return;
}

Expand All @@ -147,32 +137,50 @@ class CurrencyTextFieldController extends TextEditingController {
_setSelectionBy(offset: text.length);
}

void initValue() {
///Force a value to the text controller.
void forceValue({double? initDoubleValue, int? initIntValue}) {
assert(!(initDoubleValue != null && initIntValue != null),
"You must set either 'initDoubleValue' or 'initIntValue' parameter.");
if (initDoubleValue != null) {
_value = initDoubleValue;
_updateValue();
} else if (initIntValue != null) {
_value = initIntValue / 100;
_updateValue();
}
}

void _updateValue() {
if (_value < 0) {
if (!_enableNegative) {
_value = _value * -1;
} else {
_isNegative = true;
}
} else {
_isNegative = false;
}
_previewsText = _composeCurrency(_applyMaskTo(value: _value));
text = _previewsText;
_setSelectionBy(offset: text.length);
}

void checkNegative() {
///check if the value is negative.
bool checkNegative() {
if (_enableNegative) {
_isNegative = text.startsWith('-');
} else {
_isNegative = false;
}
return _isNegative;
}

void _setSelectionBy({required int offset}) {
selection = TextSelection.fromPosition(TextPosition(offset: offset));
}

void zeroValue() {
///force the sign when `_value = 0`
void _zeroValue() {
_value = 0;
_previewsText = _negativeSign();
text = _previewsText;
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.0.0
version: 4.1.0
homepage: https://github.com/IsaiasSantana/currency_textfield
environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down

0 comments on commit ee9d051

Please sign in to comment.