selectPositionAt method

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

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,
  required SelectionChangedCause cause,
  Offset? to,
}) {
  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;
}