Skip to content

[Docs Example] NavigationBar with massive rebuilds.  #136125

Closed

Description

@felipecastrosales

Is there an existing issue for this?

Steps to reproduce

Analyzing the flutter create --sample=material.NavigationBar.3 mysample, the third example in NavigationBar from documentation it is noticeable that the assembled structure causes rebuilds.

I really wanted to use this model in a POC I'm working on, because I really enjoyed the possibilities that the structure can allow, but this is being annoying, as it makes debugging quite difficult, as it crashes infinitely many times (even in children of this route , all componetized and already constant).

For reproduce in a simple way, add a print to the HomePage build and its children; you can put it in other places too, e.g:

In this print below, I make the run, and click on the second item from nav bar, and in my console is the result returned:

image

Expected results

Is expected that there will not be these rebuilds.

Actual results

The rebuild appears and maybe is very bad for app performance.

Code sample

Code sample
import 'package:flutter/material.dart';

/// Flutter code sample for [NavigationBar] with nested [Navigator] destinations.

void main() {
  runApp(const MaterialApp(home: Home()));
}

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> with TickerProviderStateMixin<Home> {
  static const List<Destination> allDestinations = <Destination>[
    Destination(0, 'Teal', Icons.home, Colors.teal),
    Destination(1, 'Cyan', Icons.business, Colors.cyan),
    Destination(2, 'Orange', Icons.school, Colors.orange),
    Destination(3, 'Blue', Icons.flight, Colors.blue),
  ];

  late final List<GlobalKey<NavigatorState>> navigatorKeys;
  late final List<GlobalKey> destinationKeys;
  late final List<AnimationController> destinationFaders;
  late final List<Widget> destinationViews;
  int selectedIndex = 0;

  AnimationController buildFaderController() {
    final AnimationController controller = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 200));
    controller.addStatusListener((AnimationStatus status) {
      if (status == AnimationStatus.dismissed) {
        setState(() {}); // Rebuild unselected destinations offstage.
      }
    });
    return controller;
  }

  @override
  void initState() {
    super.initState();
    navigatorKeys = List<GlobalKey<NavigatorState>>.generate(
        allDestinations.length, (int index) => GlobalKey()).toList();
    destinationFaders = List<AnimationController>.generate(
        allDestinations.length, (int index) => buildFaderController()).toList();
    destinationFaders[selectedIndex].value = 1.0;
    destinationViews = allDestinations.map((Destination destination) {
      return FadeTransition(
        opacity: destinationFaders[destination.index]
            .drive(CurveTween(curve: Curves.fastOutSlowIn)),
        child: DestinationView(
          destination: destination,
          navigatorKey: navigatorKeys[destination.index],
        ),
      );
    }).toList();
  }

  @override
  void dispose() {
    for (final AnimationController controller in destinationFaders) {
      controller.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('BUILD HOME');
    return WillPopScope(
      onWillPop: () async {
        final NavigatorState navigator =
            navigatorKeys[selectedIndex].currentState!;
        if (!navigator.canPop()) {
          return true;
        }
        navigator.pop();
        return false;
      },
      child: Scaffold(
        body: SafeArea(
          top: false,
          child: Stack(
            fit: StackFit.expand,
            children: allDestinations.map((Destination destination) {
              print('BUILD CHILDREN');
              final int index = destination.index;
              final Widget view = destinationViews[index];
              if (index == selectedIndex) {
                destinationFaders[index].forward();
                return Offstage(offstage: false, child: view);
              } else {
                destinationFaders[index].reverse();
                if (destinationFaders[index].isAnimating) {
                  return IgnorePointer(child: view);
                }
                return Offstage(child: view);
              }
            }).toList(),
          ),
        ),
        bottomNavigationBar: NavigationBar(
          selectedIndex: selectedIndex,
          onDestinationSelected: (int index) {
            setState(() {
              selectedIndex = index;
            });
          },
          destinations: allDestinations.map((Destination destination) {
            return NavigationDestination(
              icon: Icon(destination.icon, color: destination.color),
              label: destination.title,
            );
          }).toList(),
        ),
      ),
    );
  }
}

class Destination {
  const Destination(this.index, this.title, this.icon, this.color);
  final int index;
  final String title;
  final IconData icon;
  final MaterialColor color;
}

class RootPage extends StatelessWidget {
  const RootPage({super.key, required this.destination});

  final Destination destination;

  Widget _buildDialog(BuildContext context) {
    return AlertDialog(
      title: Text('${destination.title} AlertDialog'),
      actions: <Widget>[
        TextButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: const Text('OK'),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    final TextStyle headlineSmall = Theme.of(context).textTheme.headlineSmall!;
    final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
      backgroundColor: destination.color,
      visualDensity: VisualDensity.comfortable,
      padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
      textStyle: headlineSmall,
    );

    return Scaffold(
      appBar: AppBar(
        title: Text('${destination.title} RootPage - /'),
        backgroundColor: destination.color,
      ),
      backgroundColor: destination.color[50],
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            ElevatedButton(
              style: buttonStyle,
              onPressed: () {
                Navigator.pushNamed(context, '/list');
              },
              child: const Text('Push /list'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              style: buttonStyle,
              onPressed: () {
                showDialog(
                  context: context,
                  useRootNavigator: false,
                  builder: _buildDialog,
                );
              },
              child: const Text('Local Dialog'),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              style: buttonStyle,
              onPressed: () {
                showDialog(
                  context: context,
                  useRootNavigator: true,
                  builder: _buildDialog,
                );
              },
              child: const Text('Root Dialog'),
            ),
            const SizedBox(height: 16),
            Builder(
              builder: (BuildContext context) {
                return ElevatedButton(
                  style: buttonStyle,
                  onPressed: () {
                    showBottomSheet(
                      context: context,
                      builder: (BuildContext context) {
                        return Container(
                          padding: const EdgeInsets.all(16),
                          width: double.infinity,
                          child: Text(
                            '${destination.title} BottomSheet\n'
                            'Tap the back button to dismiss',
                            style: headlineSmall,
                            softWrap: true,
                            textAlign: TextAlign.center,
                          ),
                        );
                      },
                    );
                  },
                  child: const Text('Local BottomSheet'),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}

class ListPage extends StatelessWidget {
  const ListPage({super.key, required this.destination});

  final Destination destination;

  @override
  Widget build(BuildContext context) {
    const int itemCount = 50;
    final ButtonStyle buttonStyle = OutlinedButton.styleFrom(
      foregroundColor: destination.color,
      fixedSize: const Size.fromHeight(128),
      textStyle: Theme.of(context).textTheme.headlineSmall,
    );
    return Scaffold(
      appBar: AppBar(
        title: Text('${destination.title} ListPage - /list'),
        backgroundColor: destination.color,
      ),
      backgroundColor: destination.color[50],
      body: SizedBox.expand(
        child: ListView.builder(
          itemCount: itemCount,
          itemBuilder: (BuildContext context, int index) {
            return Padding(
              padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
              child: OutlinedButton(
                style: buttonStyle.copyWith(
                  backgroundColor: MaterialStatePropertyAll<Color>(
                    Color.lerp(destination.color[100], Colors.white,
                        index / itemCount)!,
                  ),
                ),
                onPressed: () {
                  Navigator.pushNamed(context, '/text');
                },
                child: Text('Push /text [$index]'),
              ),
            );
          },
        ),
      ),
    );
  }
}

class TextPage extends StatefulWidget {
  const TextPage({super.key, required this.destination});

  final Destination destination;

  @override
  State<TextPage> createState() => _TextPageState();
}

class _TextPageState extends State<TextPage> {
  late final TextEditingController textController;

  @override
  void initState() {
    super.initState();
    textController = TextEditingController(text: 'Sample Text');
  }

  @override
  void dispose() {
    textController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final ThemeData theme = Theme.of(context);
    return Scaffold(
      appBar: AppBar(
        title: Text('${widget.destination.title} TextPage - /list/text'),
        backgroundColor: widget.destination.color,
      ),
      backgroundColor: widget.destination.color[50],
      body: Container(
        padding: const EdgeInsets.all(32.0),
        alignment: Alignment.center,
        child: TextField(
          controller: textController,
          style: theme.primaryTextTheme.headlineMedium?.copyWith(
            color: widget.destination.color,
          ),
          decoration: InputDecoration(
            focusedBorder: UnderlineInputBorder(
              borderSide: BorderSide(
                color: widget.destination.color,
                width: 3.0,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class DestinationView extends StatefulWidget {
  const DestinationView({
    super.key,
    required this.destination,
    required this.navigatorKey,
  });

  final Destination destination;
  final Key navigatorKey;

  @override
  State<DestinationView> createState() => _DestinationViewState();
}

class _DestinationViewState extends State<DestinationView> {
  @override
  Widget build(BuildContext context) {
    return Navigator(
      key: widget.navigatorKey,
      onGenerateRoute: (RouteSettings settings) {
        return MaterialPageRoute<void>(
          settings: settings,
          builder: (BuildContext context) {
            switch (settings.name) {
              case '/':
                return RootPage(destination: widget.destination);
              case '/list':
                return ListPage(destination: widget.destination);
              case '/text':
                return TextPage(destination: widget.destination);
            }
            assert(false);
            return const SizedBox();
          },
        );
      },
    );
  }
}

Screenshots or Video

No response

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.13.6, on macOS 14.1 23B5056e darwin-arm64, locale en-BR)
    • Flutter version 3.13.6 on channel stable at /Users/felipecastrosales/fvm/versions/3.13.6
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ead455963c (11 days ago), 2023-09-26 18:28:17 -0700
    • Engine revision a794cf2681
    • Dart version 3.1.3
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /Users/felipecastrosales/Library/Android/sdk
    • Platform android-34, build-tools 33.0.1
    • ANDROID_HOME = /Users/felipecastrosales/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/felipecastrosales/Library/Android/sdk
    • 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)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.13.0

[✓] 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.74.3)
    • VS Code at /Users/felipecastrosales/Downloads/app/Visual Studio Code.app/Contents
    • Flutter extension version 3.60.0

[✓] Connected device (3 available)
    • M2003J15SC (mobile) • 192.168.1.20:5555 • android-arm64  • Android 11 (API 30)
    • macOS (desktop)     • macos             • darwin-arm64   • macOS 14.1 23B5056e darwin-arm64
    • Chrome (web)        • chrome            • web-javascript • Google Chrome 117.0.5938.149
    ! Error: Could not locate device support files. You may be able to resolve the issue by installing the latest version of Xcode from the Mac App Store or developer.apple.com.

      [missing string: 869a8e318f07f3e2f42e11d435502286094f76de] (code 2)

[✓] Network resources
    • All expected network resources are available.

Activity

  1. added
    in triagePresently being triaged by the triage team
    on Oct 9, 2023
  2. dam-ease commented on Oct 9, 2023

    @dam-ease

    Hi @felipecastrosales. Thanks for filing this. I can reproduce this on the latest master and stable channels following the steps highlighted.

    stable, master flutter doctor -v

    [✓] Flutter (Channel stable, 3.13.6, on macOS 13.0 22A380 darwin-arm64, locale
        en-NG)
        • Flutter version 3.13.6 on channel stable at
          /Users/damilolaalimi/sdks/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision ead455963c (12 days ago), 2023-09-26 18:28:17 -0700
        • Engine revision a794cf2681
        • Dart version 3.1.3
        • DevTools version 2.25.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
        • Android SDK at /Users/damilolaalimi/Library/Android/sdk
        • Platform android-34, build-tools 34.0.0
        • ANDROID_HOME = /Users/damilolaalimi/Library/Android/sdk
        • 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)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14E300c
        • CocoaPods version 1.12.1
    
    [✓] 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)
    
    [!] Android Studio (version unknown)
        • Android Studio at /Users/damilolaalimi/Downloads/Android Studio
          Preview.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
        ✗ Unable to determine Android Studio version.
        • Java version OpenJDK Runtime Environment (build
          17.0.7+0-17.0.7b1000.6-10550314)
    
    [✓] VS Code (version 1.82.3)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.50.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.0
          22A380 darwin-arm64
        • Chrome (web)                • chrome        • web-javascript • Google
          Chrome 117.0.5938.149
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
    [✓] Flutter (Channel master, 3.16.0-6.0.pre.41, on macOS 13.0 22A380 darwin-arm64, locale en-NG)
        • Flutter version 3.16.0-6.0.pre.41 on channel master at /Users/damilolaalimi/fvm/versions/master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 6ddeec2a6a (81 minutes ago), 2023-10-09 02:48:25 -0400
        • Engine revision 53da096c0c
        • Dart version 3.3.0 (build 3.3.0-1.0.dev)
        • DevTools version 2.28.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
        • Android SDK at /Users/damilolaalimi/Library/Android/sdk
        • Platform android-34, build-tools 34.0.0
        • ANDROID_HOME = /Users/damilolaalimi/Library/Android/sdk
        • 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)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14E300c
        • CocoaPods version 1.12.1
    
    [✓] 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)
    
    [!] Android Studio (version unknown)
        • Android Studio at /Users/damilolaalimi/Downloads/Android Studio Preview.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
        ✗ Unable to determine Android Studio version.
        • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    
    [✓] VS Code (version 1.82.3)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.50.0
    
    [✓] Connected device (4 available)
        • sdk gphone64 arm64 (mobile) • emulator-5554                        • android-arm64  • Android 14 (API 34) (emulator)
        • iPhone 14 Pro Max (mobile)  • BB55E997-7F31-462D-B3B1-6B177D9B40C7 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator)
        • macOS (desktop)             • macos                                • darwin-arm64   • macOS 13.0 22A380 darwin-arm64
        • Chrome (web)                • chrome                               • web-javascript • Google Chrome 117.0.5938.149
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    

  3. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    p: material_uimaterial_ui package in flutter/packages
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    team-designOwned by Design Languages team
    and removed
    in triagePresently being triaged by the triage team
    on Oct 9, 2023
  4. self-assigned this
    on Oct 11, 2023
  5. added
    P2Important issues not at the top of the work list
    triaged-designTriaged by Design Languages team
    on Oct 11, 2023
  6. HansMuller commented on Nov 2, 2023

    @HansMuller
    Contributor

    The example is working as intended, the repeated builds you're seeing are due to the fade-in/out animations used to segue between selected destinations. You could eliminate the animations if you wanted to reduce the number of builds, for example:

    no-animation version of the example
    import 'package:flutter/material.dart';
    
    /// Flutter code sample for [NavigationBar] with nested [Navigator] destinations.
    
    void main() {
      runApp(const MaterialApp(home: Home()));
    }
    
    class Home extends StatefulWidget {
      const Home({super.key});
    
      @override
      State<Home> createState() => _HomeState();
    }
    
    class _HomeState extends State<Home> with TickerProviderStateMixin<Home> {
      static const List<Destination> allDestinations = <Destination>[
        Destination(0, 'Teal', Icons.home, Colors.teal),
        Destination(1, 'Cyan', Icons.business, Colors.cyan),
        Destination(2, 'Orange', Icons.school, Colors.orange),
        Destination(3, 'Blue', Icons.flight, Colors.blue),
      ];
    
      late final List<GlobalKey<NavigatorState>> navigatorKeys;
      late final List<GlobalKey> destinationKeys;
      late final List<AnimationController> destinationFaders;
      late final List<Widget> destinationViews;
      int selectedIndex = 0;
    
      AnimationController buildFaderController() {
        final AnimationController controller = AnimationController(
          vsync: this,
          duration: const Duration(milliseconds: 300),
        );
        controller.addStatusListener(
          (AnimationStatus status) {
            if (status == AnimationStatus.dismissed) {
              setState(() {}); // Rebuild unselected destinations offstage.
            }
          },
        );
        return controller;
      }
    
      @override
      void initState() {
        super.initState();
    
        navigatorKeys = List<GlobalKey<NavigatorState>>.generate(
          allDestinations.length,
          (int index) => GlobalKey(),
        ).toList();
    
        destinationFaders = List<AnimationController>.generate(
          allDestinations.length,
          (int index) => buildFaderController(),
        ).toList();
        destinationFaders[selectedIndex].value = 1.0;
    
        final CurveTween tween = CurveTween(curve: Curves.fastOutSlowIn);
        destinationViews = allDestinations.map<Widget>(
          (Destination destination) {
            return DestinationView(
              destination: destination,
              navigatorKey: navigatorKeys[destination.index],
            );
          },
        ).toList();
      }
    
      @override
      void dispose() {
        for (final AnimationController controller in destinationFaders) {
          controller.dispose();
        }
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return WillPopScope(
          onWillPop: () async {
            final NavigatorState navigator =
                navigatorKeys[selectedIndex].currentState!;
            if (!navigator.canPop()) {
              return true;
            }
            navigator.pop();
            return false;
          },
          child: Scaffold(
            body: SafeArea(
              top: false,
              child: Stack(
                fit: StackFit.expand,
                children: allDestinations.map(
                  (Destination destination) {
                    final int index = destination.index;
                    final Widget view = destinationViews[index];
                    if (index == selectedIndex) {
                      return Offstage(offstage: false, child: view);
                    } else {
                      return Offstage(child: view);
                    }
                  },
                ).toList(),
              ),
            ),
            bottomNavigationBar: NavigationBar(
              selectedIndex: selectedIndex,
              onDestinationSelected: (int index) {
                setState(() {
                  selectedIndex = index;
                });
              },
              destinations: allDestinations.map<NavigationDestination>(
                (Destination destination) {
                  return NavigationDestination(
                    icon: Icon(destination.icon, color: destination.color),
                    label: destination.title,
                  );
                },
              ).toList(),
            ),
          ),
        );
      }
    }
    
    class Destination {
      const Destination(this.index, this.title, this.icon, this.color);
      final int index;
      final String title;
      final IconData icon;
      final MaterialColor color;
    }
    
    class RootPage extends StatelessWidget {
      const RootPage({super.key, required this.destination});
    
      final Destination destination;
    
      Widget _buildDialog(BuildContext context) {
        return AlertDialog(
          title: Text('${destination.title} AlertDialog'),
          actions: <Widget>[
            TextButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: const Text('OK'),
            ),
          ],
        );
      }
    
      @override
      Widget build(BuildContext context) {
        final TextStyle headlineSmall = Theme.of(context).textTheme.headlineSmall!;
        final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
          backgroundColor: destination.color,
          foregroundColor: Colors.white,
          visualDensity: VisualDensity.comfortable,
          padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
          textStyle: headlineSmall,
        );
    
        return Scaffold(
          appBar: AppBar(
            title: Text('${destination.title} RootPage - /'),
            backgroundColor: destination.color,
            foregroundColor: Colors.white,
          ),
          backgroundColor: destination.color[50],
          body: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                ElevatedButton(
                  style: buttonStyle,
                  onPressed: () {
                    Navigator.pushNamed(context, '/list');
                  },
                  child: const Text('Push /list'),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  style: buttonStyle,
                  onPressed: () {
                    showDialog(
                      context: context,
                      useRootNavigator: false,
                      builder: _buildDialog,
                    );
                  },
                  child: const Text('Local Dialog'),
                ),
                const SizedBox(height: 16),
                ElevatedButton(
                  style: buttonStyle,
                  onPressed: () {
                    showDialog(
                      context: context,
                      useRootNavigator: true,
                      builder: _buildDialog,
                    );
                  },
                  child: const Text('Root Dialog'),
                ),
                const SizedBox(height: 16),
                Builder(
                  builder: (BuildContext context) {
                    return ElevatedButton(
                      style: buttonStyle,
                      onPressed: () {
                        showBottomSheet(
                          context: context,
                          builder: (BuildContext context) {
                            return Container(
                              padding: const EdgeInsets.all(16),
                              width: double.infinity,
                              child: Text(
                                '${destination.title} BottomSheet\n'
                                'Tap the back button to dismiss',
                                style: headlineSmall,
                                softWrap: true,
                                textAlign: TextAlign.center,
                              ),
                            );
                          },
                        );
                      },
                      child: const Text('Local BottomSheet'),
                    );
                  },
                ),
              ],
            ),
          ),
        );
      }
    }
    
    class ListPage extends StatelessWidget {
      const ListPage({super.key, required this.destination});
    
      final Destination destination;
    
      @override
      Widget build(BuildContext context) {
        const int itemCount = 50;
        final ColorScheme colorScheme = Theme.of(context).colorScheme;
        final ButtonStyle buttonStyle = OutlinedButton.styleFrom(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8),
            side: BorderSide(width: 1, color: colorScheme.onSurface.withOpacity(0.12)),
          ),
          foregroundColor: destination.color,
          fixedSize: const Size.fromHeight(64),
          textStyle: Theme.of(context).textTheme.headlineSmall,
        );
        return Scaffold(
          appBar: AppBar(
            title: Text('${destination.title} ListPage - /list'),
            backgroundColor: destination.color,
            foregroundColor: Colors.white,
          ),
          backgroundColor: destination.color[50],
          body: SizedBox.expand(
            child: ListView.builder(
              itemCount: itemCount,
              itemBuilder: (BuildContext context, int index) {
                return Padding(
                  padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
                  child: OutlinedButton(
                    style: buttonStyle.copyWith(
                      backgroundColor: MaterialStatePropertyAll<Color>(
                        Color.lerp(
                          destination.color[100],
                          Colors.white,
                          index / itemCount
                        )!,
                      ),
                    ),
                    onPressed: () {
                      Navigator.pushNamed(context, '/text');
                    },
                    child: Text('Push /text [$index]'),
                  ),
                );
              },
            ),
          ),
        );
      }
    }
    
    class TextPage extends StatefulWidget {
      const TextPage({super.key, required this.destination});
    
      final Destination destination;
    
      @override
      State<TextPage> createState() => _TextPageState();
    }
    
    class _TextPageState extends State<TextPage> {
      late final TextEditingController textController;
    
      @override
      void initState() {
        super.initState();
        textController = TextEditingController(text: 'Sample Text');
      }
    
      @override
      void dispose() {
        textController.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        final ThemeData theme = Theme.of(context);
        return Scaffold(
          appBar: AppBar(
            title: Text('${widget.destination.title} TextPage - /list/text'),
            backgroundColor: widget.destination.color,
            foregroundColor: Colors.white,
          ),
          backgroundColor: widget.destination.color[50],
          body: Container(
            padding: const EdgeInsets.all(32.0),
            alignment: Alignment.center,
            child: TextField(
              controller: textController,
              style: theme.primaryTextTheme.headlineMedium?.copyWith(
                color: widget.destination.color,
              ),
              decoration: InputDecoration(
                focusedBorder: UnderlineInputBorder(
                  borderSide: BorderSide(
                    color: widget.destination.color,
                    width: 3.0,
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    
    class DestinationView extends StatefulWidget {
      const DestinationView({
        super.key,
        required this.destination,
        required this.navigatorKey,
      });
    
      final Destination destination;
      final Key navigatorKey;
    
      @override
      State<DestinationView> createState() => _DestinationViewState();
    }
    
    class _DestinationViewState extends State<DestinationView> {
      @override
      Widget build(BuildContext context) {
        return Navigator(
          key: widget.navigatorKey,
          onGenerateRoute: (RouteSettings settings) {
            return MaterialPageRoute<void>(
              settings: settings,
              builder: (BuildContext context) {
                switch (settings.name) {
                  case '/':
                    return RootPage(destination: widget.destination);
                  case '/list':
                    return ListPage(destination: widget.destination);
                  case '/text':
                    return TextPage(destination: widget.destination);
                }
                assert(false);
                return const SizedBox();
              },
            );
          },
        );
      }
    }

    I've updated the description of the example to make it clear that changing destinations animates the app: #137788

  7. added a commit that references this issue on Nov 3, 2023
    3479aab
  8. added
    r: fixedIssue is closed as already fixed in a newer version
    on Nov 6, 2023
  9. github-actions commented on Nov 20, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listd: examplesSample code and demosfound in release: 3.13Found to occur in 3.13found in release: 3.16Found to occur in 3.16frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions