Skip to content

The back button appears for a few moments while I replace a route with Navigator 2.0. #67277

Closed

Description

Hello everyone!

I'm trying to use the new Navigator like this :

Navigator(
  pages: [
    if (showSignIn)
      MaterialPage(
        key: ValueKey('SignIn'),
        child: SignIn(
          switchToSignUp: () {
            setState(() => showSignIn = false);
          },
        ),
      )
    else
      MaterialPage(
        key: ValueKey('SignUp'),
        child: SignUp(
          switchToSignIn: () {
            setState(() => showSignIn = true);
          },
        ),
      )
  ],
  onPopPage: (route, result) => route.didPop(result),
)

When showSignIn changes value, I want to switch from one screen to another. It works, but the back button appears for a few moments in the AppBar. Is this normal?

I tried on the master channel, but I had the same problem.

I imagined several solutions to solve this issue, but they seem a little strange to me:

  • Put the same key on both pages.
  • Create a single page (Authentication), and change widget according to showSignIn inside.

Thank you in advance.

Activity

  1. darshankawar commented on Oct 5, 2020

    @darshankawar
    Member

    @johynpapin
    Please provide flutter doctor -v and a complete minimal code sample that we can copy paste and run in IDE to see the issue.
    Thanks.

  2. added
    in triage
    waiting for response
    on Oct 5, 2020
  3. removed
    waiting for response
    on Oct 5, 2020
  4. darshankawar commented on Oct 6, 2020

    @darshankawar
    Member

    @johynpapin The back button is a default setting. If you don't want it to show upon toggle, you may set automaticallyImplyLeading: false,, that'll not show the appbar back button.

    appBar: AppBar(
            automaticallyImplyLeading: false,
            title: Text(signedIn ? 'Home' : 'Sign In'),
          ),
    

    I am going ahead and closing it, as it's not an issue. If you disagree, write in comments and I'll reopen.
    Thanks.

  5. removed
    in triage
    on Oct 6, 2020
  6. jacobaraujo7 commented on Oct 31, 2020

    @jacobaraujo7
    Contributor

    @darshankawar
    I believe that the problem has not been solved.
    When there is a replace route, canPop() becomes 'true' causing the back button to appear for a few milliseconds.
    prover

  7. jacobaraujo7 commented on Nov 3, 2020

    @jacobaraujo7
    Contributor
  8. self-assigned this
    on Nov 3, 2020
  9. darshankawar commented on Nov 4, 2020

    @darshankawar
    Member
    flutter doctor -v
    
    [✓] Flutter (Channel stable, 1.22.3, on Mac OS X 10.15.4 19E2269, locale en-IN)
        • Flutter version 1.22.3 at /Users/dhs/documents/Fluttersdk/flutter
        • Framework revision 8874f21e79 (5 days ago), 2020-10-29 14:14:35 -0700
        • Engine revision a1440ca392
        • Dart version 2.10.3
    
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
        • Android SDK at /Users/dhs/Library/Android/sdk
        • Platform android-30, build-tools 30.0.0
        • 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 12.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.0.1, Build version 12A7300
        • CocoaPods version 1.9.3
    
    [✓] Android Studio (version 4.0)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin version 46.0.2
        • Dart plugin version 193.7361
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6222593)
    
    [✓] VS Code (version 1.50.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.15.1
    
    [✓] Connected device (1 available)
        • SM A260G (mobile) • 5200763ebcfa861f • android-arm • Android 8.1.0 (API
          27)
    
    • No issues found!
    
    
  10. added
    f: routes
    found in release: 1.22
    framework
    has reproducible steps
    on Nov 4, 2020
  11. jacobaraujo7 commented on Nov 5, 2020

    @jacobaraujo7
    Contributor

    @chunhtai Why markForReplace not exist in RouteTransitionRecord?
    I believe that this would solve this bug

  12. chunhtai commented on Nov 5, 2020

    @chunhtai
    Contributor

    @jacobaraujo7 if you want to replace B with A you can use B.markForComplete and A.markForAdd to achieve the same result

  13. jacobaraujo7 commented on Nov 6, 2020

    @jacobaraujo7
    Contributor

    @jacobaraujo7 if you want to replace B with A you can use B.markForComplete and A.markForAdd to achieve the same result

    But the A.markForAdd wouldn't show the input animation, right?

  14. chunhtai commented on Nov 6, 2020

    @chunhtai
    Contributor

    @jacobaraujo7 yes, replace do not show animation too. or are you talking about pushAndReplace? in that case you can do A.markForPush

  15. jacobaraujo7 commented on Nov 6, 2020

    @jacobaraujo7
    Contributor

    cool! I'm studying Translate Delegate and I really didn't see anything wrong with the standard Delegate logic. I believe that Navigator.canPop() is True during the transition because there are actually 2 entryOverlay in the history.

  16. jacobaraujo7 commented on Nov 24, 2020

    @jacobaraujo7
    Contributor

    Hello @chunhtai
    Any news?

  17. chunhtai commented on Nov 25, 2020

    @chunhtai
    Contributor

    @jacobaraujo7 Sorry for the late reply, I am currently overwhelmed by some high priority issues. I will get to this issue by the end of the next week

  18. chunhtai commented on Dec 8, 2020

    @chunhtai
    Contributor

    Finished investigating, this is a bug in route.canpop that it check whether the route is the first or not, it return false when the route is completed but still on the route stack, this cause it to return true for route.canpop.

    I will put up a fix

  19. atnpem commented on Mar 24, 2021

    @atnpem

    Hello,

    I'm facing with this issue.

    RPReplay_Final1616577793.mov

    Flutter doctor:

    [✓] Flutter (Channel stable, 1.22.6, on macOS 11.1 20C69 darwin-arm, locale en-FR)
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    [✓] Xcode - develop for iOS and macOS (Xcode 12.4)
    [✓] Android Studio (version 4.1)
    [✓] Connected device (2 available)
    

    My sample code to reproduce:

    import 'package:flutter/material.dart';
    import 'package:flutter/foundation.dart';
    import 'dart:async';
    
    // Custom Object -> PageConfiguration
    
    const String SplashPath = '/splash';
    const String LoginPath = '/login';
    const String ListItemsPath = '/list';
    const String DetailsPath = '/details';
    
    enum Pages {
      Splash,
      Login,
      List,
      Details,
    }
    
    class PageConfiguration {
      final String key;
      final String path;
      final Pages uiPage;
    
      const PageConfiguration(
          {@required this.key, @required this.path, @required this.uiPage});
    }
    
    const PageConfiguration SplashPageConfig = PageConfiguration(key: 'Splash', path: SplashPath, uiPage: Pages.Splash);
    const PageConfiguration LoginPageConfig = PageConfiguration(key: 'Login', path: LoginPath, uiPage: Pages.Login);
    const PageConfiguration ListItemsPageConfig = PageConfiguration(key: 'List', path: ListItemsPath, uiPage: Pages.List);
    const PageConfiguration DetailsPageConfig = PageConfiguration(key: 'Details', path: DetailsPath, uiPage: Pages.Details);
    
    // Router Delegate
    
    class MyRouterDelegate extends RouterDelegate<PageConfiguration> with ChangeNotifier, PopNavigatorRouterDelegateMixin<PageConfiguration> {
    
      final List<Page> _pages = [];
    
      @override
      PageConfiguration get currentConfiguration => _pages.last.arguments as PageConfiguration;
    
      @override
      Widget build(BuildContext context) {
        return Navigator(
          key: navigatorKey,
          onPopPage: _onPopPage,
          pages: List.of(_pages),
        );
      }
    
      @override
      GlobalKey<NavigatorState> get navigatorKey => GlobalObjectKey<NavigatorState>(this);
    
      @override
      Future<void> setNewRoutePath(PageConfiguration configuration) {
        _pages.clear();
        addPage(configuration);
        return Future.value(null);
      }
    
      bool _onPopPage(Route<dynamic> route, result) {
    
        final didPop = route.didPop(result);
        if (!didPop) {
          return false;
        }
    
        _pages.remove(route.settings);
    
        notifyListeners();
    
        return true;
      }
    
      @override
      Future<bool> popRoute() {
        if (_pages.length > 1) {
          _removePage(_pages.last);
          return Future.value(true);
        }
        return Future.value(false);
      }
    
      void _removePage(MaterialPage page) {
        if (page != null) {
          _pages.remove(page);
        }
        notifyListeners();
      }
    
      void addPage(PageConfiguration pageConfig) {
    
        final shouldAddPage = _pages.isEmpty ||
            (_pages.last.arguments as PageConfiguration).uiPage !=
                pageConfig.uiPage;
    
        if (shouldAddPage) {
          switch (pageConfig.uiPage) {
            case Pages.Splash:
              _addPageData(SplashPage(), SplashPageConfig);
              break;
            case Pages.Login:
              _addPageData(LoginPage(), LoginPageConfig);
              break;
            case Pages.List:
              _addPageData(ListPage(), ListItemsPageConfig);
              break;
            case Pages.Details:
              _addPageData(DetailsPage(), DetailsPageConfig);
              break;
            default:
              break;
          }
    
          notifyListeners();
        }
      }
    
      void _addPageData(Widget child, PageConfiguration pageConfig) {
        _pages.add(
          _createPage(child, pageConfig),
        );
      }
    
      MaterialPage _createPage(Widget child, PageConfiguration pageConfig) {
        return MaterialPage(
            child: child,
            key: Key(pageConfig.key),
            name: pageConfig.path,
            arguments: pageConfig
        );
      }
    
      void replace(PageConfiguration newRoute) {
        if (_pages.isNotEmpty) {
          _pages.removeLast();
        }
        addPage(newRoute);
      }
    
      void setPath(List<MaterialPage> path) {
        _pages.clear();
        _pages.addAll(path);
        notifyListeners();
      }
    
      void replaceAll(PageConfiguration newRoute) {
        setNewRoutePath(newRoute);
      }
    
      void push(PageConfiguration newRoute) {
        addPage(newRoute);
      }
    
      void pushWidget(Widget child, PageConfiguration newRoute) {
        _addPageData(child, newRoute);
        notifyListeners();
      }
    }
    
    // Route Information Parser
    
    class MyParser extends RouteInformationParser<PageConfiguration> {
    
      @override
      Future<PageConfiguration> parseRouteInformation(RouteInformation routeInformation) {
    
        final uri = Uri.parse(routeInformation.location);
    
        if (uri.pathSegments.isEmpty) {
          return Future.value(SplashPageConfig);
        }
    
        final path = uri.pathSegments[0];
    
        switch (path) {
          case SplashPath:
            return Future.value(SplashPageConfig);
          case LoginPath:
            return Future.value(LoginPageConfig);
          case ListItemsPath:
            return Future.value(ListItemsPageConfig);
          case DetailsPath:
            return Future.value(DetailsPageConfig);
          default:
            return Future.value(SplashPageConfig);
        }
      }
    
      @override
      RouteInformation restoreRouteInformation(PageConfiguration configuration) {
        switch (configuration.uiPage) {
          case Pages.Splash:
            return const RouteInformation(location: SplashPath);
          case Pages.Login:
            return const RouteInformation(location: LoginPath);
          case Pages.List:
            return const RouteInformation(location: ListItemsPath);
          case Pages.Details:
            return const RouteInformation(location: DetailsPath);
          default:
            return const RouteInformation(location: SplashPath);
        }
      }
    }
    
    // Views
    
    final delegate = MyRouterDelegate();
    final parser = MyParser();
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
    
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
    
      _MyAppState() {
        delegate.setNewRoutePath(SplashPageConfig);
      }
    
      @override
      void initState() {
        super.initState();
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp.router(
          routeInformationParser: parser,
          routerDelegate: delegate,
        );
      }
    }
    
    class SplashPage extends StatefulWidget {
      @override
      _SplashPageState createState() => _SplashPageState();
    }
    
    class _SplashPageState extends State<SplashPage> {
      @override
      Widget build(BuildContext context) {
        return Container(
          color: Colors.white,
          child: Center(
            child: CircularProgressIndicator(),
          ),
        );
      }
    
      @override
      void didChangeDependencies() {
        super.didChangeDependencies();
        Timer(const Duration(milliseconds: 2000), () {
          delegate.replace(LoginPageConfig);
        });
      }
    }
    
    class LoginPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Login Page"),
          ),
          body: Container(
            color: Colors.red,
            child: Center(
              child: ElevatedButton(
                child: Text("Login"),
                onPressed: () {
                  delegate.replace(ListItemsPageConfig);
                },
              ),
            ),
          ),
        );
      }
    }
    
    class ListPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("List Page"),
          ),
          body: Container(
            color: Colors.blue,
            child: Center(
              child: Column(
                children: [
                  ElevatedButton(
                    child: Text("Logout"),
                    onPressed: () {
                      delegate.replace(LoginPageConfig);
                    },
                  ),
                  ElevatedButton(
                    child: Text("Show details"),
                    onPressed: () {
                      delegate.push(DetailsPageConfig);
                    },
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    class DetailsPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              title: Text("Details Page"),
            ),
            body: Container(
                color: Colors.green,
                child: Center(
                  child: ElevatedButton(
                    child: Text("Back"),
                    onPressed: () {
                      delegate.popRoute();
                    },
                  ),
                )
            )
        );
      }
    }

    Thank you for your help.

  20. github-actions commented on Aug 4, 2021

    @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.

  21. locked as resolved and limited conversation to collaborators on Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

f: routesfound in release: 1.22frameworkhas reproducible steps

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions