How to render a local HTML file with flutter dart webview

后端 未结 8 1962
孤城傲影
孤城傲影 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<void> 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());
        }
    
    0 讨论(0)
  • 2020-12-02 19:44

    You can use my plugin flutter_inappwebview, which has a lot of events, methods, and options compared to other plugins!

    To load an html file from your assets folder, you need to declare it in the pubspec.yaml file before use it (see more here).

    Example of a pubspec.yaml file:

    ...
    
    # The following section is specific to Flutter.
    flutter:
    
      # The following line ensures that the Material Icons font is
      # included with your application, so that you can use the icons in
      # the material Icons class.
      uses-material-design: true
    
      assets:
        - assets/index.html
    
    ...
    

    After that, you can simply use the initialFile parameter of the InAppWebView widget to load index.html into the WebView:

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    
    import 'package:flutter_inappwebview/flutter_inappwebview.dart';
    
    Future main() async {
      runApp(new MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => new _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
    
      @override
      void initState() {
        super.initState();
      }
    
      @override
      void dispose() {
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
            home: InAppWebViewPage()
        );
      }
    }
    
    class InAppWebViewPage extends StatefulWidget {
      @override
      _InAppWebViewPageState createState() => new _InAppWebViewPageState();
    }
    
    class _InAppWebViewPageState extends State<InAppWebViewPage> {
      InAppWebViewController webView;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
                title: Text("InAppWebView")
            ),
            body: Container(
                child: Column(children: <Widget>[
                  Expanded(
                    child: Container(
                      child: InAppWebView(
                        initialFile: "assets/index.html",
                        initialHeaders: {},
                        initialOptions: InAppWebViewWidgetOptions(
                          inAppWebViewOptions: InAppWebViewOptions(
                            debuggingEnabled: true,
                          ),
                        ),
                        onWebViewCreated: (InAppWebViewController controller) {
                          webView = controller;
                        },
                        onLoadStart: (InAppWebViewController controller, String url) {
    
                        },
                        onLoadStop: (InAppWebViewController controller, String url) {
    
                        },
                      ),
                    ),
                  ),
                ]))
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题