selectPositionAt method

void selectPositionAt({
  1. required Offset from,
  2. Offset? to,
  3. required SelectionChangedCause cause,
})

Select text between the global positions from and to.

from corresponds to the TextSelection.baseOffset, and to corresponds to the TextSelection.extentOffset.

Implementation

void selectPositionAt({ required Offset from, Offset? to, required SelectionChangedCause cause }) {
  _computeTextMetricsIfNeeded();
  final TextPosition fromPosition = _textPainter.getPositionForOffset(globalToLocal(from) - _paintOffset);
  final TextPosition? toPosition = to == null
    ? null
    : _textPainter.getPositionForOffset(globalToLocal(to) - _paintOffset);

  final int baseOffset = fromPosition.offset;
  final int extentOffset = toPosition?.offset ?? fromPosition.offset;

  final TextSelection newSelection = TextSelection(
    baseOffset: baseOffset,
    extentOffset: extentOffset,
    affinity: fromPosition.affinity,
  );

  _setSelection(newSelection, cause);
}