简体   繁体   中英

How can I pop to specific screen in flutter

I pushed four screens ScreenOne >> ScreenTwo >> ScreenThree >> ScreenFour

and I'm at ScreenFour now I want to pop back to ScreenTwo

In Swift it works like this::

if let viewControllers: [UIViewController] = self.navigationController!.viewControllers {
    for vc in viewControllers
    {
        if (vc is SecondViewController)
        {
            self.navigationController!.popToViewController(vc, animated: true)
        }
    }
}

how i perform this operation in flutter.

Please give a solution, Thank you .

If you didn't define any route in the MaterialApp then you need to define at the time of push.

Navigator.of(context).push(
    MaterialPageRoute(builder: (_) {
      return SecondPage();
    },
      settings: RouteSettings(name: 'SecondPage',),
    ));

You need to define the same route name

Navigator.of(context).popUntil((route){
  return route.settings.name == 'SecondPage';
}); 

try pushNamedAndRemoveUntil ,

        Navigator.of(context).pushNamedAndRemoveUntil(
            '/BottomNavigation', (Route<dynamic> route) => false);

For in depth reading follow this

I believe that would be the Navigator.popUntil Method.

You need to have your routes defined in your materialApp for Navigator to recognise it. For the code below to work "/home" needs to be defined in my MaterialApp.

Navigator.popUntil(context, ModalRoute.withName('/home'));

If you don't want to use named route then simply use-- pushAndRemoveUntil

Example--

Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
                          builder: (context) => MyHomePage()), (route) => false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM