execute method

  1. @override
Future<Uint8List?> execute(
  1. List arguments,
  2. Map<int, RxElement> elementMap
)
override

Implementation

@override
Future<Uint8List?> execute(
    List arguments, Map<int, RxElement> elementMap) async {
  try {
    RxElement? element =
        arguments.isNotEmpty ? elementMap[arguments[0]] : null;

    RenderRepaintBoundary? boundary;
    if (element != null) {
      boundary = _findAncestorRenderRepaintBoundary(element.element);
    } else {
      boundary = _findChildRenderRepaintBoundary(
         WidgetsBinding.instance.rootElement);
    }

    // Navigate the widget tree to find the RenderRepaintBoundary
    if (boundary != null) {
      // get device pixel ratio
      FlutterView view =
          WidgetsBinding.instance.platformDispatcher.views.first;
      var devicePixelRatio = view.devicePixelRatio;

      ui.Image image = await boundary.toImage(pixelRatio: devicePixelRatio);
      ByteData? byteData =
          await image.toByteData(format: ui.ImageByteFormat.png);
      Uint8List? pngBytes = byteData?.buffer.asUint8List();

      if (pngBytes != null) {
        return pngBytes;
      }
    }
  } catch (e) {
    // print("GetScreenshotMethod error $e");
  }

  return null;
}