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, // unused.
  TextEditingValue newValue,
) {
  final _TextEditingValueAccumulator formatState = _TextEditingValueAccumulator(newValue);
  assert(!formatState.debugFinalized);

  final Iterable<Match> matches = filterPattern.allMatches(newValue.text);
  Match? previousMatch;
  for (final Match match in matches) {
    assert(match.end >= match.start);
    // Compute the non-match region between this `Match` and the previous
    // `Match`. Depending on the value of `allow`, either the match region or
    // the non-match region is the banned pattern.
    //
    // The non-matching region.
    _processRegion(allow, previousMatch?.end ?? 0, match.start, formatState);
    assert(!formatState.debugFinalized);
    // The matched region.
    _processRegion(!allow, match.start, match.end, formatState);
    assert(!formatState.debugFinalized);

    previousMatch = match;
  }

  // Handle the last non-matching region between the last match region and the
  // end of the text.
  _processRegion(allow, previousMatch?.end ?? 0, newValue.text.length, formatState);
  assert(!formatState.debugFinalized);
  return formatState.finalize();
}