selectPositionAt method

void selectPositionAt ({@required Offset from, Offset to, @required SelectionChangedCause cause })

Select text between the global positions from and to.

Implementation

void selectPositionAt({ @required Offset from, Offset to, @required SelectionChangedCause cause }) {
  assert(cause != null);
  assert(from != null);
  _layoutText(minWidth: constraints.minWidth, maxWidth: constraints.maxWidth);
  if (onSelectionChanged == null) {
    return;
  }
  final TextPosition fromPosition = _textPainter.getPositionForOffset(globalToLocal(from - _paintOffset));
  final TextPosition toPosition = to == null
    ? null
    : _textPainter.getPositionForOffset(globalToLocal(to - _paintOffset));

  int baseOffset = fromPosition.offset;
  int extentOffset = fromPosition.offset;
  if (toPosition != null) {
    baseOffset = math.min(fromPosition.offset, toPosition.offset);
    extentOffset = math.max(fromPosition.offset, toPosition.offset);
  }

  final TextSelection newSelection = TextSelection(
    baseOffset: baseOffset,
    extentOffset: extentOffset,
    affinity: fromPosition.affinity,
  );
  // Call [onSelectionChanged] only when the selection actually changed.
  _handleSelectionChange(newSelection, cause);
  _setFallbackAffinity(newSelection.affinity);
}