简体   繁体   English

Flutter (Web) 如何监听 javascript 事件或将 javascript 数据传递给 flutter?

[英]Flutter (Web) how to listen to javascript event or pass javascript data to flutter?

I am currently writing a flutter web application and am creating a fullscreen button that makes the page fullscreen and edits the page so that a container is maximized upon clicking.我目前正在编写一个 flutter web 应用程序,并正在创建一个全屏按钮,使页面全屏并编辑页面,以便在单击时最大化容器。 This button works by tracking a boolean called "isfullscreen", which edits the ui in flutter and maximizing the browser window by calling a javascript function with dart:js. This button works by tracking a boolean called "isfullscreen", which edits the ui in flutter and maximizing the browser window by calling a javascript function with dart:js. This button works fine, and allows the user to exit out of fullscreen when clicking as well, and everything is reset to default.此按钮工作正常,并且允许用户在单击时退出全屏,并且所有内容都重置为默认值。 However, if the user clicks on the fullscreen button and then manually leaves fullscreen (f11 key, esc key on browser), flutter doesn't pick up the event and the contents of the webpage are mangled (isfullscreen=true but browser is not fullscreen).但是,如果用户单击全屏按钮然后手动离开全屏(f11 键,浏览器上的 esc 键),flutter 不会接收事件并且网页内容被破坏(isfullscreen=true 但浏览器不是全屏)。 Is it possible to a) listen to a javascript event within flutter, or b) listen to the javascript event and then somehow pass the data to the flutter web page so that the boolean isfullscreen=false when the user manually exits fullscreen without clicking the button? Is it possible to a) listen to a javascript event within flutter, or b) listen to the javascript event and then somehow pass the data to the flutter web page so that the boolean isfullscreen=false when the user manually exits fullscreen without clicking the button ? Thanks for helping.感谢您的帮助。

you can post a message in your js code when the event occures您可以在事件发生时在您的 js 代码中发布消息

window.parent.postMessage('any message', "*");

and listen to it in your dart code并在您的 dart 代码中收听

@override
void initState() {
  super.initState();

  //setup listener ---------------------------------
  window.addEventListener("message", (event) {
    MessageEvent event2 = event;
    print(event2.data);
  });
  //------------------------------------------------

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM