Skip to content

Commit

Permalink
4.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Macacoazul01 committed Feb 19, 2024
1 parent 705c150 commit daa619f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.3.1] - 2024-02-19
- Added `maxValue` property to the controller. Now it is possible to define the maximum value the user is allowed to input. Everything greater than that will be forced to the maximum value.
- Fixed input of `initIntValue` when `numberOfDecimals` was different than 2.

## [4.2.0] - 2024-02-17
- Added `textWithoutCurrencySymbol` getter to the controller. Now it is possible to return the number part of the controller as a String. Good to avoid round errors and to use with decimal package.
- Readme improvements.
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class _MyHomePageState extends State<MyHomePage> {
final CurrencyTextFieldController _controller2 = CurrencyTextFieldController(
initDoubleValue: -10, currencySeparator: ' -> ');
final CurrencyTextFieldController _controller3 =
CurrencyTextFieldController(initIntValue: -1000, enableNegative: false);
CurrencyTextFieldController(initIntValue: -1000, enableNegative: false, maxValue: 2000);

@override
Widget build(BuildContext context) {
Expand Down
28 changes: 23 additions & 5 deletions lib/currency_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ import 'dart:math';
///
/// `currencySeparator` lets you define the separator between the symbol and the value.
///
/// Default ` `
/// Default `' '`
///
/// `maxValue` lets you define the maximum allowed value of the controller.
///
/// Default `null`
///
class CurrencyTextFieldController extends TextEditingController {
final int _maxDigits, _numberOfDecimals;
Expand All @@ -53,9 +57,10 @@ class CurrencyTextFieldController extends TextEditingController {
_thousandSymbol,
_currencySeparator;
final bool _currencyOnLeft, _enableNegative;
final _onlyNumbersRegex = RegExp(r'[^\d]');
final RegExp _onlyNumbersRegex = RegExp(r'[^\d]');
late final String _symbolSeparator;

final double? _maxValue;

String _previewsText = '';
double _value = 0.0;
bool _isNegative = false;
Expand All @@ -81,14 +86,16 @@ class CurrencyTextFieldController extends TextEditingController {
int numberOfDecimals = 2,
bool currencyOnLeft = true,
bool enableNegative = true,
double? maxValue,
}) : _currencySymbol = currencySymbol,
_decimalSymbol = decimalSymbol,
_thousandSymbol = thousandSymbol,
_currencySeparator = currencySeparator,
_maxDigits = maxDigits,
_numberOfDecimals = numberOfDecimals,
_currencyOnLeft = currencyOnLeft,
_enableNegative = enableNegative {
_enableNegative = enableNegative,
_maxValue = maxValue {
_symbolSeparator = currencyOnLeft
? (_currencySymbol + _currencySeparator)
: (_currencySeparator + _currencySymbol);
Expand Down Expand Up @@ -134,6 +141,8 @@ class CurrencyTextFieldController extends TextEditingController {

_value = _getDoubleValueFor(string: clearText);

_checkMaxValue();

final String maskedValue = _composeCurrency(_applyMaskTo(value: _value));

_previewsText = maskedValue;
Expand All @@ -151,7 +160,7 @@ class CurrencyTextFieldController extends TextEditingController {
_value = initDoubleValue;
_updateValue();
} else if (initIntValue != null) {
_value = initIntValue / 100;
_value = initIntValue / pow(10, _numberOfDecimals);
_updateValue();
}
}
Expand All @@ -171,6 +180,15 @@ class CurrencyTextFieldController extends TextEditingController {
_setSelectionBy(offset: text.length);
}

///function to check if the value is greater than maxValue.
void _checkMaxValue() {
if (_maxValue != null) {
if (_value > _maxValue!) {
_value = _maxValue!;
}
}
}

///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.2.0
version: 4.3.1
homepage: https://github.com/IsaiasSantana/currency_textfield
environment:
sdk: '>=3.0.0 <4.0.0'
Expand Down

0 comments on commit daa619f

Please sign in to comment.