getBoxesForSelection method

List<TextBox> getBoxesForSelection(
  1. TextSelection selection,
  2. {BoxHeightStyle boxHeightStyle = ui.BoxHeightStyle.tight,
  3. BoxWidthStyle boxWidthStyle = ui.BoxWidthStyle.tight}
)

Returns a list of rects that bound the given selection.

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

The boxHeightStyle and boxWidthStyle arguments may be used to select the shape of the TextBoxs. These properties default to ui.BoxHeightStyle.tight and ui.BoxWidthStyle.tight respectively.

A given selection might have more than one rect if this text painter contains bidirectional text because logically contiguous text might not be visually contiguous.

Leading or trailing newline characters will be represented by zero-width TextBoxes.

The method only returns TextBoxes 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<TextBox> getBoxesForSelection(
  TextSelection selection, {
  ui.BoxHeightStyle boxHeightStyle = ui.BoxHeightStyle.tight,
  ui.BoxWidthStyle boxWidthStyle = ui.BoxWidthStyle.tight,
}) {
  assert(_debugAssertTextLayoutIsValid);
  assert(selection.isValid);
  assert(!_debugNeedsRelayout);
  final _TextPainterLayoutCacheWithOffset cachedLayout = _layoutCache!;
  final Offset offset = cachedLayout.paintOffset;
  if (!offset.dx.isFinite || !offset.dy.isFinite) {
    return <TextBox>[];
  }
  final List<TextBox> boxes = cachedLayout.paragraph.getBoxesForRange(
    selection.start,
    selection.end,
    boxHeightStyle: boxHeightStyle,
    boxWidthStyle: boxWidthStyle,
  );
  return offset == Offset.zero
    ? boxes
    : boxes.map((TextBox box) => _shiftTextBox(box, offset)).toList(growable: false);
}