getEndpointsForSelection method
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.
See also:
- getLocalRectForCaret, which is the equivalent but for a TextPosition rather than a TextSelection.
Implementation
List<TextSelectionPoint> getEndpointsForSelection(TextSelection selection) {
_computeTextMetricsIfNeeded();
final paintOffset = _paintOffset;
final boxes = selection.isCollapsed
? <Rect>[]
: _textPainter.getBoxesForSelection(selection);
if (boxes.isEmpty) {
final caretOffset =
_textPainter.getOffsetForCaret(selection.extent, _caretPrototype);
final start = Offset(preferredLineWidth, 0.0) + caretOffset + paintOffset;
return <TextSelectionPoint>[TextSelectionPoint(start, TextDirection.ltr)];
} else {
final start = Offset(boxes.first.left, boxes.first.top) + paintOffset;
final end = Offset(boxes.last.right, boxes.last.bottom) + paintOffset;
return <TextSelectionPoint>[
TextSelectionPoint(start, TextDirection.ltr),
TextSelectionPoint(end, TextDirection.ltr),
];
}
}