paint method Null safety

  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 (child == null) {
    layer = null;
    return;
  }

  _updateClip();
  final RRect offsetRRect = _clip!.shift(offset);
  final Rect offsetBounds = offsetRRect.outerRect;
  final Path offsetRRectAsPath = Path()..addRRect(offsetRRect);
  bool paintShadows = true;
  assert(() {
    if (debugDisableShadows) {
      if (elevation > 0.0) {
        context.canvas.drawRRect(
          offsetRRect,
          Paint()
            ..color = shadowColor
            ..style = PaintingStyle.stroke
            ..strokeWidth = elevation * 2.0,
        );
      }
      paintShadows = false;
    }
    return true;
  }());

  final Canvas canvas = context.canvas;
  if (elevation != 0.0 && paintShadows) {
    // The drawShadow call doesn't add the region of the shadow to the
    // picture's bounds, so we draw a hardcoded amount of extra space to
    // account for the maximum potential area of the shadow.
    // TODO(jsimmons): remove this when Skia does it for us.
    canvas.drawRect(
      offsetBounds.inflate(20.0),
      _transparentPaint,
    );
    canvas.drawShadow(
      offsetRRectAsPath,
      shadowColor,
      elevation,
      color.alpha != 0xFF,
    );
  }
  final bool usesSaveLayer = clipBehavior == Clip.antiAliasWithSaveLayer;
  if (!usesSaveLayer) {
    canvas.drawRRect(
      offsetRRect,
      Paint()..color = color
    );
  }
  layer = context.pushClipRRect(
    needsCompositing,
    offset,
    Offset.zero & size,
    _clip!,
    (PaintingContext context, Offset offset) {
      if (usesSaveLayer) {
        // If we want to avoid the bleeding edge artifact
        // (https://github.com/flutter/flutter/issues/18057#issue-328003931)
        // using saveLayer, we have to call drawPaint instead of drawPath as
        // anti-aliased drawPath will always have such artifacts.
        context.canvas.drawPaint( Paint()..color = color);
      }
      super.paint(context, offset);
    },
    oldLayer: layer as ClipRRectLayer?,
    clipBehavior: clipBehavior,
  );

  assert(() {
    layer?.debugCreator = debugCreator;
    return true;
  }());
}