How to render a local HTML file with flutter dart webview

后端 未结 8 1991
孤城傲影
孤城傲影 2020-12-02 18:48

I want to render a local HTML file stored in my phone memory in webview using flutter and dart.

8条回答
  •  佛祖请我去吃肉
    2020-12-02 19:36

    I have the same problem; this is how I solved it.

    1. Add webview_flutter to your project dependencies:

      webview_flutter: 0.3.14+1

    2. Create a WebViewController inside your screen/stateful widget

      WebViewController _controller;

    3. Implement the WebView and assign a value to the _controller using the onWebViewCreated property. Load the HTML file.

        WebView(
            initialUrl: '',
            onWebViewCreated: (WebViewController webViewController) async {
              _controller = webViewController;
              await loadHtmlFromAssets('legal/privacy_policy.html', _controller);
            },
          )
    
    1. Implement the function to load the file from the asset folder
        Future loadHtmlFromAssets(String filename, controller) async {
            String fileText = await rootBundle.loadString(filename);
            controller.loadUrl(Uri.dataFromString(fileText, mimeType: 'text/html', encoding: Encoding.getByName('utf-8')).toString());
        }
    

提交回复
热议问题