嗨,我开发了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'),
),
),
);
}
}
但是,此代码在为网络构建时很好,但是在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
编码,并使用条件导入。
这使您可以在移动设备和Web上使用不同的工具
import 'mobileui.dart' if (dart.library.html) 'webui.dart' as multiPlatform;
...
home: multiPlatform.TestPlugin(),
与Chrome
或Android Emulator
一起运行时的工作演示Android Studio
主镖
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,
)));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句