captureScreenshot method
Implementation
Future<File?> captureScreenshot(BuildContext context) async {
try {
RenderRepaintBoundary boundary = screenshotKey.currentContext
?.findRenderObject() as RenderRepaintBoundary;
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData? byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
if (byteData == null) {
throw ScreenshotException("Failed to convert image to byte data.");
}
Uint8List pngBytes = byteData.buffer.asUint8List();
final directory = await getApplicationDocumentsDirectory();
DateTime now = DateTime.now();
String timestamp = '${now.minute}_${now.second}';
final imagePath = File('${directory.path}/screenshot-$timestamp.png');
await imagePath.writeAsBytes(pngBytes);
return imagePath;
} catch (e) {
throw ScreenshotException(e.toString());
}
}