captureImage method

Future<ByteData?> captureImage({
  1. required double pixelRatio,
  2. required Size size,
})

Implementation

Future<ByteData?> captureImage(
    {required double pixelRatio, required Size size}) async {
  OffscreenRenderView renderView = OffscreenRenderView(
    configuration:
        ViewConfiguration(devicePixelRatio: pixelRatio, size: size),
    repaintBoundary: RenderRepaintBoundary(),
  );
  OffscreenWidgetTree widgetTree = OffscreenWidgetTree(
      renderView: renderView, focusManager: FocusManager.instance);

  widgetTree.attachWidget(this);
  final image = await widgetTree.toImage(pixelRatio: pixelRatio);
  final ByteData? byteData =
      await image.toByteData(format: ui.ImageByteFormat.png);
  return byteData;
}