captureScreenshot static method
Implementation
static Future<String> captureScreenshot() async {
try {
String data = "";
final globalKey = PxContextAware.instance.globalKey;
// Make sure currentContext & globalKey is valid
if (globalKey.currentContext == null ||
globalKey.currentContext?.owner == null) {
Logger.d("Can't capture screen : GlobalKey issue. "
"Please assign 'PxContextAware.instance.globalKey' correctly");
return data;
}
final renderObject = globalKey.currentContext?.findRenderObject();
// Make sure main widget was wrapped with [PxWidget]
if (renderObject == null ||
renderObject.owner == null ||
renderObject is! RenderRepaintBoundary) {
Logger.d("Can't capture screen: PxWidget issue. "
"Please wrap your main widget with PxWidget");
return data;
}
final Image image = await renderObject.toImage();
final ByteData? byteData =
await image.toByteData(format: ImageByteFormat.png);
if (byteData == null || byteData.lengthInBytes == 0) {
Logger.d('Invalid byte data for captured image');
return data;
}
final Uint8List pngBytes = byteData.buffer.asUint8List();
data = base64Encode(pngBytes);
return data;
} on Exception catch (e) {
Logger.d("Can't capture screen: $e");
return "";
}
}