Sta*_*sVo 39
如果您想通过使用导航键获取当前路线,可以使用 popUntil 方法:
String? currentPath;
navigatorKey.currentState?.popUntil((route) {
currentPath = route.settings.name;
return true;
});
Run Code Online (Sandbox Code Playgroud)
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)
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。
如果您使用的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)
| 归档时间: |
|
| 查看次数: |
12247 次 |
| 最近记录: |