extendSelection method

void extendSelection(
  1. Offset to, {
  2. required SelectionChangedCause cause,
})

Extends current selection to the position closest to specified offset.

Implementation

void extendSelection(Offset to, {required SelectionChangedCause cause}) {
  /// The below logic does not exactly match the native version because
  /// we do not allow swapping of base and extent positions.
  assert(_extendSelectionOrigin != null);
  final position = getPositionForOffset(to);

  if (position.offset < _extendSelectionOrigin!.baseOffset) {
    _handleSelectionChange(
      TextSelection(
        baseOffset: position.offset,
        extentOffset: _extendSelectionOrigin!.extentOffset,
        affinity: selection.affinity,
      ),
      cause,
    );
  } else if (position.offset > _extendSelectionOrigin!.extentOffset) {
    _handleSelectionChange(
      TextSelection(
        baseOffset: _extendSelectionOrigin!.baseOffset,
        extentOffset: position.offset,
        affinity: selection.affinity,
      ),
      cause,
    );
  }
}