flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
166.3k stars 27.52k forks source link

PageRouteBuillder rebuilds its widgets. #97449

Closed neelansh-creatorstack closed 2 years ago

neelansh-creatorstack commented 2 years ago

Steps to Reproduce

I was searching about the widget rebuild problem and stumbled upon this https://github.com/flutter/flutter/issues/37878 This was working fine, but after updating the flutter version to 2.8.0 On 2.3.1everything is working as expected. I'm facing a similar issue. This is precisely happening when I'm having two MaterialApps, and my routes are defined in the

  1. Execute flutter run on the code sample
  2. In the Material App provide the route names and build a route via PageRouteBuilder
  3. In your widget have a ListView.builder with items as TextFields with print() statement in the itemBuillder.
  4. Tap on the textField and you would notice didUpdateWidget() being called every time the soft keyboard opens/closes.

Expected results:

Actual results:

Code sample ```dart import 'package:cloudinary_media_sample/home_entry_point_widget2.dart'; import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. // GlobalKey key = GlobalKey(); @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage( title: 'Title', ), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key? key, required this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { return MaterialApp( key: ValueKey('kk'), supportedLocales: [ Locale("en"), ], localizationsDelegates: [], title: 'My app', debugShowCheckedModeBanner: false, themeMode: ThemeMode.dark, theme: ThemeData(brightness: Brightness.dark, primaryColor: Colors.blue), darkTheme: ThemeData( brightness: Brightness.dark, ), initialRoute: '/home', onGenerateRoute: (settings) { var child; if (settings.name == '/home') { return MaterialPageRoute( builder: (context) { return HomeEntryPointWidget(); }, settings: settings); child = HomeEntryPointWidget(); } else if (settings.name == '/pp') { return MaterialPageRoute( builder: (context) { return HomeEntryPointWidget2(); }, settings: settings); } else { child = Container(); } return MaterialPageRoute( builder: (context) { return child; }, settings: settings); }, onUnknownRoute: (settings) => MaterialPageRoute( builder: (context) => Scaffold( backgroundColor: Colors.black12, body: Center( child: Container( child: Text(' ${settings.name} Are you lost!!'), )), ), settings: settings), builder: (context, child) { return Container( color: Colors.red, child: Padding( padding: const EdgeInsets.all(8.0), child: child, ), ); }, ); } } class HomeEntryPointWidget extends StatefulWidget { const HomeEntryPointWidget({Key? key}) : super(key: key); @override _HomeEntryPointWidgetState createState() => _HomeEntryPointWidgetState(); } class _HomeEntryPointWidgetState extends State { @override void initState() { print('inside home_entry_point_widget.dart init'); super.initState(); } @override void didChangeDependencies() { print('inside home_entry_point_widget.dart didChangeDependencies'); super.didChangeDependencies(); } @override void didUpdateWidget(covariant HomeEntryPointWidget oldWidget) { print('inside home_entry_point_widget.dart didUpdateWidget'); super.didUpdateWidget(oldWidget); } @override Widget build(BuildContext context) { print('inside home_entry_point_widget.dart build : '); return Scaffold( body: ListView.builder( itemCount: 20, itemBuilder: (context, _) { print('inside building item $_::'); return Column( children: [ GestureDetector( onTap: () => Navigator.pushReplacementNamed(context, '/pp'), child: FlutterLogo( size: 400, )), TextField(), ], ); }), ); } } ``` ``` [✓] Flutter (Channel unknown, 2.8.1, on macOS 11.4 20F71 darwin-x64, locale en-IN) • Flutter version 2.8.1 at /Users/neelansh/Downloads/flutter • Upstream repository unknown • Framework revision 77d935af4d (6 weeks ago), 2021-12-16 08:37:33 -0800 • Engine revision 890a5fca2e • Dart version 2.15.1 [!] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at /Users/neelansh/Library/Android/sdk ✗ cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. ✗ Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS (Xcode 13.1) • Xcode at /Applications/Xcode.app/Contents/Developer • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 4.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 11.0.8+10-b944.6916264) [!] Android Studio • Android Studio at /Users/neelansh/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 find bundled Java version. • Try updating or re-installing Android Studio. [✓] VS Code (version 1.55.2) • VS Code at /Users/neelansh/Downloads/Visual Studio Code.app/Contents • Flutter extension version 3.22.0 [✓] Connected device (3 available) • iPhone (mobile) • 00008030-0005394C3ED8402E • ios • iOS 15.0.2 19A404 • macOS (desktop) • macos • darwin-x64 • macOS 11.4 20F71 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 97.0.4692.99 ! Doctor found issues in 2 categories. ```
maheshj01 commented 2 years ago

Hi @neelansh-creatorstack, Thanks for filing the issue.

There is a similar issue describing this case #90550, Please follow up there for further updates, Closing this issue as a duplicate. If you disagree feel free to write in the comments and I will reopen it.

Thank you.

neelansh-creatorstack commented 2 years ago

Yes @maheshmnj , this is the same problem of having nested Material App. Once I removed the nested Material App and passed the other Material App in the builder function of the first one, there are no more rebuilds happening. But if passed in the home parameter, the above issue happens.

github-actions[bot] commented 2 years ago

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.