The ScreenshotController can be used to capture images of widgets that are currently part of the active widget tree. To do this, you must wrap the target widget in a Screenshot widget, which provides the necessary RepaintBoundary and links it to the controller via a GlobalKey.
Key Methods:
capture(): Returns the captured widget as a Uint8List (PNG format).captureAsUiImage(): Returns the captured widget as a ui.Image.captureAndSave(directory, {fileName, pixelRatio, delay}): Captures the widget and saves it directly to the specified directory as a file.
Important Note: A delay is required to ensure the widget has finished rendering. For larger widget trees, increase the delay (e.g., 1 second).
final ScreenshotController controller = ScreenshotController();
// In your build method:
Screenshot(
controller: controller,
child: MyWidgetToCapture(),
);
// To capture:
Uint8List? bytes = await controller.capture();
// To capture and save to a path:
String? filePath = await controller.captureAndSave(
'/path/to/directory',
fileName: 'my_image.png'
);