pushTransform method Null safety

TransformLayer? pushTransform(
  1. bool needsCompositing,
  2. Offset offset,
  3. Matrix4 transform,
  4. PaintingContextCallback painter,
  5. {TransformLayer? oldLayer}
)

Transform further painting using a matrix.

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 to pass to painter and the offset to the origin used by transform.

The transform argument is the Matrix4 with which to transform the coordinate system while calling painter. It should not include offset. It is applied effectively after applying offset.

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

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

TransformLayer? pushTransform(bool needsCompositing, Offset offset, Matrix4 transform, PaintingContextCallback painter, { TransformLayer? oldLayer }) {
  final Matrix4 effectiveTransform = Matrix4.translationValues(offset.dx, offset.dy, 0.0)
    ..multiply(transform)..translate(-offset.dx, -offset.dy);
  if (needsCompositing) {
    final TransformLayer layer = oldLayer ?? TransformLayer();
    layer.transform = effectiveTransform;
    pushLayer(
      layer,
      painter,
      offset,
      childPaintBounds: MatrixUtils.inverseTransformRect(effectiveTransform, estimatedBounds),
    );
    return layer;
  } else {
    canvas
      ..save()
      ..transform(effectiveTransform.storage);
    painter(this, offset);
    canvas.restore();
    return null;
  }
}