pushClipRect method

ClipRectLayer pushClipRect (bool needsCompositing, Offset offset, Rect clipRect, PaintingContextCallback painter, { Clip clipBehavior: Clip.hardEdge, ClipRectLayer oldLayer })

Clip further painting using a rectangle.

{@template flutter.rendering.object.needsCompositing}

  • needsCompositing is whether the child needs compositing. Typically matches the value of RenderObject.needsCompositing for the caller. If false, this method returns null, indicating that a layer is no longer necessary. If a render object calling this method stores the oldLayer in its RenderObject.layer field, it should set that field to null. {@end template}
  • offset is the offset from the origin of the canvas' coordinate system to the origin of the caller's coordinate system.
  • clipRect is rectangle (in the caller's coordinate system) to use to clip the painting done by painter.
  • painter is a callback that will paint with the clipRect applied. This function calls the painter synchronously.
  • clipBehavior controls how the rectangle is clipped. {@template flutter.rendering.object.oldLayer}
  • oldLayer is the layer created in the previous frame. Specifying the old layer gives the engine more information for performance optimizations. Typically this is the value of RenderObject.layer that a render object creates once, then reuses for all subsequent frames until a layer is no longer needed (e.g. the render object no longer needs compositing) or until the render object changes the type of the layer (e.g. from opacity layer to a clip rect layer). {@end template}

Implementation

ClipRectLayer pushClipRect(bool needsCompositing, Offset offset, Rect clipRect, PaintingContextCallback painter, { Clip clipBehavior = Clip.hardEdge, ClipRectLayer oldLayer }) {
  final Rect offsetClipRect = clipRect.shift(offset);
  if (needsCompositing) {
    final ClipRectLayer layer = oldLayer ?? ClipRectLayer();
    layer
      ..clipRect = offsetClipRect
      ..clipBehavior = clipBehavior;
    pushLayer(layer, painter, offset, childPaintBounds: offsetClipRect);
    return layer;
  } else {
    clipRectAndPaint(offsetClipRect, clipBehavior, offsetClipRect, () => painter(this, offset));
    return null;
  }
}