Repository: flutter
Issue: WidgetsBindingObserver didRequestAppExit cancel behaviour incorrect on macOS
Labels: framework,platform-mac,a: desktop,has reproducible steps,P2,found in release: 3.16,found in release: 3.19,team-macos,triaged-macos

### Steps to reproduce

Create a new flutter project with the following `main.dart`

```dart
import 'dart:ui';

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return CancelApplicationClose(
      child: MaterialApp(
        theme: ThemeData.dark(),
        home: const Scaffold(
          body: Center(
            child: Text("Application"),
          ),
        ),
      ),
    );
  }
}

class CancelApplicationClose extends StatefulWidget {
  const CancelApplicationClose({super.key, required this.child});

  final Widget child;

  @override
  State<CancelApplicationClose> createState() => _CancelApplicationCloseState();
}

class _CancelApplicationCloseState extends State<CancelApplicationClose> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  Future<AppExitResponse> didRequestAppExit() async {
    _p("didRequestAppExit - enter");
    await Future.delayed(const Duration(seconds: 1));
    _p("didRequestAppExit - waited");

    // return AppExitResponse.exit;
    return AppExitResponse.cancel;
  }

  void _p(String msg) => print("${DateTime.now()} $msg");

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        _p("AppLifecycleState.resumed");
      case AppLifecycleState.paused:
        _p("AppLifecycleState.paused");
      case AppLifecycleState.detached:
        _p("AppLifecycleState.detached");
      case AppLifecycleState.inactive:
        _p("AppLifecycleState.inactive");
      case AppLifecycleState.hidden:
        _p("AppLifecycleState.hidden");
    }
  }

  @override
  Widget build(BuildContext context) {
    return widget.child;
  }
}
```

Now run the flutter application on macos.

Close the application (x) and observe how the application window is closed.

### Expected results

The application window stays open until we return a future from `didRequestAppExit`.

If we return `AppExitResponse.cancel`, the application window stays open.

If we return `AppExitResponse.exit`, the application window closes.


### Actual results

The application window is closed before returning from `didRequestAppExit`.

If we return `AppExitResponse.cancel`, the application window is closed, while the application continues to run, with no way to open the window again.
(macos shows the "hide" option instead of "show", and no open windows for the application)
<img width="349" alt="Screenshot 2024-01-11 at 15 48 55" src="https://github.com/flutter/flutter/assets/24209580/8150016b-f94d-4d71-bb68-1ea61ac02438">

```
flutter: 2024-01-11 15:43:44.622751 didRequestAppExit - enter
flutter: 2024-01-11 15:43:44.629863 AppLifecycleState.inactive
flutter: 2024-01-11 15:43:44.629987 AppLifecycleState.hidden
flutter: 2024-01-11 15:43:45.626148 didRequestAppExit - waited
```


If we return `AppExitResponse.exit`, the application window closes and the application terminates.
```
flutter: 2024-01-11 15:50:20.807475 didRequestAppExit - enter
flutter: 2024-01-11 15:50:20.812208 AppLifecycleState.inactive
flutter: 2024-01-11 15:50:20.812483 AppLifecycleState.hidden
flutter: 2024-01-11 15:50:21.815436 didRequestAppExit - waited
Lost connection to device.

Exited.
```

### Code sample

See above

### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

If we return `AppExitResponse.cancel`, the application window is closed, while the application continues to run, with no way to open the window again.


https://github.com/flutter/flutter/assets/24209580/26afb516-0e83-4ea3-9712-f9d511e10f36





If we return `AppExitResponse.exit`, the application window closes and the application terminates.

https://github.com/flutter/flutter/assets/24209580/33e7b3ab-ed1a-4ebf-abe4-d9749a0076cb


</details>


### Logs

_No response_

### Flutter Doctor output

<details open><summary>Doctor output</summary>

Tested on stable and master

```console
$ flutter doctor -v
[✓] Flutter (Channel master, 3.16.0-18.0.pre.63, on macOS 14.1 23B74 darwin-arm64, locale en-DE)
    • Flutter version 3.16.0-18.0.pre.63 on channel master at /Users/gian/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision eba149fcc3 (3 months ago), 2023-10-27 12:04:07 +0300
    • Engine revision bedc49efc8
    • Dart version 3.3.0 (build 3.3.0-56.0.dev)
    • DevTools version 2.28.1

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/gian/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses

[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15C65
    • CocoaPods version 1.14.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.85.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.80.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 14.1 23B74 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 120.0.6099.216

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.
```

</details>


____

Also note that e.g. the https://github.com/zonble/flutter_window_close package works on macos, which uses NSWindowDelegate