setSelectionSilently method

void setSelectionSilently(
  1. TextSelection newSelection
)

Updates selection and syncs to text input connection for keyboard navigation.

This method flushes any pending buffer first to ensure IME state is consistent. Use this for programmatic selection changes that should sync with the platform.

Implementation

void setSelectionSilently(TextSelection newSelection) {
  if (_selection == newSelection) return;

  _flushBuffer();

  final textLength = text.length;
  final clampedBase = newSelection.baseOffset.clamp(0, textLength);
  final clampedExtent = newSelection.extentOffset.clamp(0, textLength);
  newSelection = newSelection.copyWith(
    baseOffset: clampedBase,
    extentOffset: clampedExtent,
  );

  _selection = newSelection;
  selectionOnly = true;

  if (connection != null && connection!.attached) {
    _lastSentText = text;
    _lastSentSelection = newSelection;
    connection!.setEditingState(
      TextEditingValue(text: _lastSentText!, selection: newSelection),
    );
  }

  notifyListeners();
}