Repository: flutter
Issue: Flutter becomes dark theme for one frame whenever screen is turned off, even though the phone is light theme, because `onPlatformBrightnessChanged` reports dark for one frame
Labels: platform-ios,framework,engine,has reproducible steps,P2,found in release: 3.7,found in release: 3.9,team-ios,triaged-ios

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

Hi thanks for the wonderful framework! However I am facing the bug mentioned in the title in production environment. The effect is that, the widget tree needs to be rebuilt, and thus many states are lost, resulting in bad user experience.

The reproduction simply prints out two things: `window.onPlatformBrightnessChanged` and `Theme.of(context).brightness`. The summarization is as follows:

1. ACTION: app starts
2. theme.brightness=light (correct)
3. ACTION: turn off screen
4. onPlatformBrightnessChanged reports dark (WRONG)
5. onPlatformBrightnessChanged reports light (in the next frame)
6. ACTION: turn on screen and unlock it
7. theme.brightness=dark (WRONG, seems to be caused by wrong onPlatformBrightnessChanged)
8. theme.brightness=light (correct)

## Steps to Reproduce

1. Execute `flutter run` on the code sample, I use ios simulator on macos
9. Turn off screen
10. turn on screen again (and unlock screen)
11. look at logs

**Expected results:** <!-- what did you want to see? -->
theme is always `light`

**Actual results:** <!-- what did you see? -->
theme becomes `dark` for one frame

<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

void main() {
  debugPrintBeginFrameBanner = debugPrintEndFrameBanner = true;
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();

    final window = WidgetsBinding.instance.window;
    final oldCallback = window.onPlatformBrightnessChanged;
    window.onPlatformBrightnessChanged = () {
      oldCallback?.call();
      print('hi window.onPlatformBrightnessChanged window.platformBrightness=${window.platformBrightness}');
    };
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.light(),
      darkTheme: ThemeData.dark(),
      themeMode: ThemeMode.system,
      home: Scaffold(
        appBar: AppBar(),
        body: Builder(builder: (context) {
          print('theme.brightness=${Theme.of(context).brightness}');
          return Text('${Theme.of(context).brightness}');
        }),
      ),
    );
  }
}
```

</details>

<details>
  <summary>Logs</summary>

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue. If there is a crash, please include
      the entirety of the crash log including the backtrace.
-->

```
Performing hot reload...
Syncing files to device iPhone 12 Pro Max...
flutter: ▄▄▄▄▄▄▄▄ Frame 23           (warm-up frame) ▄▄▄▄▄▄▄▄
flutter: theme.brightness=Brightness.light
flutter: ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
Reloaded 1 of 640 libraries in 181ms (compile: 32 ms, reload: 76 ms, reassemble: 63 ms).
flutter: ▄▄▄▄▄▄▄▄ Frame 24              5s 550.002ms ▄▄▄▄▄▄▄▄
flutter: ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
flutter: ▄▄▄▄▄▄▄▄ Frame 25             30s 820.373ms ▄▄▄▄▄▄▄▄
flutter: ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
flutter: hi window.onPlatformBrightnessChanged window.platformBrightness=Brightness.dark
flutter: ▄▄▄▄▄▄▄▄ Frame 26             30s 950.002ms ▄▄▄▄▄▄▄▄
flutter: ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
flutter: hi window.onPlatformBrightnessChanged window.platformBrightness=Brightness.light
flutter: ▄▄▄▄▄▄▄▄ Frame 27             31s 150.002ms ▄▄▄▄▄▄▄▄
flutter: theme.brightness=Brightness.dark
flutter: ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
flutter: ▄▄▄▄▄▄▄▄ Frame 28              32s 33.335ms ▄▄▄▄▄▄▄▄
flutter: theme.brightness=Brightness.light
flutter: ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
flutter: ▄▄▄▄▄▄▄▄ Frame 29              32s 50.002ms ▄▄▄▄▄▄▄▄
flutter: ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
flutter: ▄▄▄▄▄▄▄▄ Frame 30              32s 66.669ms ▄▄▄▄▄▄▄▄
flutter: ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
```

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
n/a
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
latest
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is limited to 10MB.
-->