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. Is there a way of hooking into the app to detect this behavior and to show such a warning message?
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. 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:
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;
}
}