Skip to content

Instantly share code, notes, and snippets.

@nodahikaru
Created August 14, 2020 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nodahikaru/fbb02811e391efb29f918e2e12bc7042 to your computer and use it in GitHub Desktop.
Save nodahikaru/fbb02811e391efb29f918e2e12bc7042 to your computer and use it in GitHub Desktop.
Capture and share images from RepaintBoundary
Future<void> _captureAndSharePng() async {
try {
RenderRepaintBoundary boundary = globalKey.currentContext.findRenderObject();
var image = await boundary.toImage();
ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
final tempDir = await getTemporaryDirectory();
final file = await new File('${tempDir.path}/image.png').create();
await file.writeAsBytes(pngBytes);
final channel = const MethodChannel('channel:me.alfian.share/share');
channel.invokeMethod('shareFile', 'image.png');
} catch(e) {
print(e.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment