HTML窗口小部件中未显示本地扑动资产图像

问题描述 投票:0回答:2

我正在使用flutter_html呈现html代码,并且运行良好,但是img标签存在问题

img标记在图像来自网络时效果很好:

<img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">

但是当它是本地资产图片文件时不起作用注意:我可以在图像小部件中使用相同的图像,如下所示:

Image.asset('assets/images/logo_tst.png'),

但是它不会显示在html代码中,我尝试了所有这些方法:

String htmlUrl = '''  
<img src="file:///storage/assets/images/logo_tst.png" alt="web-img1" >
<img src="file:///assets/images/logo_tst.png" alt="web-img2">
<img src="file:///images/logo_tst.png" alt="web-img3">
''';

然后我称呼它:

Html( data:htmlUrl),

它只显示alt:

web-img1
web-img2
web-img3

我正在Android模拟器和设备上进行测试

我做错了什么?

谢谢

html image flutter dart assets
2个回答
1
投票

尝试:

"file:///android_asset/flutter_assets/" + url

换句话说:

"file:///android_asset/flutter_assets/assets/images/logo_tst.png"

0
投票

我做到了!我找到了一种解决方案,但并不明显,也没有任何地方记录在案!

经过几天寻找正确的方法来使用带有img标签的资产,我决定看一下github中flutter_html的源代码,并且发现了这两行:

else if (node.attributes['src'].startsWith('asset:')) {
                  final assetPath = node.attributes['src'].replaceFirst('asset:', '');

所以我尝试了如下img标签:

<img src="asset:assets/images/logo_tst.png" alt="web-img2">

通过pubspec.yaml中将我的图像文件声明为

assets/images/logo_tst.png

而且有效!!

正如我所说的,没有关于此的文档,希望有人会添加它

© www.soinside.com 2019 - 2024. All rights reserved.