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}) {
  _layoutText(
      minHeight: constraints.minHeight, maxHeight: constraints.maxHeight);
  final fromPosition =
      _textPainter.getPositionForOffset(globalToLocal(from - _paintOffset));
  final toPosition = (to == null)
      ? null
      : _textPainter.getPositionForOffset(globalToLocal(to - _paintOffset));

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

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