cTakePicture function

Future<Uint8List> cTakePicture(
  1. GlobalKey<State<StatefulWidget>> key
)

Takes a screenshot of a widget using a GlobalKey and returns it as a Uint8List (PNG image).

Usage:

final GlobalKey<State<StatefulWidget>> key = GlobalKey();
// ...
Uint8List screenshot = await cTakePicture(key);

Implementation

Future<Uint8List> cTakePicture(GlobalKey<State<StatefulWidget>> key) async {
  RenderRepaintBoundary boundary =
      key.currentContext?.findRenderObject() as RenderRepaintBoundary;
  ui.Image image = await boundary.toImage(pixelRatio: 3.0);
  ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  Uint8List pngBytes = byteData!.buffer.asUint8List();
  return pngBytes;
}