selectPositionAt method

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

Select text between the global positions from and to.

Returns the new selection. Note that the returned value may not be yet reflected in the latest widget state.

Returns null if no change occurred.

Implementation

@override
TextSelection? selectPositionAt({
  required Offset from,
  Offset? to,
  required SelectionChangedCause cause,
}) {
  // _layoutText(minWidth: constraints.minWidth, maxWidth: constraints.maxWidth);
  if (onSelectionChanged == null) {
    return null;
  }
  final fromPosition = getPositionForOffset(from);
  final toPosition = to == null ? null : getPositionForOffset(to);

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

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

  // Call [onSelectionChanged] only when the selection actually changed.
  _handleSelectionChange(newSelection, cause);
  return newSelection;
}