onDragSelectionUpdate method Null safety

  1. @protected
void onDragSelectionUpdate(
  1. DragStartDetails startDetails,
  2. DragUpdateDetails updateDetails
)
protected">@protected

Handler for TextSelectionGestureDetector.onDragSelectionUpdate.

By default, it updates the selection location specified in the provided details objects.

See also:

Implementation

@protected
void onDragSelectionUpdate(DragStartDetails startDetails, DragUpdateDetails updateDetails) {
  if (!delegate.selectionEnabled) {
    return;
  }

  if (!_isShiftTapping) {
    // Adjust the drag start offset for possible viewport offset changes.
    final Offset startOffset = renderEditable.maxLines == 1
        ? Offset(renderEditable.offset.pixels - _dragStartViewportOffset, 0.0)
        : Offset(0.0, renderEditable.offset.pixels - _dragStartViewportOffset);

    return renderEditable.selectPositionAt(
      from: startDetails.globalPosition - startOffset,
      to: updateDetails.globalPosition,
      cause: SelectionChangedCause.drag,
    );
  }

  if (_shiftTapDragSelection!.isCollapsed
      || (defaultTargetPlatform != TargetPlatform.iOS
          && defaultTargetPlatform != TargetPlatform.macOS)) {
    return _extendSelection(updateDetails.globalPosition, SelectionChangedCause.drag);
  }

  // If the drag inverts the selection, Mac and iOS revert to the initial
  // selection.
  final TextSelection selection = editableText.textEditingValue.selection;
  final TextPosition nextExtent = renderEditable.getPositionForPoint(updateDetails.globalPosition);
  final bool isShiftTapDragSelectionForward =
      _shiftTapDragSelection!.baseOffset < _shiftTapDragSelection!.extentOffset;
  final bool isInverted = isShiftTapDragSelectionForward
      ? nextExtent.offset < _shiftTapDragSelection!.baseOffset
      : nextExtent.offset > _shiftTapDragSelection!.baseOffset;
  if (isInverted && selection.baseOffset == _shiftTapDragSelection!.baseOffset) {
    editableText.userUpdateTextEditingValue(
      editableText.textEditingValue.copyWith(
        selection: TextSelection(
          baseOffset: _shiftTapDragSelection!.extentOffset,
          extentOffset: nextExtent.offset,
        ),
      ),
      SelectionChangedCause.drag,
    );
  } else if (!isInverted
      && nextExtent.offset != _shiftTapDragSelection!.baseOffset
      && selection.baseOffset != _shiftTapDragSelection!.baseOffset) {
    editableText.userUpdateTextEditingValue(
      editableText.textEditingValue.copyWith(
        selection: TextSelection(
          baseOffset: _shiftTapDragSelection!.baseOffset,
          extentOffset: nextExtent.offset,
        ),
      ),
      SelectionChangedCause.drag,
    );
  } else {
    _extendSelection(updateDetails.globalPosition, SelectionChangedCause.drag);
  }
}