gpt4 book ai didi

Flutter:如何避免在现有路由上推送相同的路由

转载 作者:IT老高 更新时间:2023-10-28 12:40:57 26 4
gpt4 key购买 nike

我有底部导航栏的 View ,当您推送导航栏项目时,会推送一条新路线。

 final navigatorKey = GlobalKey<NavigatorState>();

@override
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
key: _scaffoldKey,
body: _buildBody(context),
bottomNavigationBar: _buildBottomNavigationBar(context),
),
);

void routeToView(routeIndex) {
navigatorKey.currentState.pushNamed(pagesRouteFactories.keys.toList()[routeIndex]);
}

我想防止在当前 View 上推送相同的路线。我想将当前路线与我们尝试推送的新路线进行比较,如果相同则忽略推送路线。

如果我们尝试推送的路线相同,我想将 View 滚动到顶部

任何想法。

最佳答案

NavigatorState doesn't expose an API for getting the path of thecurrent route, and Route doesn't expose an API for determining aroute's path either. Routes can be (and often are) anonymous. You canfind out if a given Route is on the top of the navigator stack rightnow using the isCurrent method, but this isn't very convenient foryour use case.

https://stackoverflow.com/a/46498543/2554745

这是我能想到的最接近的解决方案:

Navigator.of(context).pushNamedAndRemoveUntil(
"newRouteName",
(route) => route.isCurrent && route.settings.name == "newRouteName"
? false
: true);

如果路由名称为“newRouteName”,则弹出当前路由,然后推送新的,否则不会弹出任何内容。

编辑:最新的flutter提供了ModalRoute.of(context).settings.name来获取当前路由的名称。

关于Flutter:如何避免在现有路由上推送相同的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54410461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com