formatEditUpdate method

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

Called when text is being typed or cut/copy/pasted in the EditableText.

You can override the resulting text based on the previous text value and the incoming new text value.

When formatters are chained, oldValue reflects the initial value of TextEditingValue at the beginning of the chain.

Implementation

@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
  final newText = newValue.text;
  final oldText = oldValue.text;

  if (newText.length == 0 || newText.length < oldText.length) {
    return newValue;
  }

  final pasted = (newText.length - oldText.length).abs() > 1;
  final mask = _masks.firstWhere((value) {
    final maskValue = pasted ? value.replaceAll(_separator, '') : value;
    return newText.length <= maskValue.length;
  }, orElse: () => '');

  if (mask.isEmpty) {
    return oldValue;
  }

  final needReset = (_prevMask != mask || newText.length - oldText.length > 1);
  _prevMask = mask;

  if (needReset) {
    final text = newText.replaceAll(_separator, '');
    String resetValue = '';
    int sep = 0;

    for (int i = 0; i < text.length; i++) {
      if (mask[i + sep] == _separator) {
        resetValue += _separator;
        ++sep;
      }
      resetValue += text[i];
    }

    return TextEditingValue(
      text: resetValue,
      selection: TextSelection.collapsed(offset: resetValue.length),
    );
  }

  if (newText.length < mask.length && mask[newText.length - 1] == _separator) {
    final text = '$oldText$_separator${newText.substring(newText.length - 1)}';
    return TextEditingValue(
      text: text,
      selection: TextSelection.collapsed(offset: text.length),
    );
  }

  return newValue;
}