Ale*_*lex 8 google-maps google-maps-markers flutter flutter-image flutter-web
为了在谷歌地图google_maps_flutter上显示自定义小部件,我使用了一个非常方便的函数,可以将任何Widget
(包括包含图像的)转换为Image
,然后将其转换为Uint8List
我需要的。它在 iOS 和 Android 上运行良好。
Future createImageFromWidget( Widget widget){
final RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary();
final RenderView renderView = RenderView(
child: RenderPositionedBox(alignment: Alignment.center, child: repaintBoundary),
configuration: ViewConfiguration(size: const Size.square(300.0), devicePixelRatio: ui.window.devicePixelRatio),
window: null,
);
final PipelineOwner pipelineOwner = PipelineOwner()..rootNode = renderView;
renderView.prepareInitialFrame();
final BuildOwner buildOwner = BuildOwner(focusManager: FocusManager());
final RenderObjectToWidgetElement<RenderBox> rootElement = RenderObjectToWidgetAdapter<RenderBox>(
container: repaintBoundary,
child: Directionality(
textDirection: TextDirection.ltr,
child: IntrinsicHeight(child: IntrinsicWidth(child: widget)),
),
).attachToRenderTree(buildOwner);
buildOwner..buildScope(rootElement)..finalizeTree();
pipelineOwner..flushLayout()..flushCompositingBits()..flushPaint();
return repaintBoundary.toImage(pixelRatio: ui.window.devicePixelRatio)
.then((image) => image.toByteData(format: ui.ImageByteFormat.png))
.then((byteData) => byteData.buffer.asUint8List());
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是,当我尝试在网络上使用此功能时,出现以下错误:
不支持的操作:Web 上不支持 toImage
现在 Flutter 支持Image.toByteData
和Picture.toImage
#20750,但我不知道如何在我的案例中使用它。
实际上问题是 - 如何重新设计这个功能,使其也能在 Web 上运行?
谢谢,我很高兴有任何想法
--dart-define=FLUTTER_WEB_USE_SKIA
如果您添加参数,toImage() 就会起作用flutter build web
。
该方法在没有此参数的情况下在本地运行。
我在看到裁剪包文档后找到了这个解决方案,它也使用了 toImage()
编辑
通过 iPhone 访问应用程序时,我发现了一些问题,从小部件生成图像可以工作,但项目中的一些动画停止工作。
我发现了这个问题,我将仅使用参数进行一些测试,--dart-define=FLUTTER_WEB_AUTO_DETECT=true
看看是否可以在桌面、Android 和 iOS 上正确使用 Flutter Web。
编辑2
经过在 MacO 上的一些测试,toImage() 方法也可以正常工作。
归档时间: |
|
查看次数: |
4525 次 |
最近记录: |