paint method

  1. @override
void paint(
  1. PaintingContext context,
  2. Offset offset
)
override

Paint this render object into the given context at the given offset.

Subclasses should override this method to provide a visual appearance for themselves. The render object's local coordinate system is axis-aligned with the coordinate system of the context's canvas and the render object's local origin (i.e, x=0 and y=0) is placed at the given offset in the context's canvas.

Do not call this function directly. If you wish to paint yourself, call markNeedsPaint instead to schedule a call to this function. If you wish to paint one of your children, call PaintingContext.paintChild on the given context.

When painting one of your children (via a paint child function on the given context), the current canvas held by the context might change because draw operations before and after painting children might need to be recorded on separate compositing layers.

Implementation

@override
void paint(PaintingContext context, Offset offset) {
  if (!_paintsSelf) return; // Participates in IFC; painted by parent
  if ((_textPainter == null) || (_textPainter!.text == null)) {
    _layoutText(constraints);
  }
  if (_textPainter == null) return;
  // Apply horizontal alignment manually when painting within a bounded box.
  double dx = 0.0;
  if (_textPainter != null) {
    // Use the actual laid-out box width for alignment decisions.
    // Using constraints.maxWidth here can cause painting outside the allocated box
    // when maxWidth >> size.width (e.g., under RenderPositionedBox/Align).
    final double availableWidth = size.width.isFinite ? size.width : (_textPainter!.width);
    final double lineWidth = _textPainter!.width;
    TextAlign align = renderStyle.textAlign;
    if (align == TextAlign.start) {
      align = (renderStyle.direction == TextDirection.rtl) ? TextAlign.right : TextAlign.left;
    }
    switch (align) {
      case TextAlign.center:
        dx = (availableWidth - lineWidth) / 2.0;
        break;
      case TextAlign.right:
      case TextAlign.end:
        dx = (availableWidth - lineWidth);
        break;
      case TextAlign.justify:
      case TextAlign.left:
      case TextAlign.start:
        dx = 0.0;
        break;
    }
    if (dx.isNaN || !dx.isFinite) dx = 0.0;
  }

  _textPainter!.paint(context.canvas, offset + Offset(dx, 0));

  // Optional debug painting for text bounds/baseline when enabled globally.
  if (DebugFlags.debugPaintInlineLayoutEnabled) {
    final Paint outline = Paint()
      ..style = PaintingStyle.stroke
      ..strokeWidth = 1
      ..color = const Color(0xFF00AAFF);
    final Rect r = offset & size;
    context.canvas.drawRect(r, outline);
  }
}