WebView 支持 FlutterWeb 插件开发
WebView support for FlutterWeb for plugin development
嗨,我开发了一个 Flutter 插件 flutter_tex。它基于 WebView。如何为此添加 Flutter Web 支持?
我试过这个例子来展示我的 HTML 内容。
import 'dart:ui' as ui;
void forWeb() {
if(kIsWeb){
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
'hello-world-html',
(int viewId) => uni_html.IFrameElement()
..width = '640'
..height = '360'
..src = 'https://www.youtube.com/embed/IyFZznAk69U'
..style.border = 'none');
Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: SizedBox(
width: 200,
height: 200,
child: HtmlElementView(viewType: 'hello-world-html'),
),
),
);
}
}
但是这段代码在为 web 构建时没有问题,但是在 android 上编译时,即使我没有调用上面的代码,我也会收到这个错误。
Compiler message:
../lib/flutter_tex.dart:139:10: Error: Getter not found: 'platformViewRegistry'.
ui.platformViewRegistry.registerViewFactory(
^^^^^^^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.
FAILURE: Build failed with an exception.
您可以复制粘贴 运行 3 个文件到 main.dart
、mobileui.dart
和 webui.dart
下面
您可以将 mobile
和 web
代码放在不同的文件中并使用条件导入
这允许您在移动设备和网络上使用不同的工具
import 'mobileui.dart' if (dart.library.html) 'webui.dart' as multiPlatform;
...
home: multiPlatform.TestPlugin(),
工作演示
当 运行 与 Chrome
或 Android Emulator
在 Android Studio
main.dart
import 'package:flutter/material.dart';
import 'mobileui.dart' if (dart.library.html) 'webui.dart' as multiPlatform;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: multiPlatform.TestPlugin(),
);
}
}
mobileui.dart
import 'package:flutter/material.dart';
class TestPlugin extends StatefulWidget {
@override
_TestPluginState createState() => _TestPluginState();
}
class _TestPluginState extends State<TestPlugin> {
@override
Widget build(BuildContext context) {
return Text("Mobile");
}
}
webui.dart
import 'package:flutter/material.dart';
import 'dart:html' as html;
import 'dart:js' as js;
import 'dart:ui' as ui;
class TestPlugin extends StatefulWidget {
TestPlugin();
_TestPluginState createState() => _TestPluginState();
}
class _TestPluginState extends State<TestPlugin> {
String createdViewId = 'map_element';
@override
void initState() {
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
createdViewId,
(int viewId) => html.IFrameElement()
..width = MediaQuery.of(context).size.width.toString() //'800'
..height = MediaQuery.of(context).size.height.toString() //'400'
..srcdoc = """<!DOCTYPE html><html>
<head><title>Page Title</title></head><body><h1>This is a Heading</h1><p>This is a paragraph.</p></body></html>"""
..style.border = 'none');
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey[300], width: 1),
borderRadius: BorderRadius.all(Radius.circular(5))),
width: 200,
height: 200,
child: Directionality(
textDirection: TextDirection.ltr,
child: HtmlElementView(
viewType: createdViewId,
)));
}
}
嗨,我开发了一个 Flutter 插件 flutter_tex。它基于 WebView。如何为此添加 Flutter Web 支持?
我试过这个例子来展示我的 HTML 内容。
import 'dart:ui' as ui;
void forWeb() {
if(kIsWeb){
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
'hello-world-html',
(int viewId) => uni_html.IFrameElement()
..width = '640'
..height = '360'
..src = 'https://www.youtube.com/embed/IyFZznAk69U'
..style.border = 'none');
Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: SizedBox(
width: 200,
height: 200,
child: HtmlElementView(viewType: 'hello-world-html'),
),
),
);
}
}
但是这段代码在为 web 构建时没有问题,但是在 android 上编译时,即使我没有调用上面的代码,我也会收到这个错误。
Compiler message:
../lib/flutter_tex.dart:139:10: Error: Getter not found: 'platformViewRegistry'.
ui.platformViewRegistry.registerViewFactory(
^^^^^^^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.
FAILURE: Build failed with an exception.
您可以复制粘贴 运行 3 个文件到 main.dart
、mobileui.dart
和 webui.dart
下面
您可以将 mobile
和 web
代码放在不同的文件中并使用条件导入
这允许您在移动设备和网络上使用不同的工具
import 'mobileui.dart' if (dart.library.html) 'webui.dart' as multiPlatform;
...
home: multiPlatform.TestPlugin(),
工作演示
当 运行 与 Chrome
或 Android Emulator
在 Android Studio
main.dart
import 'package:flutter/material.dart';
import 'mobileui.dart' if (dart.library.html) 'webui.dart' as multiPlatform;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: multiPlatform.TestPlugin(),
);
}
}
mobileui.dart
import 'package:flutter/material.dart';
class TestPlugin extends StatefulWidget {
@override
_TestPluginState createState() => _TestPluginState();
}
class _TestPluginState extends State<TestPlugin> {
@override
Widget build(BuildContext context) {
return Text("Mobile");
}
}
webui.dart
import 'package:flutter/material.dart';
import 'dart:html' as html;
import 'dart:js' as js;
import 'dart:ui' as ui;
class TestPlugin extends StatefulWidget {
TestPlugin();
_TestPluginState createState() => _TestPluginState();
}
class _TestPluginState extends State<TestPlugin> {
String createdViewId = 'map_element';
@override
void initState() {
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
createdViewId,
(int viewId) => html.IFrameElement()
..width = MediaQuery.of(context).size.width.toString() //'800'
..height = MediaQuery.of(context).size.height.toString() //'400'
..srcdoc = """<!DOCTYPE html><html>
<head><title>Page Title</title></head><body><h1>This is a Heading</h1><p>This is a paragraph.</p></body></html>"""
..style.border = 'none');
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(color: Colors.grey[300], width: 1),
borderRadius: BorderRadius.all(Radius.circular(5))),
width: 200,
height: 200,
child: Directionality(
textDirection: TextDirection.ltr,
child: HtmlElementView(
viewType: createdViewId,
)));
}
}