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

Flutter中如何在SearchOnScreen页面隐藏BottomNavigationBar?

解决Flutter中指定Tab页面隐藏BottomNavigationBar的问题

你只需要根据当前选中的Tab索引,动态控制bottomNavigationBar的显示即可。具体修改如下:

在build方法里,将bottomNavigationBar的赋值改为条件判断:当索引为1(对应SearchOnScreen页面)时,返回null隐藏底部导航栏,其他情况返回原来的导航栏组件。

修改后的完整build方法代码:

@override
Widget build(BuildContext context) {
  return DefaultLayout(
    child: TabBarView(
      physics: NeverScrollableScrollPhysics(),
      controller: controller,
      children: [
        SearchOffScreen(),
        SearchOnScreen(),
        Center(child: Container(child: Text('My page'),)),
      ],
    ),
    // 核心修改部分:根据当前索引控制底部导航栏显示
    bottomNavigationBar: index == 1 
        ? null // 隐藏底部导航栏
        : Theme(
            data: ThemeData(
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
            ),
            child: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              onTap: (int index){
                controller.animateTo(index);
                this.index = index;
              },
              currentIndex: index,
              items: [
                BottomNavigationBarItem(
                    icon: Icon(Icons.home_filled),
                    label: 'Home'),
                BottomNavigationBarItem(
                    icon: Icon(Icons.search_rounded),
                    label: 'Search'),
                BottomNavigationBarItem(
                    icon: Icon(Icons.person),
                    label: 'My page'),
              ],
            ),
          ),
  );
}

逻辑说明

  • 你的index变量已经和当前选中的Tab索引同步,利用这个变量做条件判断就能精准控制导航栏的显示状态。
  • 当index == 1时,给bottomNavigationBar传入null,DefaultLayout就不会渲染底部导航栏;其他索引则正常渲染原来的BottomNavigationBar组件。

内容的提问来源于stack exchange,提问作者Wowmoment

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 23:25:33