addToScene method Null safety

  1. @override
void addToScene(
  1. SceneBuilder builder
)
override

Override this method to upload this layer to the engine.

Return the engine layer for retained rendering. When there's no corresponding engine layer, null is returned.

Implementation

@override
void addToScene(ui.SceneBuilder builder) {
  assert(alpha != null);

  // Don't add this layer if there's no child.
  bool enabled = firstChild != null;
  if (!enabled) {
    // Ensure the engineLayer is disposed.
    engineLayer = null;
    // TODO(dnfield): Remove this if/when we can fix https://github.com/flutter/flutter/issues/90004
    return;
  }

  assert(() {
    enabled = enabled && !debugDisableOpacityLayers;
    return true;
  }());

  final int realizedAlpha = alpha!;
  // The type assertions work because the [alpha] setter nulls out the
  // engineLayer if it would have changed type (i.e. changed to or from 255).
  if (enabled && realizedAlpha < 255) {
    assert(_engineLayer is ui.OpacityEngineLayer?);
    engineLayer = builder.pushOpacity(
      realizedAlpha,
      offset: offset,
      oldLayer: _engineLayer as ui.OpacityEngineLayer?,
    );
  } else {
    assert(_engineLayer is ui.OffsetEngineLayer?);
    engineLayer = builder.pushOffset(
      offset.dx,
      offset.dy,
      oldLayer: _engineLayer as ui.OffsetEngineLayer?,
    );
  }
  addChildrenToScene(builder);
  builder.pop();
}