Flutter Web 中的 WebView
WebView in Flutter Web
我试图在 Flutter for Web 中显示 Web 视图,但出现以下错误:
PlatformException(Unregistered factory, No factory registered for viewtype 'plugins.flutter.io/webview', null)
有没有办法在 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'),
),
));
}
剩下的post只是复制上面的内容,是原作者post
首先需要执行platformViewRegistry:
ui.platformViewRegistry.registerViewFactory(
'hello-world-html',
(int viewId) => IFrameElement()
..width = '640'
..height = '360'
..src = 'https://www.youtube.com/embed/IyFZznAk69U'
..style.border = 'none');
看看那个 example。在那个例子中,旧库被导入(29.09.19),但是如果你在 'flutter' 上更改 'flutter_web' 它必须工作。
另外,你不仅可以使用'IFrameElement',还可以使用正则html:
ui.platformViewRegistry.registerViewFactory("simple_div", (int viewId) {
DivElement element = DivElement();
...
return element;
@mohamed-salah 的回答很有帮助,但是,我的屏幕上只有一个加载符号。我们需要将 webview
放入 WillPopScope
小部件中。使用以下代码正确加载 webview
-
在pubspec.yaml
添加依赖
flutter_webview_plugin: ^0.3.9+1 // replace with latest version
在StatefulWidget
class中,使用下面的代码-
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();
}
);
}
}
您可以尝试使用 easy_web_view 插件。
对于 iOS 和 Android,它使用本机 webview_flutter 插件,对于 Web,它从 @alex89607 的回答中做类似的事情。
我试图在 Flutter for Web 中显示 Web 视图,但出现以下错误:
PlatformException(Unregistered factory, No factory registered for viewtype 'plugins.flutter.io/webview', null)
有没有办法在 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'),
),
));
}
剩下的post只是复制上面的内容,是原作者post
首先需要执行platformViewRegistry:
ui.platformViewRegistry.registerViewFactory(
'hello-world-html',
(int viewId) => IFrameElement()
..width = '640'
..height = '360'
..src = 'https://www.youtube.com/embed/IyFZznAk69U'
..style.border = 'none');
看看那个 example。在那个例子中,旧库被导入(29.09.19),但是如果你在 'flutter' 上更改 'flutter_web' 它必须工作。
另外,你不仅可以使用'IFrameElement',还可以使用正则html:
ui.platformViewRegistry.registerViewFactory("simple_div", (int viewId) {
DivElement element = DivElement();
...
return element;
@mohamed-salah 的回答很有帮助,但是,我的屏幕上只有一个加载符号。我们需要将 webview
放入 WillPopScope
小部件中。使用以下代码正确加载 webview
-
在pubspec.yaml
添加依赖
flutter_webview_plugin: ^0.3.9+1 // replace with latest version
在StatefulWidget
class中,使用下面的代码-
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();
}
);
}
}
您可以尝试使用 easy_web_view 插件。
对于 iOS 和 Android,它使用本机 webview_flutter 插件,对于 Web,它从 @alex89607 的回答中做类似的事情。