【发布时间】:2021-03-08 08:15:08
【问题描述】:
我想使用 Flutter 在 webview 中显示任何类型的文件。
webview 中加载文件的代码。
class ViewFile extends StatelessWidget {
WebViewController _controller;
String path;
ViewFile({this.path});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Container(
height: double.infinity,
width: double.infinity,
child: WebView(
initialUrl: 'about:blank',
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller = webViewController;
_loadHtmlFromAssets();
},
),
),
);
}
_loadHtmlFromAssets() async {
print('path is $path');
String fileText = '''<embed src="$path" width="100%" height="100%"/>''';
_controller.loadUrl(Uri.dataFromString(
fileText,
mimeType: 'text/html',
encoding: Encoding.getByName('utf-8')
).toString());
}
}
但它显示为空白。虽然嵌入标签其他标签工作完美。
【问题讨论】:
-
这个
path字符串是什么格式的,是URI还是bytes?有没有出现任何错误? -
这是一个临时文件路径,我必须在 webview 中显示
标签: html flutter dart webview embed