From 925757ff5b1490759fbf453fe80fc930fcd2aa9e Mon Sep 17 00:00:00 2001 From: Gianluca Bettega Date: Wed, 26 Jun 2024 22:30:50 -0300 Subject: [PATCH] 4.12.0 - min value --- CHANGELOG.md | 3 +++ lib/currency_textfield.dart | 31 +++++++++++++++++++++++++++++++ pubspec.yaml | 2 +- test/currency_textfield_test.dart | 22 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21866b3..d101c34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/lib/currency_textfield.dart b/lib/currency_textfield.dart index 6f12651..438fc33 100644 --- a/lib/currency_textfield.dart +++ b/lib/currency_textfield.dart @@ -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` @@ -76,6 +80,7 @@ class CurrencyTextFieldController extends TextEditingController { String _previewsText = ''; double _value = 0.0; double? _maxValue; + double? _minValue; bool _isNegative = false; late bool _startWithSeparator; @@ -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, @@ -136,6 +142,7 @@ class CurrencyTextFieldController extends TextEditingController { _currencyOnLeft = currencyOnLeft, _enableNegative = enableNegative, _maxValue = maxValue, + _minValue = minValue, _startWithSeparator = startWithSeparator, _resetSeparator = !startWithSeparator, _showZeroValue = showZeroValue, @@ -228,6 +235,8 @@ class CurrencyTextFieldController extends TextEditingController { } void _updateValue() { + _checkMinValue(); + if (_value < 0) { if (!_enableNegative) { _value = _value * -1; @@ -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; @@ -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) { diff --git a/pubspec.yaml b/pubspec.yaml index a588c24..0971bcb 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.11.0 +version: 4.12.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 f702e10..8df2c89 100644 --- a/test/currency_textfield_test.dart +++ b/test/currency_textfield_test.dart @@ -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'); + }); }