formatEditUpdate method

  1. @override
TextEditingValue formatEditUpdate(
  1. TextEditingValue oldValue,
  2. TextEditingValue newValue
)
override

method to format user input into given mask

Implementation

@override
TextEditingValue formatEditUpdate(
    TextEditingValue oldValue, TextEditingValue newValue) {
  if (mask == '') {
    return newValue;
  }
  _newLength = newValue.text.length;
  _oldLength = oldValue.text.length;
  _isBackPressed = _newLength < _oldLength;

  if (_isBackPressed) {
    return _formatText(oldValue, newValue);
  } else {
    /// get new inserted character
    _newChar = newValue.text[oldValue.selection.baseOffset];
  }

  if (!_regex.contains(_newChar) && _oldLength == _newLength - 1) {
    /// if new value not contain allowable values return old value
    return oldValue;
  } else if (_oldLength == _maxLength && _newLength >= _maxLength) {
    /// if already inserted max chars return old value
    return oldValue;
  } else if (newValue.composing.start != -1 &&
      !_letters.contains(newValue.text[oldValue.selection.baseOffset]) &&
      _newLength > _oldLength) {
    if (_newLength > _oldLength) {
      return _formatText(
          oldValue,
          TextEditingValue(
            text: newValue.text.substring(0, _maxLength),
            selection: newValue.selection,
          ));
    }
    return oldValue;
  } else {
    /// format input value
    return _formatText(oldValue, newValue);
  }
}