Flutter如何实现BottomAppBar与BottomNavigationBar同时持久显示
解决方案
直接将BottomNavigationBar和BottomAppBar用Column组件垂直拼接后,整体赋值给Scaffold的bottomNavigationBar属性即可,Scaffold的该字段支持传入任意Widget,不限制只能是单个导航栏组件,改造后两个底部组件都会全局持久显示,不会随PageView页面切换消失。
具体修改步骤:
- 将原来写在PageView子页面中的BottomNavigationBar移到外层,和BottomAppBar共同包裹在Column内
- 删除PageView各子页面中重复的BottomNavigationBar代码
- 可根据需求调整两个组件的间距、分割线、阴影样式
修改后核心代码示例:
import 'package:flutter/material.dart'; //screens import 'package:timewise_flutter/screens/calendar_screen.dart'; import 'package:timewise_flutter/screens/covey_quadrants_screen.dart'; import 'package:timewise_flutter/screens/kanban_screen.dart'; import 'package:timewise_flutter/screens/todo_list_screen.dart'; class OverviewScreen extends StatefulWidget { static const String id = 'overview_screen'; @override _OverviewScreenState createState() => _OverviewScreenState(); } class _OverviewScreenState extends State<OverviewScreen> { PageController _pageController = PageController(initialPage: 2); int _bottomNavBarCurrentIndex = 2; @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, // 两个底部组件拼接到一起赋值给bottomNavigationBar bottomNavigationBar: SafeArea( child: Column( mainAxisSize: MainAxisSize.min, // 高度自适应内容 children: [ // 上层的页面切换BottomNavigationBar BottomNavigationBar( currentIndex: _bottomNavBarCurrentIndex, type: BottomNavigationBarType.fixed, backgroundColor: Colors.white, elevation: 0.0, iconSize: 16.0, selectedItemColor: Colors.black, unselectedItemColor: Colors.grey, showSelectedLabels: false, showUnselectedLabels: false, items: const [ BottomNavigationBarItem( icon: Icon(Icons.calendar_today_rounded), label: 'Calendar', tooltip: 'Calendar', ), BottomNavigationBarItem( icon: Icon(Icons.checklist_rounded), label: 'To-Do', tooltip: 'To-Do List', ), BottomNavigationBarItem( icon: Icon(Icons.panorama_fish_eye_rounded), label: 'Overview', tooltip: 'Overview', ), BottomNavigationBarItem( icon: Icon(Icons.border_all_rounded), label: 'Covey\'s 4 Quadrants', tooltip: 'Covey\'s 4 Quadrants', ), BottomNavigationBarItem( icon: Icon(Icons.view_column_rounded), label: 'Kanban Board', tooltip: 'Kanban Board', ), ], onTap: (index) { setState(() { _bottomNavBarCurrentIndex = index; _pageController.jumpToPage(index); }); }, ), // 可按需添加分割线 const Divider(height: 1, thickness: 1, color: Color(0xFFEEEEEE)), // 原来的底部操作BottomAppBar BottomAppBar( elevation: 16.0, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ IconButton( tooltip: 'Menu', icon: Icon(Icons.menu_rounded), onPressed: () { print('menu icon pressed!'); //TODO: show bottom modal bottom sheet }, ), IconButton( tooltip: 'Pomodoro Timer', icon: Icon(Icons.hourglass_empty_rounded), onPressed: () { print('pomo icon pressed!'); //TODO: show pomodoro timer modal bottom sheet }, ), IconButton( tooltip: 'Add', icon: Icon(Icons.add_circle_outline_outlined), onPressed: () { print('add icon pressed!'); //TODO: show add task modal bottom sheet }, ), ], ), ), ], ), ), body: PageView( controller: _pageController, onPageChanged: (page) { setState(() { _bottomNavBarCurrentIndex = page; }); }, children: [ CalendarScreen(), ToDoListScreen(), // 移除原来Overview页面里的BottomNavigationBar const SafeArea( child: Center( child: Text('Overview Screen'), ), ), CoveyQuadrantsScreen(), KanbanScreen(), ], ), ); } }
补充说明
- Column设置
mainAxisSize: MainAxisSize.min可以避免底部栏占用不必要的高度 - 两个组件的样式可按需调整,比如不需要分割线直接删除对应代码即可
- 所有页面切换、底部按钮点击的原有逻辑都可以完整保留,不需要额外修改状态管理逻辑
内容的提问来源于stack exchange,提问作者theowl-0011
相关产品推荐
相关产品推荐

