[英]Is there a way to show a warning dialog when closing a browser tab running a flutter app?
您可以像这样为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;
}
}
你可以使用类似的东西
onTap() async {
await showDialog(
context: context,
builder: (_) {
return Container();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.