• 【Flutter】底部导航BottomNavigationBar的使用


    常用基本属性

    属性名含义是否必须
    items底部导航栏的子项List
    currentIndex当前显示索引
    onTap底部导航栏的点击事件, Function(int)
    type底部导航栏类型,定义 [BottomNavigationBar] 的布局和行为
    selectedItemColor选中项图标和label的颜色
    unselectedItemColor未选中项图标和label的颜色
    iconSize图标大小
    backgroundColor底部导航栏背景色
    showSelectedLabels是否显示选中项的label
    showUnselectedLabels是否显示未选中项的label
    selectedIconTheme选中项 图标的主题 设置
    unselectedIconTheme选中项 图标的主题 设置
    selectedFontSize选中项 文字大小 设置
    unselectedFontSize未选中项 文字大小 设置
    selectedLabelStyle选中项 文字样式 设置
    unselectedLabelStyle未选中项 文字样式 设置
    mouseCursor当鼠标指针进入或悬停在屏幕上时的光标
    enableFeedback检测到的手势是否应提供声音和/或触觉反馈

    示例

    效果一

    当选中时图标文字变色,未选中时不显示文字
    在这里插入图片描述

    	bottomNavigationBar: BottomNavigationBar(
            selectedItemColor: Colors.red, // 选中时
            unselectedItemColor: Colors.black, // 未选中
            currentIndex: _currentIndex,
            onTap: (value) {
              setState(() {
                _currentIndex = value;
              });
            },
            items: const [
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                label: 'Home',
              ),
              // ...
            ],
          ),
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    效果二

    显示图标和文字,选中变色
    在这里插入图片描述

    可设置type: BottomNavigationBarType.fixed固定或设置showUnselectedLabels: true

    	bottomNavigationBar: BottomNavigationBar(
            selectedItemColor: Colors.red, // 选中时
            unselectedItemColor: Colors.black, // 未选中
            type: BottomNavigationBarType.fixed,  // 固定
            currentIndex: _currentIndex,
            onTap: (value) {
              setState(() {
                _currentIndex = value;
              });
            },
            items: const [
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                label: 'Home',
              ),
              // ...
            ],
          ),
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    效果三

    显示图标和文字,设置背景
    在这里插入图片描述
    type: BottomNavigationBarType.fixed必须与backgroundColor配合使用,背景才生效

    	bottomNavigationBar: BottomNavigationBar(
            selectedItemColor: Colors.red, // 选中时
            unselectedItemColor: Colors.black, // 未选中
            type: BottomNavigationBarType.fixed,  // 固定
            backgroundColor: Colors.amber,  // 背景色
            currentIndex: _currentIndex,
            onTap: (value) {
              setState(() {
                _currentIndex = value;
              });
            },
            items: const [
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                label: 'Home',
              ),
              // ...
            ],
          ),
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    完整示例

    class PageWidget extends StatefulWidget {
      const PageWidget({super.key});
    
      
      State<PageWidget> createState() => _PageWidgetState();
    }
    
    class _PageWidgetState extends State<PageWidget> {
      int _currentIndex = 0;
    
      Widget _getPage(index) {
        final List<Widget> _pages = <Widget>[
          Container(
              color: Colors.red,
              child: ElevatedButton(
                onPressed: () {
                  Navigator.pushNamed(context, '/user-page');
                },
                child: const Text('User Page'),
              )),
          Container(
            color: Colors.green,
          ),
          Container(
            color: Colors.blue,
          ),
          Container(
            color: Colors.yellow,
          ),
        ];
        return _pages[index];
      }
    
      
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Page Widget'),
          ),
          bottomNavigationBar: BottomNavigationBar(
            backgroundColor: Colors.amber,
            type: BottomNavigationBarType.fixed,
            // showSelectedLabels: true,
            // showUnselectedLabels: true,
            selectedItemColor: Colors.red,
            unselectedItemColor: Colors.black,
            currentIndex: _currentIndex,
            onTap: (value) {
              setState(() {
                _currentIndex = value;
              });
            },
            items: const [
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                label: 'Home',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.business),
                label: 'Business',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.school),
                label: 'School',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.account_circle),
                label: 'User',
              ),
            ],
          ),
          body: _getPage(_currentIndex),
        );
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
  • 相关阅读:
    Vue3 - 异步组件(写法及详细示例)
    黄州科目三
    Django中如何让DRF的接口针对前后台返回不同的字段
    webpack定制化 代码规范[eslint]
    [扩展欧几里得]Draw a triangle 2022年桂林站E
    MySQL的时区引起的前后端数据交互不畅的问题解决
    在java代码中判断当前国家码&&遍历Switchpreferencer开关进行展示
    [电源选项]没有系统散热方式,没有被动散热选项
    基于51单片机霍尔传感器测速(仿真+源程序)
    python3-- Pillow10 ‘FreeTypeFont‘ object has no attribute ‘getsize‘报错解决
  • 原文地址:https://blog.csdn.net/weixin_52418790/article/details/136186350