0

我有一个加载 css 文件、字体和图像文件的 html 文件。目前我的 webview 加载时没有加载任何这些辅助文件,没有显示图像,没有显示 css。我正在尝试这样:

    loadHtmlFromAssets();
    return WebviewScaffold(
      appBar: AppBar(
        title: Text("Web", style: TextStyle(color: Colors.black),),
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(
            color: Colors.black
        ),
      ),
      url: 'about:blank',
      withZoom: false,
      withJavascript: true,
      allowFileURLs: true,
      withLocalUrl: true,
      localUrlScope: 'assets/maphtml/',
      withLocalStorage: true,
    );

loadHtmlFromAssets() async {
    String fileText = await rootBundle.loadString('assets/maphtml/zemelapis.html');
    flutterWebViewPlugin.reloadUrl(Uri.dataFromString(fileText, mimeType: 'text/html', encoding: Encoding.getByName("utf-8")).toString());
  }

html 中的资产具有 zemelapis.html 位置的假定前缀,例如

<script src="js/image-map-pro.min.js"></script>
4

1 回答 1

0

您可以在生成 Uri 之前将 js 内容添加到 html 正文

static void loadHtmlFromAssets(String fileText) async{

String htmlText = await rootBundle.loadString('assets/maphtml/zemelapis.html');
final String jsText = await rootBundle.loadString('assets/maphtml/js/image-map-pro.min.js'); //set exact js file path

//add js to html body
htmlText = '${htmlText}<script>${jsText}</script>';

flutterWebViewPlugin.reloadUrl(Uri.dataFromString(htmlText, mimeType: 'text/html', encoding: Encoding.getByName("utf-8")).toString());

}

于 2020-06-22T17:50:39.180 回答