如何使用Flatter dart webview呈现本地HTML文件
我想使用flatter和dart在webview中呈现存储在手机内存中的本地HTML文件。您可以传递数据URI如何使用Flatter dart webview呈现本地HTML文件,webview,dart,flutter,flutter-layout,Webview,Dart,Flutter,Flutter Layout,我想使用flatter和dart在webview中呈现存储在手机内存中的本地HTML文件。您可以传递数据URI Uri.dataFromString('<html><body>hello world</body></html>', mimeType: 'text/html').toString() Uri.dataFromString('hello world',mimeType:'text/html')。toString() 或者,您可以在f
Uri.dataFromString('<html><body>hello world</body></html>', mimeType: 'text/html').toString()
Uri.dataFromString('hello world',mimeType:'text/html')。toString()
或者,您可以在flatter中启动一个web服务器,并传递一个URL,该URL指向服务器为文件提供服务的IP/端口
另请参见中的讨论
请参见关于如何从资源加载字符串
请参阅以了解如何读取其他文件。我正在使用来自颤振团队的插件
台阶
assets
文件夹(请参阅)。我叫它help.html
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:webview_flutter/webview_flutter.dart';
class HelpScreen extends StatefulWidget {
@override
HelpScreenState createState() {
return HelpScreenState();
}
}
class HelpScreenState extends State<HelpScreen> {
WebViewController _controller;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Help')),
body: WebView(
initialUrl: 'about:blank',
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
_loadHtmlFromAssets();
},
),
);
}
_loadHtmlFromAssets() async {
String fileText = await rootBundle.loadString('assets/help.html');
_controller.loadUrl( Uri.dataFromString(
fileText,
mimeType: 'text/html',
encoding: Encoding.getByName('utf-8')
).toString());
}
}
导入'dart:convert';
进口“包装:颤振/材料.省道”;
导入“包:flifter/services.dart”;
导入“package:webview_flatter/webview_flatter.dart”;
类帮助屏幕扩展StatefulWidget{
@凌驾
HelpScreenState createState(){
返回HelpScreenState();
}
}
类HelpScreenState扩展了状态{
WebViewController\u控制器;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(标题:Text('Help')),
正文:WebView(
initialUrl:“关于:空白”,
onWebViewCreated:(WebViewController WebViewController){
_控制器=webViewController;
_加载HtmlFromAssets();
},
),
);
}
_loadHtmlFromAssets()异步{
String fileText=await rootBundle.loadString('assets/help.html');
_controller.loadUrl(Uri.dataFromString(
文件文本,
mimeType:'text/html',
编码:encoding.getByName('utf-8')
).toString());
}
}
- 我需要将编码设置为UTF-8,因为非ASCII字符会崩溃
- 在iOS中,您需要在Info.plist文件中添加键
作为io.flatter.embedded\u view\u preview
。检查是否有关于此要求的任何更新true
- @Suragch,您的代码在发布时不起作用,它说
localUrl
是在null
上调用的。
\u loadHtmlFromAssets
需要在分配控制器后调用:
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
_loadHtmlFromAssets();
}
然后它工作正常:)解压缩apk包,我发现原因:路径错误 对于Android:
“assets/test.html”=”file:///android_asset/flutter_assets/assets/test.html“
就这样,
WebView(
initialUrl: "file:///android_asset/flutter_assets/assets/test.html",
javascriptMode: JavascriptMode.unrestricted,
)
您可以加载“assets/test.html”。我也有同样的问题;我就是这样解决的
webview\u颤振:0.3.14+1
WebViewController\u控制器代码>
未来加载HtmlFromAssets(字符串文件名、控制器)异步{
String fileText=await rootBundle.loadString(文件名);
controller.loadUrl(Uri.dataFromString(fileText,mimeType:'text/html',编码:encoding.getByName('utf-8')).toString());
}
您可以使用我的插件,与其他插件相比,它有很多事件、方法和选项
要从资产文件夹加载html文件,需要在使用它之前在pubspec.yaml
文件中声明它(请参阅更多)
pubspec.yaml
文件示例:
...
# 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
...
之后,您可以简单地使用InAppWebView
小部件的initialFile
参数将index.html
加载到WebView中:
导入'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){
},
),
),
),
]))
);
}
}
您可以使用。它将在应用程序中创建一个本地服务器,并在WebView中运行HTML应用程序。启动服务器:
InAppLocalhostServer localhostServer = new InAppLocalhostServer();
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await localhostServer.start();
runApp(new MyApp());
}
//... ...
class _MyHomePageState extends State < MyHomePage > {
//... ...
@override
void dispose() {
localhostServer.close();
super.dispose();
}
}
在许多情况下,它不适用于许多人,因为他们忘记将所有文件夹作为资产添加到pubspec.yaml
文件中。例如,你需要
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());
}
...
# 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
...
InAppLocalhostServer localhostServer = new InAppLocalhostServer();
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await localhostServer.start();
runApp(new MyApp());
}
//... ...
class _MyHomePageState extends State < MyHomePage > {
//... ...
@override
void dispose() {
localhostServer.close();
super.dispose();
}
}
InAppWebView(
initialUrlRequest: URLRequest(
url: Uri.parse('http://localhost:8080/assets/index.html')),
),
assets:
- assets/index.html
- assets/css/
- assets/images/
- assets/js/
- assets/others/
Use flutter_widget_from_html_core---->
dependencies:
flutter_widget_from_html_core: ^0.5.1+4
Code like this
HtmlWidget(
"""
<html lang="en">
<body>hello world</body>
</html>
""",
),