Skip to content

Commit

Permalink
fix: logicalSize.aspectRatio == imageSize.aspectRatio
Browse files Browse the repository at this point in the history
  • Loading branch information
ritheshSalyan committed Jul 9, 2021
1 parent b49a6b9 commit 0dd4344
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/screenshot.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -59,16 +59,20 @@ class ScreenshotController {
});
}

Future<ui.Image> captureAsUiImage(
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 {
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)
Expand All @@ -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,
Expand Down Expand Up @@ -168,3 +172,8 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
);
}
}


extension Ex on double {
double toPrecision(int n) => double.parse(toStringAsFixed(n));
}

0 comments on commit 0dd4344

Please sign in to comment.