如何显示HTML资产文件? [英] How to display an HTML asset file?

查看:84
本文介绍了如何显示HTML资产文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的资产目录中有一个 .html 文件。

I have an .html file in my assets directory.

如何在Flutter中显示/渲染它?

How can I display / render it in Flutter?

推荐答案

Flutter团队的软件包是昨天发布:

The package from the Flutter Team was released yesterday:

webview_flutter

如何加载本地资产:

将文件添加到项目并更新您的发布规范:

Add the file to the project and update your pubspec:

//...
assets:
    //...
    - assets/yourFile.html
    //...




在您的小部件中:


In your widget:

child: FutureBuilder<String>(
  future: LocalLoader().loadLocal(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      //                                return Text("${snapshot.data}");
      return WebView(
        initialUrl: new Uri.dataFromString(snapshot.data,
                mimeType: 'text/html')
            .toString(),
        // maybe you Uri.dataFromString(snapshot.data, mimeType: 'text/html', encoding: Encoding.getByName("UTF-8")).toString()
        javascriptMode: JavascriptMode.unrestricted,
      );
    } else if (snapshot.hasError) {
      return Text("${snapshot.error}");
    }

    return CircularProgressIndicator();
  },
),

从文件中加载文本:

class LocalLoader {
  Future<String> loadLocal() async {
    return await rootBundle.loadString('assets/yourFile.html');
  }
}

这篇关于如何显示HTML资产文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆