From 0dd43440f489984c02d06e522dbccba5ea935798 Mon Sep 17 00:00:00 2001 From: ritheshSalyan Date: Fri, 9 Jul 2021 13:48:33 +0530 Subject: [PATCH] fix: logicalSize.aspectRatio == imageSize.aspectRatio --- lib/screenshot.dart | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/screenshot.dart b/lib/screenshot.dart index f739108..d859978 100644 --- a/lib/screenshot.dart +++ b/lib/screenshot.dart @@ -44,12 +44,12 @@ class ScreenshotController { //Delay is required. See Issue https://github.com/flutter/flutter/issues/22308 return new Future.delayed(delay, () async { try { - ui.Image image = await captureAsUiImage( + ui.Image? image = await captureAsUiImage( delay: Duration.zero, pixelRatio: pixelRatio, ); ByteData? byteData = - await image.toByteData(format: ui.ImageByteFormat.png); + await image?.toByteData(format: ui.ImageByteFormat.png); Uint8List? pngBytes = byteData?.buffer.asUint8List(); return pngBytes; @@ -59,16 +59,20 @@ class ScreenshotController { }); } - Future captureAsUiImage( + Future 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 { - RenderRepaintBoundary boundary = this + var findRenderObject = this ._containerKey .currentContext - ?.findRenderObject() as RenderRepaintBoundary; + ?.findRenderObject(); + if(findRenderObject==null){ + return null; + } + RenderRepaintBoundary boundary = findRenderObject as RenderRepaintBoundary; BuildContext? context = _containerKey.currentContext; if (pixelRatio == null) { if (context != null) @@ -89,7 +93,7 @@ class ScreenshotController { Size logicalSize = ui.window.physicalSize / ui.window.devicePixelRatio; Size imageSize = ui.window.physicalSize; - assert(logicalSize.aspectRatio == imageSize.aspectRatio); + assert(logicalSize.aspectRatio.toPrecision(5) == imageSize.aspectRatio.toPrecision(5)); final RenderView renderView = RenderView( window: ui.window, @@ -168,3 +172,8 @@ class ScreenshotState extends State with TickerProviderStateMixin { ); } } + + +extension Ex on double { + double toPrecision(int n) => double.parse(toStringAsFixed(n)); +} \ No newline at end of file