Flutter GetX 路由历史记录

3
GetX是否能够查看导航栈?我在文档中查找,但没有找到相关内容。通常,我会像这样关闭对话框:
Get.until((route) => !Get.isDialogOpen);

但我想知道是否可以在路由历史记录中包含特定页面实例的情况下关闭路由,大致代码如下:

  Get.until((route) => !Get.routingHistory.contains('/someRoute'));

请注意,这不是有效的语法。

你的意思是当你在另一个页面时,想要从页面历史记录中关闭页面吗? - Ahmad Darwesh
我想要持续关闭页面,直到我的导航栈中不再包含该路由的实例。 - anonymous-dev
离线路 /someRoute * - anonymous-dev
7个回答

8

Get.until 在满足条件之前移除屏幕。 它与Navigation.popUntil()相同。 您可以这样使用它:Get.until((route) => Get.currentRoute == '/home')。

Get.offNamed 通过命名路由,删除当前屏幕并添加新屏幕。 它与Navigation.pushReplacementNamed()相同。 您可以这样使用它:Get.offNamed('/second')。

Get.offAndToNamed 通过命名路由,先添加新屏幕,然后删除以前的屏幕。 它与Navigation.popAndPushNamed()相同。 您可以这样使用它:Get.offAndToNamed('/second')。

Get.offUntil 移除屏幕直到满足条件,然后添加一个新屏幕。 它与Navigation.pushAndRemoveUntil()相同。 您可以这样使用它:Get.offUntil(page, (route) => (route as GetPageRoute).routeName == '/home')。

Get.offNamedUntil 通过命名路由,移除屏幕直到满足条件,然后添加新屏幕。 它与Navigation.pushNamedAndRemoveUntil()相同。 您可以这样使用它:Get.offNamedUntil(page, ModalRoute.withName('/home'))。

请根据您的用例使用。


6

你需要使用:

Get.offUntil(page, (route) => false)

page 表示要导航到的新页面。

(route) => false

这是一个条件语句。


1
Get.offUntil(<page>, (route) => false) 的第一个参数是什么?或者如何导航到新页面?我想直接在 <page> 处传递一个类名,而不想使用命名路由作为参数。这种做法可行吗? - Dhiren Basra
1
我们可以这样使用: RaisedButton( child: Text("关闭直到第四个"), onPressed: () => Get.offUntil( MaterialPageRoute( builder: (context) => Second(), maintainState: false), (route) => false)), - webaddicted

4

GetX还有另一个有用的功能:

int times = 2;
Get.close(times);

根据[times]的定义关闭尽可能多的路由


2
如果你想一直关闭路由,直到到达页面路由...
Navigator.of(context).popUntil(ModalRoute.withName('/route-name'));

0
It is possible. Navigator.popUntil pops pages until a passed predicate returns true. We can query the following route in the navigator stack and decide what decision to make. 
The GetX method for doing the same is
`

    Get.offUntil( MaterialPageRoute(builder: (context) => const NewPage()), (route) {
    var currentRoute = route.settings.name;
      debugPrint("Get.currentRoute --- $currentRoute");
        if(currentRoute  == "/Home") {
          return true;
        } else {
          return false;
        }
    }

`


The code above pops until home. Also, we can add custom logic in the if-else block above. 

0
Get.until((route) {
                if (route.settings.name == Routes.TEST1) {
                  //Return to the specified page
                  return true;
                } else {
                  return false;
                }
              });

2
仅提供代码的答案不被认为是好的答案,很可能会被投票降低和/或删除,因为它们对学习者社区来说不太有用。这只是对你来说很明显。请解释它的作用,以及它与现有答案的不同之处/ 优点。以下是如何撰写好的答案?的一些指南。 - lepsch

-1
Get.offAll(Home());  // remove all previous routes and redirect to home

关于namedRoutes的使用:

Get.offAllNamed('/home');

那只会重新渲染主页,这没有意义。如果主页已经在堆栈上了,就直接使用它。 - Lucifer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接