【问题标题】:local flutter asset image not showing in html widget本地颤振资产图像未显示在 html 小部件中
【发布时间】:2020-04-14 06:42:56
【问题描述】:

我正在使用 flutter_html 来呈现 html 代码,它运行良好,但我遇到了 img 标签的问题

当图片来自网络时,img 标签可以正常工作,如下所示:

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

但是当它是本地资产图像文件时它不起作用 注意:我可以像这样在 Image 小部件中使用相同的图像:

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


    【解决方案1】:

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

    在搜索了几天使用带有 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
    

    成功了!!!

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

    【讨论】:

    • 我试过这个,发生了一些事情,在使用之前我只能看到空矩形,但现在我可以看到问号。你能分享一些关于这类问题的信息吗?因为图像位于同一路径 assets/images/
    • @SumeetMourya 你找到解决办法了吗?
    • @MonikaPatel 还没有,现在我使用base64数据作为图像的临时解决方案。
    【解决方案2】:

    试试:

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

    换句话说:

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

    【讨论】:

    • 我试过了,但没用,assets/images/logo_tst.png 是 pubspec.yaml 中声明的 url
    【解决方案3】:

    似乎 webview_flutter 现在无法读取本地 CSS 或本地图像(jpg/png 文件)。我必须将其更改为内联 CSS 和 based64 图像,它可以完美运行。

    内联 CSS:

    <head>
    <style>
    body {
      background-color: linen;
    }
    
    h1 {
      color: maroon;
      margin-left: 40px;
    }
    </style>
    </head>
    

    内联 base64 图像:

    <img src="data:image/jpeg;base64,/9j/4RiDRXhpZgAATU0AKgA...">
    

    要在 Mac 中将图像转换为 base64 图像,您可以在终端中运行以下命令。

    openssl enc -base64 -in image.png -out image.b64
    

    【讨论】:

      猜你喜欢
      • 2022-08-10
      • 2020-11-15
      • 2020-07-31
      • 2018-10-24
      • 2019-12-20
      • 1970-01-01
      相关资源
      最近更新 更多