captureWidget method

Future<Image?> captureWidget(
  1. BuildContext context,
  2. GlobalKey<State<StatefulWidget>> key
)

Captures a widget as an image using its global key.

Returns a high-resolution image of the widget bounded by RenderRepaintBoundary for smooth animation rendering.

Implementation

Future<ui.Image?> captureWidget(BuildContext context, GlobalKey key) async {
  RenderRepaintBoundary boundary =
      key.currentContext!.findRenderObject() as RenderRepaintBoundary;

  // Convert to image with device pixel ratio for high resolution.
  ui.Image image = await boundary.toImage(
    pixelRatio: MediaQuery.of(context).devicePixelRatio,
  );

  return image;
}