updateEditingValue method

  1. @override
void updateEditingValue(
  1. TextEditingValue value
)
override

Requests that this client update its editing state to the given value.

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

Implementation

@override
void updateEditingValue(TextEditingValue value) {
  // This method handles text editing state updates from the platform text
  // input plugin. The [EditableText] may not have the focus or an open input
  // connection, as autofill can update a disconnected [EditableText].

  // Since we still have to support keyboard select, this is the best place
  // to disable text updating.
  if (!_shouldCreateInputConnection) {
    return;
  }

  if (_checkNeedsAdjustAffinity(value)) {
    value = value.copyWith(selection: value.selection.copyWith(affinity: _value.selection.affinity));
  }

  if (widget.readOnly) {
    // In the read-only case, we only care about selection changes, and reject
    // everything else.
    value = _value.copyWith(selection: value.selection);
  }
  _lastKnownRemoteTextEditingValue = value;

  if (value == _value) {
    // This is possible, for example, when the numeric keyboard is input,
    // the engine will notify twice for the same value.
    // Track at https://github.com/flutter/flutter/issues/65811
    return;
  }

  if (value.text == _value.text && value.composing == _value.composing) {
    // `selection` is the only change.
    SelectionChangedCause cause;
    if (_textInputConnection?.scribbleInProgress ?? false) {
      cause = SelectionChangedCause.scribble;
    } else if (_pointOffsetOrigin != null) {
      // For floating cursor selection when force pressing the space bar.
      cause = SelectionChangedCause.forcePress;
    } else {
      cause = SelectionChangedCause.keyboard;
    }
    _handleSelectionChanged(value.selection, cause);
  } else {
    if (value.text != _value.text) {
      // Hide the toolbar if the text was changed, but only hide the toolbar
      // overlay; the selection handle's visibility will be handled
      // by `_handleSelectionChanged`. https://github.com/flutter/flutter/issues/108673
      hideToolbar(false);
    }
    _currentPromptRectRange = null;

    final bool revealObscuredInput = _hasInputConnection
                                  && widget.obscureText
                                  && WidgetsBinding.instance.platformDispatcher.brieflyShowPassword
                                  && value.text.length == _value.text.length + 1;

    _obscureShowCharTicksPending = revealObscuredInput ? _kObscureShowLatestCharCursorTicks : 0;
    _obscureLatestCharIndex = revealObscuredInput ? _value.selection.baseOffset : null;
    _formatAndSetValue(value, SelectionChangedCause.keyboard);
  }

  if (_showBlinkingCursor && _cursorTimer != null) {
    // To keep the cursor from blinking while typing, restart the timer here.
    _stopCursorBlink(resetCharTicks: false);
    _startCursorBlink();
  }

  // Wherever the value is changed by the user, schedule a showCaretOnScreen
  // to make sure the user can see the changes they just made. Programmatic
  // changes to `textEditingValue` do not trigger the behavior even if the
  // text field is focused.
  _scheduleShowCaretOnScreen(withAnimation: true);
}