setSelection method

void setSelection(
  1. TextSelection nextSelection,
  2. SelectionChangedCause cause
)

Implementation

void setSelection(TextSelection nextSelection, SelectionChangedCause cause) {
  if (textSelectionDelegate == null) {
    return;
  }
  if (nextSelection.isValid) {
    // The nextSelection is calculated based on _plainText, which can be out
    // of sync with the textSelectionDelegate.textEditingValue by one frame.
    // This is due to the render editable and editable text handle pointer
    // event separately. If the editable text changes the text during the
    // event handler, the render editable will use the outdated text stored in
    // the _plainText when handling the pointer event.
    //
    // If this happens, we need to make sure the new selection is still valid.
    final int textLength =
        textSelectionDelegate!.textEditingValue.text.length;
    nextSelection = nextSelection.copyWith(
      baseOffset: math.min(nextSelection.baseOffset, textLength),
      extentOffset: math.min(nextSelection.extentOffset, textLength),
    );
  }
  _setTextEditingValue(
    textSelectionDelegate!.textEditingValue
        .copyWith(selection: nextSelection),
    cause,
  );
}