screenshot method

Future<void> screenshot()

Implementation

Future<void> screenshot() async {
  _screenshotting.value = true;
  await waitFrames(2);

  try {
    final context = _repaintBoundaryKey.currentContext;
    if (context == null) return;
    if (!context.mounted) return;

    final pixelRatio = MediaQuery.devicePixelRatioOf(context);
    final renderObject = _repaintBoundaryKey.currentContext
        ?.findRenderObject() as RenderRepaintBoundary?;

    if (renderObject == null) return;

    final image = await renderObject.toImage(pixelRatio: pixelRatio);
    _screenshotting.value = false;

    final bytes = await image.toByteData(format: ImageByteFormat.png);
    if (bytes == null) return;

    final item = DataWriterItem();
    item.add(Formats.png(bytes.buffer.asUint8List()));
    await SystemClipboard.instance?.write([item]);
  } finally {
    _screenshotting.value = false;
  }
}