Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigator.push cause the front page Scaffold rebuild. #63312

Closed
love-flutter opened this issue Aug 9, 2020 · 13 comments
Closed

Navigator.push cause the front page Scaffold rebuild. #63312

love-flutter opened this issue Aug 9, 2020 · 13 comments
Labels
c: performance Relates to speed or footprint issues (see "perf:" labels) f: material design flutter/packages/flutter/material repository. f: routes Navigator, Router, and related APIs. found in release: 1.20 Found to occur in 1.20 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on r: fixed Issue is closed as already fixed in a newer version

Comments

@love-flutter
Copy link

love-flutter commented Aug 9, 2020

void main() {
  runApp(TestApp());
}
class TestApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Two You', 
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
          primarySwatch: Colors.blue, 
        ),
        home: Container(
          child: TestPageTestRebuild(),
        ));
  }
}
import 'package:flutter/material.dart';

/// 测试页面
class TestPageTestRebuild extends StatelessWidget {
  /// 构造函数
  const TestPageTestRebuild();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        key: Key('test_push_key'),
        appBar: AppBar(),
        body: Container(
          child: FlatButton(
            onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (context) => Text('second page'))),
            child: Center(
              child: Text('Test Push'),
            )),
        ));
  }
}

if you run the code, click the "Test Push", you will see the Scaffold rebuild, if the Scaffold has BottomNavigationBar, I think it will cause some performance problem.

@xu-baolin
Copy link
Member

xu-baolin commented Aug 10, 2020

see #11655
Believe that the widget tree is cheap and unstable, will not cause performance problems.

@love-flutter
Copy link
Author

love-flutter commented Aug 10, 2020

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: Key('1111'),
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Open route'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondRoute()),
            );
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: Key('2222'),
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

Not the same problem with #11655

it is the example in flutter.dev. if click the button the Scaffold will build twice, but when we check the performance in devtools, it takes up a very long time in these widgets.

@TahaTesser
Copy link
Member

Hi @love-flutter
Can you please provide your flutter doctor -v? Also "we check the performance in devtools, it takes up a very long time in these widgets." Please provide more details and steps to reproduce this

Thank you

@TahaTesser TahaTesser added in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds labels Aug 10, 2020
@love-flutter
Copy link
Author

love-flutter commented Aug 16, 2020

[✓] Flutter (Channel stable, v1.17.5, on Mac OS X 10.15.6 19G73, locale zh-Hans-CN)
    • Flutter version 1.17.5 at /Users/xxx/flutter_sdk/flutter
    • Framework revision 8af6b2f038 (7 weeks ago), 2020-06-30 12:53:55 -0700
    • Engine revision ee76268252
    • Dart version 2.8.4

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/xxx/Library/Android/sdk
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = /Users/xxx/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3.1, Build version 11C504
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (2 available)
    • MI 5X             • 478401db0304                         • android-arm64 • Android 8.1.0 (API 27)
    • iPhone 11 Pro Max • 189F8503-C052-4C5B-B1C3-869ED289BE86 • ios           • com.apple.CoreSimulator.SimRuntime.iOS-13-3 (simulator)

• No issues found! 

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Aug 16, 2020
@love-flutter
Copy link
Author

image
This is the FPS, when we click the frame that big than 16.67ms in the last frame, we will find.
image
I do not know why it should update the scaffold, and how we can close it if we do not need to update the first page.

@love-flutter
Copy link
Author

This is the test code.

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    debugShowCheckedModeBanner: false,
      navigatorKey: GlobalKey(),
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: Key('1111'),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.people),
            title: Text('推荐'),
            activeIcon: Icon(Icons.people_outline),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.favorite),
            title: Text('关注'),
            activeIcon: Icon(Icons.favorite_border),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text('我'),
            activeIcon: Icon(Icons.person_outline),
          )
        ],
        iconSize: 24,
        currentIndex: 0,

        /// 选中后,底部BottomNavigationBar内容的颜色(选中时,默认为主题色)
        /// (仅当type: BottomNavigationBarType.fixed,时生效)
        fixedColor: Colors.lightBlueAccent,
        type: BottomNavigationBarType.fixed,
        onTap: (int index) {

        },
      ),
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Open route'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondRoute()),
            );
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      key: Key('2222'),
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
} 

@love-flutter
Copy link
Author

I have checked the source code, it cause by the _EffectiveTickerMode(effective mode: enabled) notifyClients. But I do not know why it must be related with _EffectiveTickerMode.

  void notifyClients(InheritedWidget oldWidget) {
    assert(_debugCheckOwnerBuildTargetExists('notifyClients'));
    for (final Element dependent in _dependents.keys) {
      assert(() {
        // check that it really is our descendant
        Element ancestor = dependent._parent;
        while (ancestor != this && ancestor != null)
          ancestor = ancestor._parent;
        return ancestor == this;
      }());
      // check that it really depends on us
      assert(dependent._dependencies.contains(this));
      notifyDependent(oldWidget, dependent);
    }
  }

@TahaTesser
Copy link
Member

Hi @love-flutter
I can't see low FPS on latest stable using your code sample, also adding print in first scaffold only calls it once

D/InputMethodManager(11167): startInputInner - Id : 0
I/flutter (11167): frist
D/ViewRootImpl@ec11060[MainActivity](11167): ViewPostIme pointer 0
D/ViewRootImpl@ec11060[MainActivity](11167): ViewPostIme pointer 1
I/flutter (11167): second

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

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    debugShowCheckedModeBanner: false,
    navigatorKey: GlobalKey(),
    showPerformanceOverlay: true,
    home: FirstRoute(),
  ));
}

class FirstRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('frist');
    return Scaffold(
      key: Key('1111'),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.people),
            title: Text('推荐'),
            activeIcon: Icon(Icons.people_outline),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.favorite),
            title: Text('关注'),
            activeIcon: Icon(Icons.favorite_border),
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            title: Text('我'),
            activeIcon: Icon(Icons.person_outline),
          )
        ],
        iconSize: 24,
        currentIndex: 0,

        /// 选中后,底部BottomNavigationBar内容的颜色(选中时,默认为主题色)
        /// (仅当type: BottomNavigationBarType.fixed,时生效)
        fixedColor: Colors.lightBlueAccent,
        type: BottomNavigationBarType.fixed,
        onTap: (int index) {},
      ),
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Open route'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondRoute()),
            );
          },
        ),
      ),
    );
  }
}

class SecondRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: Key('2222'),
      appBar: AppBar(
        title: Text("Second Route"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}
flutter doctor -v
[✓] Flutter (Channel dev, 1.21.0-9.0.pre, on Mac OS X 10.15.6 19G2021, locale en-GB)
    • Flutter version 1.21.0-9.0.pre at /Users/tahatesser/Code/flutter_dev
    • Framework revision 7c6f9dd239 (8 days ago), 2020-08-09 10:31:03 -0400
    • Engine revision 6d86e67f04
    • Dart version 2.10.0 (build 2.10.0-4.0.dev 0341576448)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/tahatesser/Code/sdk
    • Platform android-30, build-tools 30.0.1
    • ANDROID_HOME = /Users/tahatesser/Code/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.6, Build version 11E708
    • CocoaPods version 1.9.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 48.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.48.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.13.2

 
[✓] Connected device (4 available)            
    • SM M305F (mobile) • 32003c30dc19668f • android-arm64  • Android 10 (API 29)
    • macOS (desktop)   • macos            • darwin-x64     • Mac OS X 10.15.6 19G2021
    • Web Server (web)  • web-server       • web-javascript • Flutter Tools
    • Chrome (web)      • chrome           • web-javascript • Google Chrome 84.0.4147.125

• No issues found!

@TahaTesser TahaTesser added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Aug 17, 2020
@love-flutter
Copy link
Author

@TahaTesser ok thanks, will try the latest version.

@no-response no-response bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Aug 24, 2020
@love-flutter
Copy link
Author

love-flutter commented Aug 24, 2020

@TahaTesser I think it still has this issue, I have used the latest flutter sdk.
You can update the source code of

packages/flutter/lib/src/material/scaffold.dart 

add the code in the scaffold build method, and then run the example code above, when you click the button open the second page, you will see the output below

@override
  Widget build(BuildContext context) {
    print(context.widget.key);
    assert(debugCheckHasMediaQuery(context));
    assert(debugCheckHasDirectionality(context));
    final MediaQueryData mediaQuery = MediaQuery.of(context);
    final ThemeData themeData = Theme.of(context);
    final TextDirection textDirection = Directionality.of(context);
flutter: [<'2222'>]
flutter: [<'1111'>]
[✓] Flutter (Channel stable, 1.20.2, on Mac OS X 10.15.6 19G73, locale
    zh-Hans-CN)
    • Flutter version 1.20.2 at /Users/xxx/Downloads/flutter3
    • Framework revision bbfbf1770c (11 days ago), 2020-08-13 08:33:09 -0700
    • Engine revision 9d5b21729f
    • Dart version 2.9.1
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/xxx/Library/Android/sdk
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = /Users/xxx/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3.1, Build version 11C504
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 47.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (2 available)
    • MI 5X (mobile)             • 478401db0304                         •
      android-arm64 • Android 8.1.0 (API 27)
    • iPhone 11 Pro Max (mobile) • 189F8503-C052-4C5B-B1C3-869ED289BE86 • ios
      • com.apple.CoreSimulator.SimRuntime.iOS-13-3 (simulator)

• No issues found!

@TahaTesser
Copy link
Member

TahaTesser commented Aug 24, 2020

When adding modifications to scaffold.art source, it does show scaffold rebuilding on avigator.push

Launching lib/main.dart on iPhone 11 in debug mode...
Xcode build done.                                           32.5s
Connecting to VM Service at ws://127.0.0.1:49800/vEVmE14T70k=/ws
flutter: [<'1111'>]
flutter: [<'2222'>]
flutter: [<'1111'>]
flutter doctor -v
[✓] Flutter (Channel stable, 1.20.2, on Mac OS X 10.15.6 19G2021, locale en-GB)
    • Flutter version 1.20.2 at /Users/tahatesser/Code/flutter_stable
    • Framework revision bbfbf1770c (11 days ago), 2020-08-13 08:33:09 -0700
    • Engine revision 9d5b21729f
    • Dart version 2.9.1

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /Users/tahatesser/Code/sdk
    • Platform android-30, build-tools 30.0.1
    • ANDROID_HOME = /Users/tahatesser/Code/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.6)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.6, Build version 11E708
    • CocoaPods version 1.9.3

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 48.1.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.48.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.13.2

[✓] Connected device (1 available)
    • iPhone 11 (mobile) • A63D6F51-59F3-4A75-961F-3769709F3A92 • ios •
      com.apple.CoreSimulator.SimRuntime.iOS-13-6 (simulator)

• No issues found!

@TahaTesser TahaTesser added f: routes Navigator, Router, and related APIs. found in release: 1.20 Found to occur in 1.20 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on c: performance Relates to speed or footprint issues (see "perf:" labels) f: material design flutter/packages/flutter/material repository. and removed in triage Presently being triaged by the triage team labels Aug 24, 2020
@maheshmnj
Copy link
Member

Hi @love-flutter, I tried to reproduce the issue on the latest master channel and this is no longer reproducible. I added the print statement in the build method of Scaffold(packages/flutter/lib/src/material/scaffold.dart) and I see the widget builds only once.

Screen.Recording.2022-11-22.at.5.58.40.PM.mov
flutter doctor -v (mac)
[✓] Flutter (Channel master, 3.6.0-6.0.pre.33, on macOS 12.6 21G115 darwin-arm64,
    locale en-IN)
    • Flutter version 3.6.0-6.0.pre.33 on channel master at
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision a9858ec524 (72 minutes ago), 2022-11-21 10:58:11 -0500
    • Engine revision 46a6b54295
    • Dart version 2.19.0 (build 2.19.0-406.0.dev)
    • DevTools version 2.19.0
    • If those were intentional, you can disregard the above warnings; however it is
      recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0-rc4
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

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

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.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.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

[✓] VS Code (version 1.70.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.53.20221101

[✓] Connected device (3 available)
    • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios
      • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64
      • macOS 12.6 21G115 darwin-arm64
    • Chrome (web)           • chrome                               • web-javascript
      • Google Chrome 107.0.5304.110

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Closing as fixed, Incase you still encounter the issue feel free to write back with more details and we will reopen it.

Thank you.

@maheshmnj maheshmnj added the r: fixed Issue is closed as already fixed in a newer version label Nov 22, 2022
@github-actions
Copy link

github-actions bot commented Mar 5, 2023

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: performance Relates to speed or footprint issues (see "perf:" labels) f: material design flutter/packages/flutter/material repository. f: routes Navigator, Router, and related APIs. found in release: 1.20 Found to occur in 1.20 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on r: fixed Issue is closed as already fixed in a newer version
Projects
None yet
Development

No branches or pull requests

4 participants