Flutter中persistent_bottom_nav_bar从AlertDialog跳转后导航栏消失问题
问题根因:你调用跳转方法时用的是AlertDialog内部的context,这个context不属于底部导航栏的导航栈上下文,导致新页面脱离了底部导航的管理,所以导航栏会消失。
两种可行解决办法:
- 用全局Key绑定底部导航栏,拿它的context跳转
先定义全局的导航Key:
初始化PersistentTabView的时候绑定这个Key:final GlobalKey<PersistentTabViewState> _bottomNavKey = GlobalKey<PersistentTabViewState>();
然后在AlertDialog的点击事件里用这个Key的context来跳转:PersistentTabView( context, key: _bottomNavKey, screens: 你的页面列表, items: 底部导航按钮配置, // 其他配置... )PersistentNavBarNavigator.pushNewScreen( _bottomNavKey.currentContext!, screen: DoseForm(medName: medName), withNavBar: true, pageTransitionAnimation: PageTransitionAnimation.cupertino, ); - 确保跳转用的是底部导航页面的context
打开AlertDialog时,传入当前底部导航页面的context(不是Dialog内部的context),比如在页面里直接调用showDialog时传页面自身的context,然后在Dialog的按钮点击事件里用这个context执行跳转。
- 用全局Key绑定底部导航栏,拿它的context跳转
额外注意:
- 检查新页面是否手动写过隐藏导航栏的代码
withNavBar虽默认值为true,但显式声明能避免潜在的配置冲突
内容的提问来源于stack exchange,提问作者Reema AlDumikhi
相关产品推荐
相关产品推荐

