Repository: flutter
Issue: ModalRoute Rebuilds Transition Widgets Every Frame
Labels: framework,a: animation,f: routes,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.22

### Steps to reproduce

1. Run the code sample
2. Tap the `pageBuilder` button
3. Tap the `transitionsBuilder` button

### Expected results

`pageBuilder` and `transitionsBuilder` should each execute once.

### Actual results

`pageBuilder` executes 1 time, while `transitionsBuilder` executes 38 times.

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  const HomeScreen({super.key});

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  var pageBuilderCount = 0;
  var transitionsBuilderCount = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Screen'),
      ),
      body: Center(
        child: Column(
          children: [
            const Spacer(flex: 3),
            Text('$pageBuilderCount builds'),
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Colors.blue),
                foregroundColor: const WidgetStatePropertyAll(Colors.white),
              ),
              onPressed: () {
                Navigator.push(
                  context,
                  PageRouteBuilder(
                    pageBuilder: (context, animation, secondaryAnimation) {
                      ++pageBuilderCount;
                      WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {}));
                      return FadeTransition(
                        opacity: animation,
                        child: Scaffold(
                          backgroundColor: Colors.blue,
                          appBar: AppBar(
                            title: const Text('pageBuilder'),
                          ),
                          body: const SizedBox(),
                        ),
                      );
                    },
                  ),
                );
              },
              child: const Text('pageBuilder'),
            ),
            const Spacer(),
            Text('$transitionsBuilderCount builds'),
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Colors.red),
                foregroundColor: const WidgetStatePropertyAll(Colors.white),
              ),
              onPressed: () {
                Navigator.push(
                  context,
                  PageRouteBuilder(
                    pageBuilder: (context, animation, secondaryAnimation) {
                      return Scaffold(
                        backgroundColor: Colors.red,
                        appBar: AppBar(
                          title: const Text('transitionsBuilder'),
                        ),
                        body: const SizedBox(),
                      );
                    },
                    transitionsBuilder: (context, animation, secondaryAnimation, child) {
                      ++transitionsBuilderCount;
                      WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {}));
                      return FadeTransition(
                        opacity: animation,
                        child: child,
                      );
                    },
                  ),
                );
              },
              child: const Text('transitionsBuilder'),
            ),
            const Spacer(flex: 3),
          ],
        ),
      ),
    );
  }
}
```

</details>


### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

https://github.com/flutter/flutter/assets/1063446/286a8d5d-80b9-4ec1-9986-0724557e15e8

</details>


### Logs

_No response_

### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.22.0, on macOS 14.5 23F79 darwin-arm64, locale en-US)
    • Flutter version 3.22.0 on channel stable at /opt/homebrew/Caskroom/flutter/3.22.0/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5dcb86f68f (13 days ago), 2024-05-09 07:39:20 -0500
    • Engine revision f6344b75dc
    • Dart version 3.4.0
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/user/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.10+0-17.0.10b1087.21-11572160)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.3)
    • 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.10+0-17.0.10b1087.21-11572160)

[✓] VS Code (version 1.89.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.88.0

[✓] Connected device (5 available)
    • sdk gphone64 arm64 (mobile)     • emulator-5554                        • android-arm64  • Android 14 (API 34) (emulator)
    • iPhone 15 Pro Max (mobile)      • 9CF6C1A2-E96A-4D3D-B878-1392E4F72A14 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-5 (simulator)
    • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 14.5 23F79 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         • macOS 14.5 23F79 darwin-arm64
    • Chrome (web)                    • chrome                               • web-javascript • Google Chrome 124.0.6367.210

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>