简体   繁体   English

Flutter - 导航到新屏幕,并清除所有以前的屏幕

[英]Flutter - Navigate to a new screen, and clear all the previous screens

I used Navigator.push up to 6 screens to get to the payment page.我使用Navigator.push最多 6 个屏幕才能到达付款页面。 After Payment, I want to push to the "Payment Successful" page then remove all the previous screens ie using the back button will return to the very first screen.付款后,我想推送到“付款成功”页面,然后删除所有以前的屏幕,即使用后退按钮将返回到第一个屏幕。

NOTE: I have tried pushReplacementNamed and it doesn't work.注意:我已经尝试过pushReplacementNamed但它不起作用。

I figured it out.我想到了。 It was the Navigator.pushAndRemoveUntil function.它是Navigator.pushAndRemoveUntil函数。 Where i had to pass the PaymentSuccessful widget as the newRoute , and the "/Home" route as the predicate我必须将PaymentSuccessful小部件作为newRoute ,并将"/Home"路由作为谓词传递

  _navPaymentSuccessful(){
    Navigator.pushAndRemoveUntil(
      context, 
      MaterialPageRoute(
        builder: (context) => PaymentSuccessful()
      ), 
     ModalRoute.withName("/Home")
    );
  }

even simpler and I think a better way would be to do it this way, this Schedules a callback for the end of the current persistent frame,to push to route /loginPage and removes all the previous routes,this way you can make sure that all the frames are rendered and then you navigate to next page.甚至更简单,我认为更好的方法是这样做,这会为当前持久帧的末尾安排一个回调,推送到路由/loginPage并删除所有以前的路由,这样您就可以确保所有帧被渲染,然后你导航到下一页。

 SchedulerBinding.instance.addPostFrameCallback((_) {
                Navigator.of(context).pushNamedAndRemoveUntil(
                    '/loginPage', (Route<dynamic> route) => false);
              });

Accepted Answer is correct.接受 答案是正确的。 But you can try this too.但是你也可以试试这个。

Navigator.pushAndRemoveUntil<dynamic>(
        context,
        MaterialPageRoute<dynamic>(
          builder: (BuildContext context) => YourPageNameGoesHere(),
        ),
        (route) => false,//if you want to disable back feature set to false
);

I would Suggest use WillPopScope in your Payment successful page and onWillPop method write following snippet of code:我建议在您的付款成功页面和 onWillPop 方法中使用 WillPopScope 编写以下代码片段:

 return WillPopScope(
      onWillPop: (){
        Navigator.of(context)
            .pushNamedAndRemoveUntil('/Home', (Route<dynamic> route) => false);
      },
      child: Scaffold()
};

Try this if you want pass arguments to new page:如果你想将 arguments 传递到新页面,试试这个:

Navigator.of(context).pushNamedAndRemoveUntil(
      '/new-route-name',
      arguments: {
         any object
      },
      ModalRoute.withName("/the-route-name-that-you-want-back-to-it")
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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