pushClipRect method

ClipRectLayer? pushClipRect(
  1. bool needsCompositing,
  2. Offset offset,
  3. Rect clipRect,
  4. PaintingContextCallback painter,
  5. {Clip clipBehavior = Clip.hardEdge,
  6. ClipRectLayer? oldLayer}
)

Clip further painting using a rectangle.

The needsCompositing argument specifies whether the child needs compositing. Typically this 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.

When needsCompositing is false, this method will use a more efficient way to apply the layer effect than actually creating a layer.

The offset argument is the offset from the origin of the canvas' coordinate system to the origin of the caller's coordinate system.

The clipRect is the rectangle (in the caller's coordinate system) to use to clip the painting done by painter. It should not include the offset.

The painter callback will be called while the clipRect is applied. It is called synchronously during the call to pushClipRect.

The clipBehavior argument controls how the rectangle is clipped.

For the oldLayer argument, specify the layer created in the previous frame. This 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).

Implementation

ClipRectLayer? pushClipRect(bool needsCompositing, Offset offset, Rect clipRect, PaintingContextCallback painter, { Clip clipBehavior = Clip.hardEdge, ClipRectLayer? oldLayer }) {
  if (clipBehavior == Clip.none) {
    painter(this, offset);
    return null;
  }
  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;
  }
}