userUpdateTextEditingValue method

  1. @override
void userUpdateTextEditingValue(
  1. TextEditingValue value,
  2. SelectionChangedCause? cause
)
override

Indicates that the user has requested the delegate to replace its current text editing state with value.

The new value is treated as user input and thus may subject to input formatting.

See also:

Implementation

@override
void userUpdateTextEditingValue(TextEditingValue value, SelectionChangedCause? cause) {
  // Compare the current TextEditingValue with the pre-format new
  // TextEditingValue value, in case the formatter would reject the change.
  // Do not automatically scroll to the caret during a selection drag.
  // Viewport scrolling is instead managed downstream by _bringIntoViewBySelectionState
  // (called via _formatAndSetValue), which ensures the active handle is kept in view.
  // Bypassing the default caret auto-scroll here prevents conflicts that would snap
  // the viewport back to the opposite, static selection end.
  // TODO(Renzo-Olivares): Remove this special case once the caret reveal paths are
  // consolidated, https://github.com/flutter/flutter/issues/192595.
  final bool shouldShowCaret =
      (widget.readOnly ? _value.selection != value.selection : _value != value) &&
      cause != SelectionChangedCause.drag;

  if (shouldShowCaret) {
    _scheduleShowCaretOnScreen(withAnimation: true);
  }

  // Even if the value doesn't change, it may be necessary to focus and build
  // the selection overlay. For example, this happens when right clicking an
  // unfocused field that previously had a selection in the same spot.
  if (value == textEditingValue) {
    if (!widget.focusNode.hasFocus) {
      _flagInternalFocus();
      widget.focusNode.requestFocus();
      _selectionOverlay ??= _createSelectionOverlay();
    }
    return;
  }

  _formatAndSetValue(value, cause, userInteraction: true);
}