3

I have an App with a LoginScreen that goes to the HomeScreen after a success login. I use this code below to replace the widget and start a new navigation Stack:

Navigator.pushReplacement(
    context,
    CupertinoPageRoute(
      builder: (context) => HomeScreen(),
    ),
  );

The HomeScreen is a CupertinoTabScaffold with 2 CupertinoTabViews.

The second CupertinoTabView contain a Widget that has a logout button.

After a success logout I want to remove the HomeWidget, and go to LoginScreen.

Using

Navigator.pushReplacement(
    context,
    CupertinoPageRoute(
      builder: (context) => LoginScreen(),
    ),
  );

in a Widget inside the second CupertinoTabView only reset its navigation, and the LoginScreen appears inside the second CupertinoTabView.

What I want is some code to remove my HomeScreen and start a new Navigation stack with the LoginScreen.

Jofre
  • 531
  • 7
  • 10
  • I used the solution accepted in this post, as a workaround, but I'm not happy with it. https://stackoverflow.com/questions/50115311/flutter-how-to-force-an-application-restart-in-production-mode – Jofre Jul 17 '19 at 14:16

2 Answers2

6

Found solution from the official FlutterGallery source code. This will navigate route to root.

https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/cupertino/cupertino_navigation_demo.dart#L122

Navigator.of(context, rootNavigator: true).pop();
Kevin Khew
  • 471
  • 5
  • 11
0
              Navigator.of(context, rootNavigator: true).pushAndRemoveUntil( CupertinoPageRoute(builder: (context) => SignUpPage()), (route) => false);

This worked me, maybe you can try it out

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 04 '22 at 07:19