我正在尝试在Flutter Web中显示Web视图,但我遇到了以下错误:
PlatformException(Unregistered factory, No factory registered for viewtype 'plugins.flutter.io/webview', null)
有没有办法在Flutter Web中显示WebView?
有没有办法在Flutter Web中显示WebView?
编辑:以下是截至2021年5月16日的可运行示例:
import 'dart:html';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;
void main() {
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
'hello-world-html',
(int viewId) => IFrameElement()
..width = '640'
..height = '360'
..src = 'https://www.youtube.com/embed/IyFZznAk69U'
..style.border = 'none');
runApp(Directionality(
textDirection: TextDirection.ltr,
child: SizedBox(
width: 640,
height: 360,
child: HtmlElementView(viewType: 'hello-world-html'),
),
));
}
以下内容是本文作者的原始文章,与上述内容相同
首先需要执行platformViewRegistry:
ui.platformViewRegistry.registerViewFactory(
'hello-world-html',
(int viewId) => IFrameElement()
..width = '640'
..height = '360'
..src = 'https://www.youtube.com/embed/IyFZznAk69U'
..style.border = 'none');
ui.platformViewRegistry.registerViewFactory("simple_div", (int viewId) {
DivElement element = DivElement();
...
return element;
您可以使用 flutter_inappwebview
插件(我是它的作者)的版本 6.x.x
,它引入了Web和macOS平台支持!
当前最新版本可用的是 6.0.0-beta.16
。
它在底层使用 iframe
HTML元素,因此非常受功能限制,非常遗憾。
查看官方 Setup Web在线文档 开始使用。
loadRequest
loadHtmlString
@mohamed-salah 的回答很有帮助,但是我在屏幕上只看到一个加载符号。我们需要将 webview
放在 WillPopScope
小部件中。使用以下代码来正确加载 webview
-
在 pubspec.yaml
中添加依赖项
flutter_webview_plugin: ^0.3.9+1 // replace with latest version
StatefulWidget
类中,可以使用以下代码 -class _WebViewLoadingState extends State<Details> {
final _webViewPlugin = FlutterWebviewPlugin();
@override
void initState() {
// TODO: implement initState
super.initState();
// on pressing back button, exiting the screen instead of showing loading symbol
_webViewPlugin.onDestroy.listen((_) {
if (Navigator.canPop(context)) {
// exiting the screen
Navigator.of(context).pop();
}
});
}
@override
Widget build(BuildContext context) {
// WillPopScope will prevent loading
return WillPopScope(
child: WebviewScaffold(
url: "https://www.google.com",
withZoom: false,
withLocalStorage: true,
withJavascript: true,
appCacheEnabled: true,
appBar: AppBar(
title: Text("Browser"),
),
),
onWillPop: () {
return _webViewPlugin.close();
}
);
}
}
import 'dart:ui' as ui
来代替import 'package:flutter_web_ui/ui.dart' as ui
。你会收到关于ui.platformViewRegistry
的错误提示,但是没关系。你可以在上面一行添加// ignore: undefined_prefixed_name
来忽略它。 - Pyth0nGh057