简体   繁体   English

关闭运行 flutter 应用程序的浏览器选项卡时,有没有办法显示警告对话框?

[英]Is there a way to show a warning dialog when closing a browser tab running a flutter app?

I would like to indicate to the user that there's a file being downloaded when they try to close the tab in my flutter web app.我想向用户表明,当他们尝试关闭我的 flutter web 应用程序中的选项卡时,正在下载一个文件。 Is there a way of hooking into the app to detect this behavior and to show such a warning message?有没有办法连接到应用程序来检测这种行为并显示这样的警告信息?

警告弹出窗口

You may register code for a BeforeUnloadEvent like this:您可以像这样为BeforeUnloadEvent注册代码:

import 'dart:html' as html;

/// Subscription on application termination warning
StreamSubscription? _onBeforeUnloadSubscription;

//enable warning for closing browser tab:
_onBeforeUnloadSubscription = registerOnBeforeUnload("Transfers in progress!");

//disable warning for closing browser tab:
stopBeforeUnloadHandler();


StreamSubscription? registerOnBeforeUnload(String warningMessage) {
  StreamSubscription<html.BeforeUnloadEvent> _onBeforeUnloadSubscription;

  _onBeforeUnloadSubscription = html.window.onBeforeUnload.listen((e) async {
    (e as html.BeforeUnloadEvent).returnValue = warningMessage;
    return Future.value(warningMessage);
  }) as StreamSubscription<html.BeforeUnloadEvent>;
  return _onBeforeUnloadSubscription;
}

void stopBeforeUnloadHandler() {
  if (_onBeforeUnloadSubscription != null) {
    _onBeforeUnloadSubscription!.cancel();
    _onBeforeUnloadSubscription = null;
  }
}

you can use something like你可以使用类似的东西

onTap() async {

  await showDialog(
      context: context,
      builder: (_) {
        return  Container();
      });

}

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

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