capture method

Future<Uint8List?> capture()

Captures the wrapped widget as an image and returns its PNG bytes as a Uint8List.

Returns null if the widget's render boundary cannot be found or if an error occurs.

Implementation

Future<Uint8List?> capture() async {
  try {
    await WidgetsBinding.instance.endOfFrame;
    final boundary = captureKey.currentContext?.findRenderObject()
        as RenderRepaintBoundary?;
    if (boundary == null) return null;

    final image = await boundary.toImage(pixelRatio: 3.0);
    final byteData = await image.toByteData(format: ImageByteFormat.png);
    return byteData?.buffer.asUint8List();
  } catch (e) {
    debugPrint('Screenshot error: $e');
    return null;
  }
}