如何在Flutter应用中重置基本路线-弹出所有路线并使用导航器将其替换为新路线 [英] How to reset the base route in my Flutter App - that is pop any routes and replace it with a new one using Navigator

查看:84
本文介绍了如何在Flutter应用中重置基本路线-弹出所有路线并使用导航器将其替换为新路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Flutter的新手,如果这很明显,请原谅我,但是我已经阅读了文档并尝试了很多Navigator选项,但是它们都不如我所愿。问题集:我们有一系列复杂的(很好,半复杂)的屏幕等,它们处理登录/授权/注册过程。在路由堆栈上,这可以降至3或4级。一切都很好-不用担心。

New to Flutter to please forgive me if this is obvious, but I have read the documentation and tried a bunch of the Navigator options but none of them work as I would like. The problem set: We have a complex (well, semi-complex) series of screens, etc. that handle the login / authorization / registration process. This can get down to 3 or 4 levels on the Route stack. This is all fine -- no worries.

问题是当我们成功登录一次事件(从几个不同的路径)并想去家时出现页。进入首页后,它应该是路线树的新根(我确信这不是正确的术语,但我认为这个想法很可靠)。

The problem comes when we get a successful login event (from a few different paths) and want to go to the "home" page. Once we are on the home page that should be the new "root" of the Route tree (I am sure this isn't the correct terminology -- but I think the idea is solid).

因此,鉴于我们可能下降1/2/3甚至4级,并且希望弹出并替换整个堆栈(请提供任何转换事件)到新的顶层根目录-哪些神奇的Navigator集合或方法将彻底做到这一点?

So, given we could be 1/2/3 or even 4 levels down and want to "pop" and replace the whole stack (with any transition events, please) to a new top level root -- what magical Navigator set or methods will do this cleanly?

我当前的(可怕的)方法是将 pop()传递给各个级别并执行Navigator.pushReplacementNamed()调用(它们在此均被命名为路线),但是这不是一个通用的解决方案(必须确切地知道多少个级别),更糟糕的是,它会导致屏幕上的动画过渡到 pop,每个pop()的瞬间瞬间看起来不是很好。

My current (horrid) approach is to hand "pop()" the levels and do a Navigator.pushReplacementNamed() call (they are all named routes here) but that isn't a generic solution (have to know exactly how many levels) and worse, it causes a "animation" transition to "pop" on the screen for a split second for each pop() which looks .. not very good.

TIA!

推荐答案

Navigator暴露的不仅仅是 pop 。您可以执行以下操作:

Navigator expose more than just pop. You can do things such as the following:

Navigator.pushNamedAndRemoveUntil(context, '/', (_) => false);

这基本上将推开房屋,并删除所有 all 新路线一个

This will basically push a home and remove all the routes behind the new one

这篇关于如何在Flutter应用中重置基本路线-弹出所有路线并使用导航器将其替换为新路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆