LayerRasterizer class

Rasterizes Layers into ExportedLayers outside of a live editor session.

A layer can only be captured while it is mounted — Layer.captureAsPng returns null when the layer's repaint boundary has no context. That makes captured layers unavailable to anyone restoring a session from an exported state history: the layers deserialize fine, but they have never been laid out, so they cannot be baked into a render.

This controller closes that gap. capture mounts the given layers in the LayerRasterizerHost that carries it, waits for them to paint, and returns the result. The host paints them behind its own child, so they never become visible.

Mount exactly one host per rasterizer, above anything that captures:

final rasterizer = LayerRasterizer();

MaterialApp(
  builder: (context, child) => LayerRasterizerHost(
    rasterizer: rasterizer,
    child: child!,
  ),
);

final history = ImportStateHistory.fromMap(persistedHistory);
final captured = await rasterizer.capture(
  layers: history.stateHistory[history.editorPosition].layers,
  editorBodySize: history.lastRenderedImgSize,
  configs: myEditorConfigs,
);

Concurrent capture calls are serialized: only one set of layers is mounted at a time, so captures cannot read each other's repaint boundaries.

The layers passed to capture must not be mounted anywhere else while they are captured — no running editor, no LayerStack preview showing the same Layer instances. Every layer carries GlobalKeys (and a Hero tag) that LayerWidget attaches, so mounting it twice makes Flutter move the existing element into the host: the visible copy loses its content, and release builds do not report it. Layers already on screen need no host at all — call Layer.captureAllLayers on them directly. Both this and the one-host rule are asserted in debug mode.

Inheritance

Constructors

LayerRasterizer()

Properties

hashCode int
The hash code for this object.
no setterinherited
hasHost bool
Whether a LayerRasterizerHost is mounted for this rasterizer.
no setter
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
request LayerRasterizationRequest?
The layers currently waiting to be captured, or null when idle.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
attachHost() → void
Registers a mounted host. Called by LayerRasterizerHost and not intended for other callers — faking a host makes capture wait for layers that are never mounted.
capture({required List<Layer> layers, required Size editorBodySize, ProImageEditorConfigs configs = const ProImageEditorConfigs(), double? pixelRatio, double? basePixelRatio, bool applyTransforms = true, ImageByteFormat format = ui.ImageByteFormat.png, Future<void> awaitContentReady()?}) Future<List<ExportedLayer>>
Captures layers and returns their rendered bytes with layout metadata.
detachHost() → void
Unregisters a disposed host. Called by LayerRasterizerHost and not intended for other callers.
dispose() → void
Discards any resources used by the object.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited