paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Size size,
  3. ExtendedTextSelectionRenderObject renderEditable
)
override

Paints within the bounds of a RenderEditable.

The given Canvas has the same coordinate space as the RenderEditable, which may be different from the coordinate space the RenderEditable's TextPainter uses, when the text moves inside the RenderEditable.

Paint operations performed outside of the region defined by the canvas's origin and the size parameter may get clipped, when RenderEditable's RenderEditable.clipBehavior is not Clip.none.

Implementation

@override
void paint(Canvas canvas, Size size,
    ExtendedTextSelectionRenderObject renderEditable) {
  final TextRange? range = highlightedRange;
  final Color? color = highlightColor;
  if (range == null || color == null || range.isCollapsed) {
    return;
  }

  highlightPaint.color = color;
  final TextPainter textPainter = renderEditable.textPainter;
  final List<TextBox> boxes = textPainter.getBoxesForSelection(
    TextSelection(baseOffset: range.start, extentOffset: range.end),
    boxHeightStyle: selectionHeightStyle,
    boxWidthStyle: selectionWidthStyle,
  );

  for (final TextBox box in boxes)
    canvas.drawRect(
      box.toRect().shift(renderEditable.paintOffset).intersect(
          Rect.fromLTWH(0, 0, textPainter.width, textPainter.height)),
      highlightPaint,
    );
}