getEndpointsForSelection method

  1. @override
List<TextSelectionPoint>? getEndpointsForSelection(
  1. TextSelection selection
)
override

Returns the local coordinates of the endpoints of the given selection.

If the selection is collapsed (and therefore occupies a single point), the returned list is of length one. Otherwise, the selection is not collapsed and the returned list is of length two. In this case, however, the two points might actually be co-located (e.g., because of a bidirectional selection that contains some text but whose ends meet in the middle).

See also:

Implementation

@override
List<TextSelectionPoint>? getEndpointsForSelection(TextSelection selection) {
//    if (temp.isCollapsed && toolbar) {
//      // (mpcomplete): This doesn't work well at an RTL/LTR boundary.
////      final Offset caretOffset =
////          _textPainter.getOffsetForCaret(temp.extent, _caretPrototype);
//
//      final Offset caretOffset = _getCaretOffset(
//          effectiveOffset,
//          TextPosition(offset: temp.extentOffset, affinity: selection.affinity),
//          TextPosition(
//              offset: selection.extentOffset, affinity: selection.affinity));
//
//      final Offset start = Offset(0.0, preferredLineHeight) + caretOffset;
//
//      return <TextSelectionPoint>[TextSelectionPoint(start, null)];
//    } else

  if (!selection.isCollapsed) {
    layoutTextWithConstraints(constraints);
    TextSelection textPainterSelection = selection;
    if (hasSpecialInlineSpanBase) {
      textPainterSelection =
          convertTextInputSelectionToTextPainterSelection(text, selection);
    }

    // never drag over the over flow text span
    textPainterSelection = neverDragOnOverflow(textPainterSelection);

    final List<ui.TextBox> boxes = _textPainter.getBoxesForSelection(
      textPainterSelection,
      boxWidthStyle: selectionWidthStyle,
      boxHeightStyle: selectionHeightStyle,
    );

    if (boxes.isEmpty) {
      return null;
    }

    final Offset start = Offset(boxes.first.start, boxes.first.bottom);
    final Offset end = Offset(boxes.last.end, boxes.last.bottom);
    if (start == end) {
      return null;
    }

    return <TextSelectionPoint>[
      TextSelectionPoint(start, boxes.first.direction),
      TextSelectionPoint(end, boxes.last.direction),
    ];
  }

  return null;
}