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. 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:
- getLocalRectForCaret, which is the equivalent but for a TextPosition rather than a TextSelection.
Implementation
List<TextSelectionPoint> getEndpointsForSelection(TextSelection selection) {
assert(constraints != null);
_layoutText(constraints.maxWidth);
final Offset paintOffset = _paintOffset;
if (selection.isCollapsed) {
// TODO(mpcomplete): This doesn't work well at an RTL/LTR boundary.
final Offset caretOffset = _textPainter.getOffsetForCaret(selection.extent, _caretPrototype);
final Offset start = Offset(0.0, preferredLineHeight) + caretOffset + paintOffset;
return <TextSelectionPoint>[TextSelectionPoint(start, null)];
} else {
final List<ui.TextBox> boxes = _textPainter.getBoxesForSelection(selection);
final Offset start = Offset(boxes.first.start, boxes.first.bottom) + paintOffset;
final Offset end = Offset(boxes.last.end, boxes.last.bottom) + paintOffset;
return <TextSelectionPoint>[
TextSelectionPoint(start, boxes.first.direction),
TextSelectionPoint(end, boxes.last.direction),
];
}
}