paint method

void paint(
  1. Canvas canvas,
  2. Offset offset,
  3. Rect rect, {
  4. Offset? endOffset,
  5. TextPainter? wholeTextPainter,
})

rect: all text size

Implementation

void paint(Canvas canvas, Offset offset, Rect rect,
    {Offset? endOffset, TextPainter? wholeTextPainter}) {
  assert(_textPainterHelper.painter != null);

  if (paintBackground != null) {
    final bool handle = paintBackground!(
        this, canvas, offset, _textPainterHelper.painter, rect,
        endOffset: endOffset, wholeTextPainter: wholeTextPainter);
    if (handle) {
      return;
    }
  }

  final Rect textRect = offset & _textPainterHelper.painter!.size;

  ///top-right
  if (endOffset != null) {
    final Rect firstLineRect = offset &
        Size(rect.right - offset.dx, _textPainterHelper.painter!.height);

    if (clipBorderRadius != null) {
      canvas.save();
      canvas.clipPath(Path()
        ..addRRect(BorderRadius.only(
                topLeft: clipBorderRadius!.topLeft,
                bottomLeft: clipBorderRadius!.bottomLeft)
            .resolve(_textPainterHelper.painter!.textDirection)
            .toRRect(firstLineRect)));
    }

    ///start
    canvas.drawRect(firstLineRect, background);

    if (clipBorderRadius != null) {
      canvas.restore();
    }

    ///endOffset.y has deviation,so we calculate with text height
    ///print(((endOffset.dy - offset.dy) / _painter.height));
    final int fullLinesAndLastLine =
        ((endOffset.dy - offset.dy) / _textPainterHelper.painter!.height)
            .round();

    double y = offset.dy;
    for (int i = 0; i < fullLinesAndLastLine; i++) {
      y += _textPainterHelper.painter!.height;
      //last line
      if (i == fullLinesAndLastLine - 1) {
        final Rect lastLineRect = Offset(0.0, y) &
            Size(endOffset.dx, _textPainterHelper.painter!.height);
        if (clipBorderRadius != null) {
          canvas.save();
          canvas.clipPath(Path()
            ..addRRect(BorderRadius.only(
                    topRight: clipBorderRadius!.topRight,
                    bottomRight: clipBorderRadius!.bottomRight)
                .resolve(_textPainterHelper.painter!.textDirection)
                .toRRect(lastLineRect)));
        }
        canvas.drawRect(lastLineRect, background);
        if (clipBorderRadius != null) {
          canvas.restore();
        }
      } else {
        ///draw full line
        canvas.drawRect(
            Offset(0.0, y) &
                Size(rect.width, _textPainterHelper.painter!.height),
            background);
      }
    }
  } else {
    if (clipBorderRadius != null) {
      canvas.save();
      canvas.clipPath(Path()
        ..addRRect(clipBorderRadius!
            .resolve(_textPainterHelper.painter!.textDirection)
            .toRRect(textRect)));
    }

    canvas.drawRect(textRect, background);

    if (clipBorderRadius != null) {
      canvas.restore();
    }
  }
}