You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter底部导航栏仅热重启后显示,pushAndRemoveUntil跳转后不显示的问题求助

Flutter底部导航栏仅热重启后显示,pushAndRemoveUntil跳转后不显示的问题求助

嘿,我仔细看了你的问题和代码,马上就找到问题根源了!你现在的问题是:用pushAndRemoveUntil跳转到HomeScreen2后,底部导航栏没显示,只有热重启才出来。这是因为你直接跳转了HomeScreen2页面,但底部导航栏是属于Navigation组件的——HomeScreen2只是Navigation管理的子页面之一,单独打开它自然不会带上底部导航。

之前热重启后能显示,是因为重启后App的初始页面是Navigation,所有页面都在这个带导航的容器里。现在给你具体的解决办法:

1. 修正跳转逻辑:目标页面改为Navigation

把原来跳HomeScreen2的代码,改成跳Navigation组件,这样进入的就是带底部导航的容器,同时Navigation默认选中的索引是2(对应HomeScreen2),刚好符合你的需求。

修改后的跳转代码片段:

Navigator.pushAndRemoveUntil(
  context,
  MaterialPageRoute(builder: (context) => Navigation()),
  (route) => false,
);

2. 优化Navigation的页面实例创建

你现在的getWidgetOptions()每次调用都会新建页面实例,这不仅可能导致状态丢失,还会浪费资源。可以把页面实例提前初始化,避免重复创建:

class _NavigationState extends State<Navigation> {
  int _selectedIndex = 2;
  // 提前初始化所有页面实例,不用每次build都新建
  final List<Widget> _widgetOptions = [
    Test(note: NoteModel()),
    Options(bookmarkedRecipes: [],),
    HomeScreen2(),
    Test(note: NoteModel()),
    Profile(users: UserModel()),
  ];

  void _onItemTap(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBody: true,
      backgroundColor: Colors.transparent,
      body: Stack(
        children: [_widgetOptions.elementAt(_selectedIndex)],
      ),
      // 下面的底部导航栏代码保持不变即可
      bottomNavigationBar: Container(
        margin: EdgeInsets.zero,
        decoration: const BoxDecoration(
          color: Color(0xFF162242),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(30.0),
            topRight: Radius.circular(30.0),
          ),
        ),
        child: Theme(
          data: ThemeData(
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
          ),
          child: BottomNavigationBar(
            // 导航栏内部代码不变...
          ),
        ),
      ),
    );
  }
}

额外提示

如果HomeScreen2需要接收参数(比如用户信息),可以给Navigation添加构造函数参数,再传递给HomeScreen2实例,这样就不会影响页面的数据传递啦。

按照上面的修改,你点击“Save Profile”按钮后,就能直接进入带底部导航的页面,并且默认显示HomeScreen2,再也不需要热重启了!

备注:内容来源于stack exchange,提问作者Leena Marie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 07:53:02