
在很多情况下,使用GlobalKey并不是一个好主意,但这可能就是其中之一。
当您构建自己的
MaterialApp(我假设您正在使用)时,可以传入一个
navigatorKey参数,该参数指定用于导航器的键。然后,您可以使用此键访问导航器的状态。看起来像这样:
class _AppState extends State<App> { final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "Main Navigator"); @override Widget build(BuildContext context) { return new MaterialApp( navigatorKey: navigatorKey, home: new Scaffold( endDrawer: Drawer(), appBar: AppBar(), body: new Container(), ), ); }}然后要使用它,请访问navigatorKey.currentContext:
_goToDeeplyNestedView() { navigatorKey.currentState.push( MaterialPageRoute(builder: (_) => DeeplyNestedView()) );}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)