shareScreenshot method

dynamic shareScreenshot(
  1. GlobalKey<State<StatefulWidget>> previewContainer,
  2. int originalSize,
  3. String title,
  4. String name,
  5. String mimeType, {
  6. String text = '',
})

takes screenshot and invokes share

Implementation

shareScreenshot(GlobalKey previewContainer, int originalSize, String title,
    String name, String mimeType,
    {String text = ''}) async {
  RenderRepaintBoundary boundary = previewContainer.currentContext!
      .findRenderObject() as RenderRepaintBoundary;
  double pixelRatio = originalSize /
      MediaQuery.of(previewContainer.currentContext!).size.width;
  ui.Image image = await boundary.toImage(pixelRatio: pixelRatio);
  ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  Uint8List pngBytes = byteData!.buffer.asUint8List();
  try {
    await file(title, name, pngBytes, mimeType, text: text);
  } catch (e) {
    print('error while sharing: $e');
  }
}