takeWidgetShot method

Future<File> takeWidgetShot(
  1. GlobalKey<State<StatefulWidget>> previewKey,
  2. String imagePath, {
  3. double pixelRatio = 1.0,
})

Implementation

Future<File> takeWidgetShot(GlobalKey previewKey, String imagePath,
    {double pixelRatio = 1.0}) async {
  RenderRepaintBoundary boundary =
      previewKey.currentContext!.findRenderObject()! as RenderRepaintBoundary;
  ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);

  ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  Uint8List pngBytes = byteData!.buffer.asUint8List();

  await File(imagePath).create();
  await File('$imagePath').writeAsBytes(pngBytes);
  File imgFile = new File('$imagePath');

  return imgFile;
}