captureAsUiImage method

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

Implementation

Future<ui.Image?> captureAsUiImage({
  double? pixelRatio,
  Duration delay = const Duration(milliseconds: 20),
}) =>
    //Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
    Future.delayed(delay, () async {
      try {
        final findRenderObject =
            _containerKey.currentContext?.findRenderObject();

        if (findRenderObject == null) {
          return null;
        }

        final boundary = findRenderObject as RenderRepaintBoundary;
        final context = _containerKey.currentContext;
        var pixelRatioValue = pixelRatio;
        if (pixelRatioValue == null && context != null) {
          pixelRatioValue = MediaQuery.of(context).devicePixelRatio;
        }
        return await boundary.toImage(pixelRatio: pixelRatioValue ?? 1);
      } catch (exception) {
        _logger.severe('Failed to capture screenshot: $exception');
      }
      return null;
    });