提交 0dd43440 authored 作者: ritheshSalyan's avatar ritheshSalyan

fix: logicalSize.aspectRatio == imageSize.aspectRatio

上级 b49a6b97
...@@ -44,12 +44,12 @@ class ScreenshotController { ...@@ -44,12 +44,12 @@ class ScreenshotController {
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308 //Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
return new Future.delayed(delay, () async { return new Future.delayed(delay, () async {
try { try {
ui.Image image = await captureAsUiImage( ui.Image? image = await captureAsUiImage(
delay: Duration.zero, delay: Duration.zero,
pixelRatio: pixelRatio, pixelRatio: pixelRatio,
); );
ByteData? byteData = ByteData? byteData =
await image.toByteData(format: ui.ImageByteFormat.png); await image?.toByteData(format: ui.ImageByteFormat.png);
Uint8List? pngBytes = byteData?.buffer.asUint8List(); Uint8List? pngBytes = byteData?.buffer.asUint8List();
return pngBytes; return pngBytes;
...@@ -59,16 +59,20 @@ class ScreenshotController { ...@@ -59,16 +59,20 @@ class ScreenshotController {
}); });
} }
Future<ui.Image> captureAsUiImage( Future<ui.Image?> captureAsUiImage(
{double? pixelRatio: 1, {double? pixelRatio: 1,
Duration delay: const Duration(milliseconds: 20)}) { Duration delay: const Duration(milliseconds: 20)}) {
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308 //Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
return new Future.delayed(delay, () async { return new Future.delayed(delay, () async {
try { try {
RenderRepaintBoundary boundary = this var findRenderObject = this
._containerKey ._containerKey
.currentContext .currentContext
?.findRenderObject() as RenderRepaintBoundary; ?.findRenderObject();
if(findRenderObject==null){
return null;
}
RenderRepaintBoundary boundary = findRenderObject as RenderRepaintBoundary;
BuildContext? context = _containerKey.currentContext; BuildContext? context = _containerKey.currentContext;
if (pixelRatio == null) { if (pixelRatio == null) {
if (context != null) if (context != null)
...@@ -89,7 +93,7 @@ class ScreenshotController { ...@@ -89,7 +93,7 @@ class ScreenshotController {
Size logicalSize = ui.window.physicalSize / ui.window.devicePixelRatio; Size logicalSize = ui.window.physicalSize / ui.window.devicePixelRatio;
Size imageSize = ui.window.physicalSize; Size imageSize = ui.window.physicalSize;
assert(logicalSize.aspectRatio == imageSize.aspectRatio); assert(logicalSize.aspectRatio.toPrecision(5) == imageSize.aspectRatio.toPrecision(5));
final RenderView renderView = RenderView( final RenderView renderView = RenderView(
window: ui.window, window: ui.window,
...@@ -168,3 +172,8 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin { ...@@ -168,3 +172,8 @@ class ScreenshotState extends State<Screenshot> with TickerProviderStateMixin {
); );
} }
} }
extension Ex on double {
double toPrecision(int n) => double.parse(toStringAsFixed(n));
}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论