MoneyMaskedTextController constructor

MoneyMaskedTextController({
  1. double? initialValue,
  2. String decimalSeparator = ',',
  3. String thousandSeparator = '.',
  4. String rightSymbol = '',
  5. String leftSymbol = '',
  6. int precision = 2,
})

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);
}