MoneyMaskedTextController constructor

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

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