getBoxesForSelection method

List<Rect> getBoxesForSelection(
  1. TextSelection selection
)

Returns a list of rects that bound the given selection.

The selection must be a valid range (with TextSelection.isValid true).

Leading or trailing newline characters will be represented by zero-height Rects.

The method only returns Rects of glyphs that are entirely enclosed by the given selection: a multi-code-unit glyph will be excluded if only part of its code units are in selection.

Implementation

List<Rect> getBoxesForSelection(TextSelection selection) {
  assert(_debugAssertTextLayoutIsValid);
  assert(selection.isValid);
  assert(!_debugNeedsRelayout);
  final _TextPainterLayoutCacheWithOffset cachedLayout = _layoutCache!;
  final Offset offset = cachedLayout.paintOffset;
  if (!offset.dy.isFinite || !offset.dx.isFinite) {
    return <Rect>[];
  }
  final boxes = cachedLayout.paragraph.getBoxesForRange(
    selection.start,
    selection.end,
  );
  return offset == Offset.zero
      ? boxes
      : boxes
          .map((Rect box) => _shiftTextBox(box, offset))
          .toList(growable: false);
}