captureFromWidget method

Future<Uint8List> captureFromWidget(
  1. Widget widget, {
  2. Duration delay = const Duration(seconds: 1),
  3. double? pixelRatio,
  4. BuildContext? context,
  5. Size? targetSize,
})

Value for delay should increase with widget tree size. Prefered value is 1 seconds

context parameter is used to Inherit App Theme and MediaQuery data.

Implementation

Future<Uint8List> captureFromWidget(
  Widget widget, {
  Duration delay: const Duration(seconds: 1),
  double? pixelRatio,
  BuildContext? context,
  Size? targetSize,
}) async {
  ui.Image image = await widgetToUiImage(widget,
      delay: delay,
      pixelRatio: pixelRatio,
      context: context,
      targetSize: targetSize);
  final ByteData? byteData =
      await image.toByteData(format: ui.ImageByteFormat.png);
  image.dispose();

  return byteData!.buffer.asUint8List();
}