【问题标题】:how to show local html folder in webview如何在 webview 中显示本地 html 文件夹
【发布时间】:2019-07-24 03:52:27
【问题描述】:

我想在 webview 中显示一个 html 文件夹,其中包括 CSS 和 javascript。 喜欢这个文件夹:javascript_button

并且这个文件夹应该在内存的 sdcard 路径上,而不是在应用程序资产文件夹中。 我尝试了各种插件,如webview_flutterflutter_inappbrowserflutter_webview_plugin,但没有得到结果。 使用以下方法,我只能读取 index.html 并且文件夹内没有显示其他相关文件。 请帮我解决这个问题。

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  String filePath = '/sdcard/Download/button/index.html';
  final flutterWebViewPlugin = FlutterWebviewPlugin();

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      routes: {
        "/": (_) => WebviewScaffold(
          url:new Uri.dataFromString(readFileAsync(filePath), mimeType:
               'text/html').toString(),
          appBar: AppBar(
            title: Text("webview"),
          ),
          withJavascript: true,
          supportMultipleWindows: true,
          withLocalUrl: true,
          allowFileURLs: true,
          withLocalStorage: true,
        )
      },
    );
  }
  
  String readFileAsync(String path) {
    String contents = new File('$path').readAsStringSync();
    print(contents);
    return contents;
  }
}

【问题讨论】:

    标签: html flutter webview dart


    【解决方案1】:

    在 Scaffold 中使用 Webview_Flutter 插件使用此代码:

      WebView(
                initialUrl: '',
                javascriptMode: JavascriptMode.unrestricted,
                onWebViewCreated: (WebViewController webViewController) {
                  _webViewController = webViewController;
                  _loadHtmlFromAssets();
                },
              ),
    
    // function to load Assets files
    //filePath in bellow code for ex.(String filePath = '/assests/files/abc1.html';)
    
    
    _loadHtmlFromAssets() async {
        String fileHtmlContents = await rootBundle.loadString(filePath);
        _webViewController.loadUrl(Uri.dataFromString(fileHtmlContents,
            mimeType: 'text/html', encoding: Encoding.getByName('utf-8'))
            .toString());
      }
    

    【讨论】:

      猜你喜欢
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      • 2021-08-25
      相关资源
      最近更新 更多