captureAsUiImage method

Future<Image?> captureAsUiImage({
  1. double? pixelRatio = 1,
  2. Duration delay = const Duration(milliseconds: 20),
})

Implementation

Future<ui.Image?> captureAsUiImage(
    {double? pixelRatio: 1,
    Duration delay: const Duration(milliseconds: 20)}) {
  //Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
  return new Future.delayed(delay, () async {
    try {
      var findRenderObject =
          this._containerKey.currentContext?.findRenderObject();
      if (findRenderObject == null) {
        return null;
      }
      RenderRepaintBoundary boundary =
          findRenderObject as RenderRepaintBoundary;
      BuildContext? context = _containerKey.currentContext;
      if (pixelRatio == null) {
        if (context != null)
          pixelRatio = pixelRatio ?? MediaQuery.of(context).devicePixelRatio;
      }
      ui.Image image = await boundary.toImage(pixelRatio: pixelRatio ?? 1);
      return image;
    } catch (Exception) {
      throw (Exception);
    }
  });
}