formatEditUpdate method Null safety

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

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.


TextEditingValue formatEditUpdate(
  TextEditingValue oldValue, // unused.
  TextEditingValue newValue,
) {
  final _TextEditingValueAccumulator formatState = _TextEditingValueAccumulator(newValue);

  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);
    // The matched region.
    _processRegion(!allow, match.start, match.end, formatState);

    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);
  return formatState.finalize();