Flutter Web 监听通过 iFrame 发布的 Events
Flutter Web listen to Events posted through iFrame
我的目标是 在我的主页上借助 iframe 集成一个 flutter 小部件。主页不是用flutter写的。但是,我需要一个界面,主页可以通过该界面与小部件通信。于是想到了在主页上使用postMessage(),在flutter widget中使用html.window.addEventListener()。现在我在 flutter 中收到一个事件,但看不到它的内容。我的临时解决方案是将消息保存在本地存储中,只使用监听器作为通知器,但我不太喜欢这种方法。有没有人能更好地解决我的问题?
Flutter 测试小部件:
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;
import 'package:simple_cookies/simple_cookies.dart';
class MessageListener extends StatefulWidget {
@override
_MessageListenerState createState() => _MessageListenerState();
}
class _MessageListenerState extends State<MessageListener> {
String last;
@override
void initState() {
html.window.addEventListener('message', listen, true);
super.initState();
}
@override
void dispose() {
html.window.removeEventListener('message', listen, true);
super.dispose();
}
void listen(html.Event event) {
last = Cookies.get('iframe_message') ?? 'Error';
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('iFrame Test'),
),
body: Center(
child: SingleChildScrollView(child: Text(last ?? 'No messages yet')),
),
);
}
}
主页:
<html>
<head>
<title>website title</title>
<script type="text/javascript">
function pageProcessing() {
let messageButton = window.document.getElementById("post_message");
messageButton.addEventListener("click", () => {
let message = window.document.getElementById("message_text").value;
window.localStorage.setItem("iframe_message", message);
document.getElementById('flutter_widget').contentWindow.postMessage(
{
sender: "main_page",
message: message
},
"*"
);
});
}
window.document.addEventListener('readystatechange', () => {
if (window.document.readyState == 'complete') {
pageProcessing();
}
}
);
</script>
</head>
<body>
<div>
<input type="text" id="message_text"> <input type="button" id="post_message" value="Send Message">
</div>
<iframe id="flutter_widget" src="http://testing.orcaf#####"></iframe>
</body>
</html>
要从侦听器读取消息数据,您必须将事件转换为 MessageEvent
:
void listen(html.Event event) {
var data = (event as html.MessageEvent).data;
print(data['sender']);
print(data['message']);
setState(() {
//...
});
}
我的目标是 在我的主页上借助 iframe 集成一个 flutter 小部件。主页不是用flutter写的。但是,我需要一个界面,主页可以通过该界面与小部件通信。于是想到了在主页上使用postMessage(),在flutter widget中使用html.window.addEventListener()。现在我在 flutter 中收到一个事件,但看不到它的内容。我的临时解决方案是将消息保存在本地存储中,只使用监听器作为通知器,但我不太喜欢这种方法。有没有人能更好地解决我的问题?
Flutter 测试小部件:
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;
import 'package:simple_cookies/simple_cookies.dart';
class MessageListener extends StatefulWidget {
@override
_MessageListenerState createState() => _MessageListenerState();
}
class _MessageListenerState extends State<MessageListener> {
String last;
@override
void initState() {
html.window.addEventListener('message', listen, true);
super.initState();
}
@override
void dispose() {
html.window.removeEventListener('message', listen, true);
super.dispose();
}
void listen(html.Event event) {
last = Cookies.get('iframe_message') ?? 'Error';
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('iFrame Test'),
),
body: Center(
child: SingleChildScrollView(child: Text(last ?? 'No messages yet')),
),
);
}
}
主页:
<html>
<head>
<title>website title</title>
<script type="text/javascript">
function pageProcessing() {
let messageButton = window.document.getElementById("post_message");
messageButton.addEventListener("click", () => {
let message = window.document.getElementById("message_text").value;
window.localStorage.setItem("iframe_message", message);
document.getElementById('flutter_widget').contentWindow.postMessage(
{
sender: "main_page",
message: message
},
"*"
);
});
}
window.document.addEventListener('readystatechange', () => {
if (window.document.readyState == 'complete') {
pageProcessing();
}
}
);
</script>
</head>
<body>
<div>
<input type="text" id="message_text"> <input type="button" id="post_message" value="Send Message">
</div>
<iframe id="flutter_widget" src="http://testing.orcaf#####"></iframe>
</body>
</html>
要从侦听器读取消息数据,您必须将事件转换为 MessageEvent
:
void listen(html.Event event) {
var data = (event as html.MessageEvent).data;
print(data['sender']);
print(data['message']);
setState(() {
//...
});
}