Flutter--BottomNavigationBar组件

一、简介

BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar 是 Scaffold 组件的参数。

二、BottomNavigationBar 源码

BottomNavigationBar({
    Key key,
    @required this.items,//底部导航条按钮集合
    this.onTap,//点击选中的回调
    this.currentIndex = 0,//选中第几个题目的索引值
    this.elevation,//阴影
    this.type,//风格
    Color fixedColor,//选中的颜色
    this.backgroundColor,//背景色
    this.iconSize = 24.0,//图标的大小
    Color selectedItemColor,//选中条目的颜色
    this.unselectedItemColor,//未选中条目的颜色
    this.selectedIconTheme,//选中图标的样式
    this.unselectedIconTheme,//未选中图标的样式
    this.selectedFontSize = 14.0,//选中文字的大小
    this.unselectedFontSize = 12.0,//未选中文字的大小
    this.selectedLabelStyle,//选中文字的样式
    this.unselectedLabelStyle,//未选中文字的样式
    this.showSelectedLabels = true,//是否显示选中的文字
    this.showUnselectedLabels,//是否显示未选中的文字
    this.mouseCursor,//
  }) : assert(items != null),
       assert(items.length >= 2),
       assert(
        items.every((BottomNavigationBarItem item) => item.title != null) ||
        items.every((BottomNavigationBarItem item) => item.label != null),
        'Every item must have a non-null title or label',
       ),
       assert(0 <= currentIndex && currentIndex < items.length),
       assert(elevation == null || elevation >= 0.0),
       assert(iconSize != null && iconSize >= 0.0),
       assert(
         selectedItemColor == null || fixedColor == null,
         'Either selectedItemColor or fixedColor can be specified, but not both'
       ),
       assert(selectedFontSize != null && selectedFontSize >= 0.0),
       assert(unselectedFontSize != null && unselectedFontSize >= 0.0),
       assert(showSelectedLabels != null),
       selectedItemColor = selectedItemColor ?? fixedColor,
       super(key: key);

三、BottomNavigationBar 属性介绍

属性 说明
item List<BottomNavigationBarItem>[]底部导航条目按钮(BottomNavigationBarItem源码和属性见下方)
onTap 点击选中的回调
currentIndex 选中第几个索引值
elevation 阴影
type 样式
BottomNavigationBarType.fixed
BottomNavigationBarType.shifting
fixedColor 选中时颜色(不能和selectedItemColor共存)
backgroundColor 背景色(BottomNavigationBarType.fixed样式有效)
iconSize 图标的大小
selectedItemColor 选中条目的颜色(不能和fixedColor共存)
unselectedItemColor 未选中条目的颜色
selectedIconTheme IconThemeData()选中图标的样式(IconThemeData源码和属性见下方)
IconThemeData设置图标的颜色会覆盖fixedColor或selectedItemColor设置的图标颜色
IconThemeData设置图标的大小会覆盖iconSize 设置的图标大小
unselectedIconTheme IconThemeData()未选中图标的样式,
IconThemeData设置图标的颜色会覆盖unselectedItemColor设置的图标颜色
IconThemeData设置图标的大小会覆盖iconSize 设置的图标大小
selectedFontSize 选中文字的大小
unselectedFontSize 未选中文字的大小
selectedLabelStyle TextStyle()选中文字的样式,设置文字颜色无效,TextStyle()设置文字的大小会覆盖selectedFontSize设置的大小
unselectedLabelStyle TextStyle()未选中文字的样式,设置文字颜色无效,TextStyle()设置文字的大小会覆盖unselectedFontSize设置文字的大小
showSelectedLabels 是否显示选中条目的文字
showUnselectedLabels 是否显示未选中条目的文字

四、BottomNavigationBarItem源码

const BottomNavigationBarItem({
    @required this.icon,
    @Deprecated(
      'Use "label" instead, as it allows for an improved text-scaling experience. '
      'This feature was deprecated after v1.19.0.'
    )
    this.title,
    this.label,
    Widget activeIcon,
    this.backgroundColor,
  }) : activeIcon = activeIcon ?? icon,
       assert(label == null || title == null),
       assert(icon != null);

五、BottomNavigationBarItem属性介绍

|属性|说明|
|icon|图标|
|title|标题|
|label|标签|
|backgroundColor|背景色
|activeIcon|选中后的图标

七、IconThemeData源码

const IconThemeData({this.color, double opacity, this.size}) : _opacity = opacity;

七、IconThemeData属性介绍

属性 说明
color 图标的颜色
opacity 透明度
size 图标的大小

八、demo

void main() {
  runApp(TestFul());
}

class TestFul extends StatefulWidget {
  TestFul({Key key}) : super(key: key);

  _TestFulState createState() => _TestFulState();
}

class _TestFulState extends State<TestFul> {
  int clickIndex = 0;
  List _pageList = [
    Tab1(),
    Tab2(),
    Tab3(),
  ];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("BottomNavigationBar学习"),
        ),
        body: _pageList[clickIndex],
        bottomNavigationBar: BottomNavigationBar(
        //点击条目的监听回调
          onTap: (int index) {
            setState(() {
              clickIndex = index;
            });
          },
          currentIndex: clickIndex,
          type: BottomNavigationBarType.fixed,
          backgroundColor: Colors.greenAccent,
          iconSize: 20,
//          fixedColor: Colors.yellow,
          selectedItemColor: Colors.yellow,
          unselectedItemColor: Colors.black,
          selectedIconTheme: IconThemeData(
            color: Colors.red,
            opacity: 1,
            size: 30,
          ),
          unselectedIconTheme:
              IconThemeData(color: Colors.deepPurple, opacity: 0.8, size: 20),
          selectedFontSize: 2,
          unselectedFontSize: 2,
          selectedLabelStyle: TextStyle(
            color: Colors.purpleAccent,
            fontSize: 15,
          ),
          unselectedLabelStyle: TextStyle(
            color: Colors.purpleAccent,
            fontSize: 10,
          ),
          showSelectedLabels: true,
          showUnselectedLabels: false,

          items: [
            BottomNavigationBarItem(
                icon: Icon(Icons.search), title: Text("查找")),
            BottomNavigationBarItem(
                activeIcon: Icon(Icons.opacity),
                icon: Icon(Icons.home_rounded), title: Text("首页")),
            BottomNavigationBarItem(
                icon: Icon(Icons.add_circle_sharp), title: Text("添加")),
          ],
        ),
      ),
    );
  }
}

class Tab1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("我是查找");
  }
}

class Tab2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("我是首页");
  }
}

class Tab3 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text("我是添加");
  }
}
141402bc6e5504bbeb9f65310978970.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,475评论 4 372
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,744评论 2 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,101评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,732评论 0 221
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,141评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,049评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,188评论 2 320
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,965评论 0 213
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,716评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,867评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,341评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,663评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,376评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,200评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,990评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,179评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,979评论 2 279