Repository: flutter
Issue: [Android] When using setEnabledSystemUIMode on API 34 or later, there may be cases where MediaQuery cannot be obtained correctly.
Labels: platform-android,framework,engine,e: OS-version specific,a: layout,has reproducible steps,P2,team-android,triaged-android,found in release: 3.19,found in release: 3.20

### Steps to reproduce

1. Execute the following code
2. Display the Recents screen
3. Return to the app immediately without performing any other actions.
4. Push the 'SystemUIMode.manual'
5. Display the navigation bar

### Expected results

The value of `MediaQuery.of(context).padding.bottom` is correctly obtained, and the bottom blue container is displayed in the correct position.

### Actual results

The value of `MediaQuery.of(context).padding.bottom` is not retrieved, causing the bottom container to overlap with the navigation bar.

This issue does not occur on Android 13 (API 33). Additionally, it does not occur when the app is sent to the background and then brought back to the foreground.

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    SystemChrome.setEnabledSystemUIMode(
      SystemUiMode.edgeToEdge,
    );
  }

  @override
  Widget build(BuildContext context) {
    final tBottom = MediaQuery.of(context).padding.bottom;
    debugPrint('padding.bottom: $tBottom');
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Stack(
        children: [
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () {
                    setState(() {});
                  },
                  child: const Text('Rebuid'),
                ),
                const Padding(padding: EdgeInsets.all(8)),
                ElevatedButton(
                  onPressed: () {
                    SystemChrome.setEnabledSystemUIMode(
                      SystemUiMode.manual,
                      overlays: [SystemUiOverlay.top],
                    );
                  },
                  child: const Text('SystemUiMode.manual'),
                ),
                const Padding(padding: EdgeInsets.all(8)),
                ElevatedButton(
                  onPressed: () {
                    SystemChrome.setEnabledSystemUIMode(
                      SystemUiMode.edgeToEdge,
                    );
                  },
                  child: const Text('SystemUiMode.edgeToEdge'),
                ),
              ],
            ),
          ),
          Positioned(
            bottom: tBottom,
            left: 0,
            right: 0,
            child: Container(
              color: Colors.blue,
              height: 60,
            ),
          ),
        ],
      ),
    );
  }
}
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[setEnableSystemUIMode.webm](https://github.com/flutter/flutter/assets/69038535/15342b59-0eb1-42d3-bfe7-86065f2c49a7)

</details>


### Logs

<details open><summary>Logs</summary>

```console
I/flutter ( 4926): padding.bottom: 0.0
I/flutter ( 4926): padding.bottom: 48.0
I/flutter ( 4926): padding.bottom: 0.0
D/EGL_emulation( 4926): app_time_stats: avg=240.37ms min=0.42ms max=14807.15ms count=62
D/EGL_emulation( 4926): app_time_stats: avg=459.45ms min=1.17ms max=14689.76ms count=35
I/flutter ( 4926): padding.bottom: 48.0
D/EGL_emulation( 4926): app_time_stats: avg=4.47ms min=0.44ms max=68.50ms count=56
D/EGL_emulation( 4926): app_time_stats: avg=507.78ms min=19.62ms max=995.95ms count=2
D/EGL_emulation( 4926): app_time_stats: avg=26.67ms min=10.24ms max=422.55ms count=39
D/EGL_emulation( 4926): app_time_stats: avg=539.86ms min=167.14ms max=912.58ms count=2
D/EGL_emulation( 4926): app_time_stats: avg=461.35ms min=0.48ms max=2762.58ms count=6
I/flutter ( 4926): padding.bottom: 0.0
D/EGL_emulation( 4926): app_time_stats: avg=16.06ms min=11.52ms max=19.80ms count=61
D/EGL_emulation( 4926): app_time_stats: avg=46.42ms min=11.00ms max=1183.45ms count=39
D/EGL_emulation( 4926): app_time_stats: avg=18.79ms min=8.13ms max=167.01ms count=51
I/flutter ( 4926): padding.bottom: 0.0
D/EGL_emulation( 4926): app_time_stats: avg=112.90ms min=10.75ms max=884.15ms count=9
I/flutter ( 4926): padding.bottom: 0.0
D/EGL_emulation( 4926): app_time_stats: avg=17.23ms min=1.20ms max=79.96ms count=50
I/flutter ( 4926): padding.bottom: 48.0
D/EGL_emulation( 4926): app_time_stats: avg=22.29ms min=8.66ms max=283.87ms count=45
E/OpenGLRenderer( 4926): Unable to match the desired swap behavior.
I/flutter ( 4926): padding.bottom: 0.0
D/EGL_emulation( 4926): app_time_stats: avg=36.13ms min=1.13ms max=566.45ms count=23
D/EGL_emulation( 4926): app_time_stats: avg=17.46ms min=0.45ms max=736.14ms count=58
D/EGL_emulation( 4926): app_time_stats: avg=80.96ms min=2.08ms max=1015.78ms count=13
I/flutter ( 4926): padding.bottom: 48.0
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.19.0, on macOS 13.6.4 22G513 darwin-arm64, locale ja-JP)
    • Flutter version 3.19.0 on channel stable at /Users/me/development/flutter_3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision bae5e49bc2 (7 days ago), 2024-02-13 17:46:18 -0800
    • Engine revision 04817c99c9
    • Dart version 3.3.0
    • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/me/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.7+0-17.0.7b1000.6-10550314)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
    • Xcode at /Applications/Xcode_15_0.app/Contents/Developer
    • Build 15A240d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.1)
    • 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.7+0-17.0.7b1000.6-10550314)

[✓] VS Code (version 1.86.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.82.0

[✓] Connected device (3 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64  • Android 14 (API 34) (emulator)
    • macOS (desktop)             • macos         • darwin-arm64   • macOS 13.6.4 22G513 darwin-arm64
    • Chrome (web)                • chrome        • web-javascript • Google Chrome 121.0.6167.184

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>