setWindowShouldCloseHandler static method

Future<void> setWindowShouldCloseHandler(
  1. Future<bool> handler()?
)

Sets a function to handle window close events.

When a user click on the close button on a window, the plug-in redirects the event to your function. The function should return a future that returns a boolean to tell the plug-in whether the user really wants to close the window or not. True will let the window to be closed, while false let the window to remain open.

By default there is no handler, and the window will be directly closed when a window close event happens. You can also reset the handler by passing null to the method.

Example:

FlutterWindowClose.setWindowShouldCloseHandler(() async {
    return await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
          title: const Text('Do you really want to quit?'),
          actions: [
            ElevatedButton(
            onPressed: () => Navigator.of(context).pop(true),
            child: const Text('Yes')),
            ElevatedButton(
            onPressed: () => Navigator.of(context).pop(false),
            child: const Text('No')),
          ]);
        });
});

The method does not support Flutter Web.

Implementation

static Future<void> setWindowShouldCloseHandler(
    Future<bool> Function()? handler) async {
  if (kIsWeb) throw Exception('The method does not work in Flutter Web.');
  _onWindowShouldClose = handler;
  await _initIfRequired();
}