Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webview 如何从资产加载完整的网站?_Webview_Flutter_Local Web Server - Fatal编程技术网

Webview 如何从资产加载完整的网站?

Webview 如何从资产加载完整的网站?,webview,flutter,local-web-server,Webview,Flutter,Local Web Server,是否有可能从本地资产加载完整的网站(包括相关文件)?我用flatter插件webview\u flatter尝试了它,并加载了index.html Future<String> loadLocal() async { return await rootBundle.loadString('assets/mywebsite/index.html'); } Future loadLocal()异步{ return wait rootBundle.loadString('

是否有可能从本地资产加载完整的网站(包括相关文件)?我用flatter插件
webview\u flatter
尝试了它,并加载了index.html

  Future<String> loadLocal() async {
    return await rootBundle.loadString('assets/mywebsite/index.html');
  }
Future loadLocal()异步{
return wait rootBundle.loadString('assets/mywebsite/index.html');
}
只呈现html代码,关联的Java脚本不起作用。

您可以按照

同时,您可以在Dart中实现一个web服务器,该服务器为来自资产的文件提供服务,并将webview指向该集成web服务器


是关于如何在Dart中做到这一点的。

您也可以尝试我的插件,它是一个颤振插件,允许您添加内联网络视图或打开应用程序内浏览器窗口,并具有许多事件、方法和选项来控制网络视图

要从资产加载完整的网站,您需要在
pubspec.yaml
文件中声明相应的文件:

。。。
#以下章节专门针对颤振。
颤振:
#下一行确保材质图标字体为
#包括在应用程序中,以便您可以使用中的图标
#材质图标类。
使用材料设计:真实
资产:
-assets/index.html
-资产/page-1.html
-资产/page-2.html
-资产/js/
-资产/css/
-资产/图像/
...
然后,您可以使用
InAppWebView
小部件的
initialFile
参数加载
index.html
文件:

导入'dart:async';
进口“包装:颤振/材料.省道”;
导入“包:flatter_inappwebview/flatter_inappwebview.dart”;
Future main()异步{
runApp(新的MyApp());
}
类MyApp扩展了StatefulWidget{
@凌驾
_MyAppState createState()=>new_MyAppState();
}
类MyAppState扩展了状态{
@凌驾
void initState(){
super.initState();
}
@凌驾
无效处置(){
super.dispose();
}
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:InAppWebViewPage()
);
}
}
类InAppWebViewPage扩展了StatefulWidget{
@凌驾
_InAppWebViewPageState createState()=>新建;
}
类_InAppWebViewPageState扩展状态{
inappwebview控制器webView;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(“InAppWebView”)
),
主体:容器(
子项:列(子项:[
扩大(
子:容器(
子:InAppWebView(
初始文件:“assets/index.html”,
initialHeaders:{},
initialOptions:InAppWebViewWidgetOptions(
inappwebview选项:inappwebview选项(
debuggingEnabled:true,
),
),
onWebViewCreated:(InAppWebViewController){
网络视图=控制器;
},
onLoadStart:(InAppWebViewController控制器,字符串url){
},
onLoadStop:(InAppWebViewController控制器,字符串url){
},
OnConsolleMessage:(InApp WebViewController控制器,ConsoleMessage ConsoleMessage){
打印(consoleMessage.message);
},
),
),
),
]))
);
}
}
index.html
中,您将看到如下内容:


我已经能够做到这一点,但它只在Android中工作。我以以下方式使用了InAppWebView库:

 child: Container(
              child: InAppWebView(
                initialFile: "images/test.html",
                initialHeaders: {},
                onWebViewCreated: (InAppWebViewController controller) {
                  webView = controller;
                },
                onLoadStart: (InAppWebViewController controller, String url) {},
                onLoadStop: (InAppWebViewController controller, String url) {},
                onConsoleMessage: (InAppWebViewController controller,
                    ConsoleMessage consoleMessage) {
                  print(consoleMessage.message);
                },
              ),
            ),

html文件(包括相关文件)已添加到项目资产中。

您的示例不起作用。我只花了2个小时在它和你的github repo上,但仍然无法让它工作。你知道如何从字符串加载HTML,但从资产获取图像资源吗?请参阅