Skip to content

Commit

Permalink
4.12.0 - min value
Browse files Browse the repository at this point in the history
  • Loading branch information
Macacoazul01 committed Jun 27, 2024
1 parent a669dd6 commit 925757f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [4.12.0] - 2024-06-26
- Added `minValue` property to the controller. Now it is possible to define the minimum value the user is allowed to input. Everything lower than that will be forced to the minimum value.

## [4.11.0] - 2024-06-07
- Added `forceCursorToEnd` parameter: now you can define if the controller will always force the user to input the numbers on the end of the string.
- Fixed If enableNegative + showZeroValue properties are true, then negative #s cannot be entered! [22](https://github.com/IsaiasSantana/currency_textfield/issues/22).
Expand Down
31 changes: 31 additions & 0 deletions lib/currency_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ import 'dart:math';
///
/// Default: `null`
///
/// `minValue` lets you define the minimum allowed value of the controller.
///
/// Default: `null`
///
/// `startWithSeparator` lets you define if the controller starts with decimals activated.
///
/// Default: `true`
Expand All @@ -76,6 +80,7 @@ class CurrencyTextFieldController extends TextEditingController {
String _previewsText = '';
double _value = 0.0;
double? _maxValue;
double? _minValue;
bool _isNegative = false;
late bool _startWithSeparator;

Expand Down Expand Up @@ -120,6 +125,7 @@ class CurrencyTextFieldController extends TextEditingController {
bool currencyOnLeft = true,
bool enableNegative = true,
double? maxValue,
double? minValue,
bool startWithSeparator = true,
bool showZeroValue = false,
bool forceCursorToEnd = true,
Expand All @@ -136,6 +142,7 @@ class CurrencyTextFieldController extends TextEditingController {
_currencyOnLeft = currencyOnLeft,
_enableNegative = enableNegative,
_maxValue = maxValue,
_minValue = minValue,
_startWithSeparator = startWithSeparator,
_resetSeparator = !startWithSeparator,
_showZeroValue = showZeroValue,
Expand Down Expand Up @@ -228,6 +235,8 @@ class CurrencyTextFieldController extends TextEditingController {
}

void _updateValue() {
_checkMinValue();

if (_value < 0) {
if (!_enableNegative) {
_value = _value * -1;
Expand All @@ -251,6 +260,15 @@ class CurrencyTextFieldController extends TextEditingController {
}
}

///function to check if the value is lower than minValue.
void _checkMinValue() {
if (_minValue != null) {
if (_value < _minValue!) {
_value = _minValue!;
}
}
}

///function to replace current maxValue.
void replaceMaxValue(double newMaxvalue, {bool resetValue = false}) {
_maxValue = newMaxvalue;
Expand All @@ -264,6 +282,19 @@ class CurrencyTextFieldController extends TextEditingController {
_changeText();
}

///function to replace current minValue.
void replaceMinValue(double newMinvalue, {bool resetValue = false}) {
_minValue = newMinvalue;

if (resetValue) {
_value = 0;
} else {
_checkMinValue();
}

_changeText();
}

///check if the value is negative.
bool checkNegative() {
if (_enableNegative) {
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.11.0
version: 4.12.0
homepage: https://github.com/IsaiasSantana/currency_textfield
environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down
22 changes: 22 additions & 0 deletions test/currency_textfield_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,26 @@ void main() {
controller.text = "R\$ 7,00-";
expect(controller.text, "R\$ 7,00");
});

test('minValue', () {
final controller =
CurrencyTextFieldController(initDoubleValue: 300, minValue: 200);
final controller2 =
CurrencyTextFieldController(initDoubleValue: 100, minValue: 200);

controller.forceValue(initDoubleValue: 100);
expect(controller.textWithoutCurrencySymbol, '200,00');
expect(controller2.textWithoutCurrencySymbol, '200,00');
});

test('replace_minValue', () {
final controller =
CurrencyTextFieldController(initDoubleValue: 300, minValue: 200);

controller.forceValue(initDoubleValue: 100);
expect(controller.textWithoutCurrencySymbol, '200,00');
controller.replaceMinValue(0);
controller.forceValue(initDoubleValue: 50);
expect(controller.textWithoutCurrencySymbol, '50,00');
});
}

0 comments on commit 925757f

Please sign in to comment.