capture method
to capture widget to image by GlobalKey in RenderRepaintBoundary
Implementation
Future<Uint8List?> capture({
double pixelRatio = 6,
}) async {
try {
/// boundary widget by GlobalKey
RenderRepaintBoundary? boundary = containerKey.currentContext
?.findRenderObject() as RenderRepaintBoundary?;
/// convert boundary to image
final image = await boundary!.toImage(pixelRatio: pixelRatio);
/// set ImageByteFormat
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
final pngBytes = byteData?.buffer.asUint8List();
return pngBytes;
} catch (e) {
rethrow;
}
}