MoneyMaskedTextController constructor
MoneyMaskedTextController({})
Creates a controller for an editable text field for currencies
This constructor treats a null initialValue
argument as if it were the
empty
The decimalSeparator and thousandSeparator are useful to ensure the formatted number follows the desired localization.
The rightSymbol and leftSymbol are actually suffixes and prefixes and can have multiple characters
precision is used to controll the decimal cases
Implementation
MoneyMaskedTextController({
double? initialValue,
this.decimalSeparator = ',',
this.thousandSeparator = '.',
this.rightSymbol = '',
this.leftSymbol = '',
this.precision = 2,
}) {
_validateConfig();
_shouldApplyTheMask = true;
addListener(() {
if (_shouldApplyTheMask) {
var parts = _getOnlyNumbers(text).split('').toList(growable: true);
if (parts.isNotEmpty) {
// Ensures that the list of parts contains the minimum amount of
// characters to fit the precision
if (parts.length < precision + 1) {
parts = [...List.filled(precision, '0'), ...parts];
}
parts.insert(parts.length - precision, '.');
updateValue(double.parse(parts.join()));
}
}
});
updateValue(initialValue);
}