如何在Flutter获得当前路线?

Kya*_*Tun 20 navigation routing flutter

在实现持久性底栏时,需要在单击底栏中的按钮时恢复先前的路径.

单击底栏中的按钮时,将保存其当前路径路径(/ a/b/c),并根据按钮单击恢复先前保存的路径.

从概念上讲,用户会将每个按钮视为工作空间,其状态永远不会丢失(包括后台堆栈).用户可以安全地从一个工作区切换到另一个工

当路由重绕到root时,如何在Flutter中获取当前路由路径?

Sta*_*sVo 39

如果您想通过使用导航键获取当前路线,可以使用 popUntil 方法:

String? currentPath;
navigatorKey.currentState?.popUntil((route) {
  currentPath = route.settings.name;
  return true;
}); 
Run Code Online (Sandbox Code Playgroud)

  • 我不明白为什么我返回的所有名称都是空的。然后我发现在使用 onGenerateRoute 方法手动构建路线并返回 MaterialPageRoute( builder: (context) => YourWidget(), settings: RouteSettings(name: " payment") ) 时需要传递 'settings' 属性 (6认同)
  • @OliverDixon 这不会弹出路由,因为函数返回 true。 (3认同)

Par*_*rSa 23

如果您使用 go_router 包,您可以使用以下代码:

GoRouter.of(context).location
Run Code Online (Sandbox Code Playgroud)

go_router 10.0.0 (2023) 更新:

GoRouterState.of(context).uri.toString();
Run Code Online (Sandbox Code Playgroud)


ikb*_*ben 13

这应该给您确切的路线名称

import 'package:path/path.dart';

ModalRoute.of(context).settings.name
Run Code Online (Sandbox Code Playgroud)

  • 它不会返回导航器上的当前路线 (17认同)
  • 仅适用于非匿名路由。 (2认同)

Col*_*son 11

NavigatorState不会公开用于获取当前路线路径的API,Route也不会公开用于确定路线路径的API。路由可以是(通常是匿名的)。Route使用该isCurrent方法,您现在可以确定给定对象是否位于导航器堆栈的顶部,但这对于您的用例而言并不十分方便。

我建议您针对此问题采用其他方法,并且不要完全退回到根本。相反,请Navigator对的每个窗格使用不同的小部件BottomNavigationBar。这样,您在窗格之间切换时就不必倒回堆栈。你可以用你的Navigator小部件Opacity和IgnorePointer部件来隐藏他们时,他们不应该是不破坏他们的筹码可见。

应用程式影片

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new MyHomePage(),
    );
  }
}

class SecurePage extends StatelessWidget {
  final int index;

  SecurePage(this.index);

  Widget build(BuildContext context) {
    return new Material(
      color: Colors.amber,
      child: new InkWell(
        child: new Center(
          child: new Icon(
            Icons.security,
            color: Colors.white,
            size: index * 100.0 + 20.0,
          ),
        ),
        onTap: () {
          Navigator.of(context).push(
            new MaterialPageRoute(
              builder: (BuildContext context) {
                return new SecurePage(index + 1);
              },
            ),
          );
        },
      ),
    );
  }
}

class VerifiedPage extends StatelessWidget {
  final int index;

  VerifiedPage(this.index);

  Widget build(BuildContext context) {
    return new Material(
      color: Colors.green,
      child: new InkWell(
        child: new Center(
          child: new Icon(
            Icons.verified_user,
            color: Colors.white,
            size: index * 100.0 + 20.0,
          ),
        ),
        onTap: () {
          Navigator.of(context).push(
            new MaterialPageRoute(
              builder: (BuildContext context) {
                return new VerifiedPage(index + 1);
              },
            ),
          );
        },
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State createState() => new MyHomePageState();
}

class MyHomePageState extends State<MyHomePage> {
  int _page = 0;
  List<Widget> initialWidgets = <Widget>[
    new SecurePage(0),
    new VerifiedPage(0),
  ];

  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Stack(
        children: new List<Widget>.generate(initialWidgets.length, (int index) {
          return new IgnorePointer(
            ignoring: index != _page,
            child: new Opacity(
              opacity: _page == index ? 1.0 : 0.0,
              child: new Navigator(
                onGenerateRoute: (RouteSettings settings) {
                  return new MaterialPageRoute(
                    builder: (_) => initialWidgets[index],
                  );
                },
              ),
            ),
          );
        }),
      ),
      bottomNavigationBar: new BottomNavigationBar(
        currentIndex: _page,
        onTap: (int index) {
          setState(() {
            _page = index;
          });
        },
        items: <BottomNavigationBarItem>[
          new BottomNavigationBarItem(
            icon: new Icon(Icons.security),
            title: new Text('Secure'),
          ),
          new BottomNavigationBarItem(
            icon: new Icon(Icons.verified_user),
            title: new Text('Verified'),
          ),
        ],
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Lev*_*hes 11

发布此答案主要是为了存档目的,但正如@ikben 所提到的,获取当前路线及其所有属性的一种方法是ModalRoute.of(context). 这将返回 a ModalRoute,尽管它的名称适用于大多数Navigator.push调用,而不仅仅是showDialog. 有用的属性包括Route.settings,Route.navigator,和Route.isFirst。


Dom*_*ico 6

如果您使用的go_router软件包版本 >= 9.0.0:

GoRouter.of(context).routeInformationProvider.value.location
Run Code Online (Sandbox Code Playgroud)

或者

GoRouterState.of(context).location
Run Code Online (Sandbox Code Playgroud)