Android 是否可以将隐藏的小部件导出为图像?
我知道如何使用Android 是否可以将隐藏的小部件导出为图像?,android,flutter,dart,flutter-layout,Android,Flutter,Dart,Flutter Layout,我知道如何使用repainboundary 我想要的是一种将用户看不到的小部件保存为图像///通过首先旋转元素和渲染树,从给定小部件创建图像的方法, /// Creates an image from the given widget by first spinning up a element and render tree, /// then waiting for the given [wait] amount of time and then creating an image via
repainboundary
我想要的是一种将用户看不到的小部件
保存为图像
///通过首先旋转元素和渲染树,从给定小部件创建图像的方法,
/// Creates an image from the given widget by first spinning up a element and render tree,
/// then waiting for the given [wait] amount of time and then creating an image via a [RepaintBoundary].
///
/// The final image will be of size [imageSize] and the the widget will be layout, ... with the given [logicalSize].
Future<Uint8List> createImageFromWidget(Widget widget, {Duration wait, Size logicalSize, Size imageSize}) async {
final RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();
logicalSize ??= ui.window.physicalSize / ui.window.devicePixelRatio;
imageSize ??= ui.window.physicalSize;
assert(logicalSize.aspectRatio == imageSize.aspectRatio);
final RenderView renderView = RenderView(
window: null,
child: RenderPositionedBox(alignment: Alignment.center, child: repaintBoundary),
configuration: ViewConfiguration(
size: logicalSize,
devicePixelRatio: 1.0,
),
);
final PipelineOwner pipelineOwner = PipelineOwner();
final BuildOwner buildOwner = BuildOwner();
pipelineOwner.rootNode = renderView;
renderView.prepareInitialFrame();
final RenderObjectToWidgetElement<RenderBox> rootElement = RenderObjectToWidgetAdapter<RenderBox>(
container: repaintBoundary,
child: widget,
).attachToRenderTree(buildOwner);
buildOwner.buildScope(rootElement);
if (wait != null) {
await Future.delayed(wait);
}
buildOwner.buildScope(rootElement);
buildOwner.finalizeTree();
pipelineOwner.flushLayout();
pipelineOwner.flushCompositingBits();
pipelineOwner.flushPaint();
final ui.Image image = await repaintBoundary.toImage(pixelRatio: imageSize.width / logicalSize.width);
final ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
return byteData.buffer.asUint8List();
}
///然后等待给定的[wait]时间量,然后通过[RepaitBoundary]创建图像。
///
///最终图像的大小为[imageSize],小部件的大小为布局。。。使用给定的[logicalSize]。
Future createImageFromWidget(Widget Widget,{Duration wait,Size logicalSize,Size imageSize})异步{
最终RenderPaintBoundary RepaitBoundary=RenderPaintBoundary();
logicalSize???=ui.window.physicalSize/ui.window.devicePixelRatio;
imageSize???=ui.window.physicalSize;
断言(logicalSize.aspectRatio==imageSize.aspectRatio);
最终渲染视图渲染视图=渲染视图(
窗口:空,
子项:RenderPositionedBox(对齐方式:alignment.center,子项:重新绘制边界),
配置:视图配置(
大小:logicalSize,
devicePixelRatio:1.0,
),
);
最终管道所有者管道所有者=管道所有者();
最终BuildOwner BuildOwner=BuildOwner();
pipelineOwner.rootNode=renderView;
renderView.prepareInitialFrame();
最终RenderObjectToWidgetElement根元素=RenderObjectToWidgetAdapter(
容器:重新绘制边界,
孩子:小部件,
).AttachToRedertree(建筑业主);
buildOwner.buildScope(rootElement);
如果(等待!=null){
等待未来。延迟(等待);
}
buildOwner.buildScope(rootElement);
buildOwner.finalizeTree();
pipelineOwner.flushLayout();
pipelineOwner.flushCompositingBits();
pipelineOwner.flushPaint();
final ui.Image Image=wait repaitbundary.toImage(像素比率:imageSize.width/logicalSize.width);
final ByteData ByteData=wait image.toByteData(格式:ui.ImageByteFormat.png);
返回byteData.buffer.asUint8List();
}
你找到什么了吗?没有。但是我用了一种黑客手段来实现我想要的。我把
repaitbundary
放在所有其他小部件后面,这样用户就看不到它了。我的问题是我有一个包含许多项的列表视图,所以它不适合屏幕。我想做一个所有项目都可见的宽图像。现在我尝试使用缩放: