Как заставить веб-просмотр Flutter работать с локальными активами, загруженными из локального HTML?

У меня есть файл html, который загружает файлы css, шрифтов и изображений. В настоящее время мой веб-просмотр загружается без загрузки каких-либо дополнительных файлов, изображения не отображаются, 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>

person Gintas_    schedule 01.03.2020    source источник


Ответы (1)


Вы можете добавить содержимое js в тело html перед созданием Uri

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());

}

person raslan    schedule 22.06.2020