Flutter Web 中的 WebView [英] WebView in Flutter Web
问题描述
我正在尝试在 Flutter for Web 中显示 Web 视图,但出现以下错误:
I am trying to display a web view in Flutter for Web but I got the following error :
PlatformException(Unregistered factory, No factory registered for viewtype 'plugins.flutter.io/webview', null)
有没有办法在 Flutter Web 中显示 WebView?
Is there a way to display a WebView in Flutter Web ?
推荐答案
这是截至 2021 年 5 月 16 日的可运行示例:
Here is a runnable example as of May 16, 2021:
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:
You need at first perform 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',它必须工作.
Look at that example. In that example old library was imported (on 29.09.19), but if you change 'flutter_web' on 'flutter' it have to work.
此外,您不仅可以使用IFrameElement",还可以使用常规的 html:
Also, you can use not only 'IFrameElement', it can be regular html:
ui.platformViewRegistry.registerViewFactory("simple_div", (int viewId) {
DivElement element = DivElement();
...
return element;
这篇关于Flutter Web 中的 WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!