当前位置: 首页 > 知识库问答 >
问题:

如何在Flutter中获取当前路径路径?

明安阳
2023-03-14

在实现持久底部栏时,当单击底部栏中的按钮时,需要恢复以前的路由。

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

从概念上讲,用户会认为每个按钮都是一个工作区,其状态永远不会丢失(包括后退堆栈)。用户可以安全地从一个工作区切换到另一个工作区。

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

共有3个答案

荀嘉熙
2023-03-14

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

String? currentPath;
navigatorKey.currentState?.popUntil((route) {
  currentPath = route.settings.name;
  return true;
}); 
劳和雅
2023-03-14

< code>NavigatorState不公开用于获取当前路线路径的API,并且< code>Route也不公开用于确定路线路径的API。路由可以是(并且经常是)匿名的。您可以使用< code>isCurrent方法来确定给定的< code>Route现在是否在导航器堆栈的顶部,但是这对于您的用例来说不是很方便。

我建议您对此问题采取不同的方法,并且根本不要倒带到根目录。相反,为BottomNavigationBar的每个窗格使用不同的Navigator小部件。这样,在窗格之间切换时就不必倒带堆栈。您可以将Navigator小部件包装在不透明度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'),
          ),
        ],
      ),
    );
  }
}
公风史
2023-03-14

这应该会给你准确的路线名称

ModalRoute.of(context).settings.name

如果你使用Navigator.popuntil.。看看Rémi Rousselet的另一个答案https://stackoverflow.com/a/50817399/5921185

 类似资料:
  • 问题内容: 在实现持久性底部栏时,单击底部栏中的按钮时,需要恢复以前的路线。 单击底部栏中的按钮时,将保存其当前路径路径(/ a / b / c ),并根据该按钮单击恢复先前保存的路径。 从概念上讲,用户会将每个按钮视为一个工作区,并且其状态永远不会丢失(包括后退堆栈)。用户可以安全地从一个工作区切换到另一个工作区。 当路由重绕到根时,如何在Flutter中获取当前路由路径? 问题答案: 不会公开

  • 我需要得到当前路径的url在路由更改和基于值我想做条件渲染 这是我的代码,我没有继续的想法。 所以我需要做的是: 对于每个更改,我需要更新的值,并进行条件渲染,并将该值(路径)作为

  • 问题内容: 我在代码中的某个地方遇到了异常,但是可以在应用程序启动时安全地导入相同的模块。我很好奇,看看Python寻找要导入模块的路径,以便我能够追踪为什么会出现此问题。我找到了这个: 这是系统在尝试导入模块时查找的所有路径的列表吗? 问题答案: python默认检查的路径位置可以通过check检查。

  • 如何使用react router v4获取当前路径? 我尝试了以下方法,但没有成功: 错误: 这是我的Routes.js文件:

  • 在此URL中: 我想抓,当然是在路由里面。这工作很棒: 但是我想访问任何路由之外的路径以及文件如下:

  • 问题内容: 我需要从当前请求的URL获取路径。例如,如果当前URL是: 我想要这个: 问题答案: 你要。从文档: 为了访问该页面而给出的URI;例如,。