文章目录
一、BottomNavigationBar 组件
二、BottomNavigationBarItem 组件
三、BottomNavigationBar 底部导航栏代码示例
四、BottomNavigationBar 底部导航栏选中状态切换代码示例
五、BottomNavigationBar 底部导航栏切换选项卡界面
六、 相关资源
一、BottomNavigationBar 组件
BottomNavigationBar 组件是底部导航栏 , 用于设置给 Scaffold 组件的 bottomNavigationBar 字段 ;
下面是 BottomNavigationBar 组件的构造函数源码 , 该构造函数的可选参数列表就是可以设置的字段属性 ;
class BottomNavigationBar extends StatefulWidget { /// Creates a bottom navigation bar which is typically used as a /// [Scaffold]'s [Scaffold.bottomNavigationBar] argument. /// /// The length of [items] must be at least two and each item's icon and title /// must not be null. /// /// If [type] is null then [BottomNavigationBarType.fixed] is used when there /// are two or three [items], [BottomNavigationBarType.shifting] otherwise. /// /// The [iconSize], [selectedFontSize], [unselectedFontSize], and [elevation] /// arguments must be non-null and non-negative. /// /// If [selectedLabelStyle.color] and [unselectedLabelStyle.color] values /// are non-null, they will be used instead of [selectedItemColor] and /// [unselectedItemColor]. /// /// If custom [IconThemData]s are used, you must provide both /// [selectedIconTheme] and [unselectedIconTheme], and both /// [IconThemeData.color] and [IconThemeData.size] must be set. /// /// If both [selectedLabelStyle.fontSize] and [selectedFontSize] are set, /// [selectedLabelStyle.fontSize] will be used. /// /// Only one of [selectedItemColor] and [fixedColor] can be specified. The /// former is preferred, [fixedColor] only exists for the sake of /// backwards compatibility. /// /// The [showSelectedLabels] argument must not be non-null. /// /// The [showUnselectedLabels] argument defaults to `true` if [type] is /// [BottomNavigationBarType.fixed] and `false` if [type] is /// [BottomNavigationBarType.shifting]. BottomNavigationBar({ Key key, @required this.items,// 当前的若干 BottomNavigationBarItem 组件 this.onTap, this.currentIndex = 0,// 当前选中条目 this.elevation = 8.0, BottomNavigationBarType type, Color fixedColor, this.backgroundColor, this.iconSize = 24.0, Color selectedItemColor, this.unselectedItemColor, this.selectedIconTheme = const IconThemeData(), this.unselectedIconTheme = const IconThemeData(), this.selectedFontSize = 14.0, this.unselectedFontSize = 12.0, this.selectedLabelStyle, this.unselectedLabelStyle, this.showSelectedLabels = true, bool showUnselectedLabels, }) }
二、BottomNavigationBarItem 组件
BottomNavigationBarItem 组件是 BottomNavigationBar 的 items 字段值 , 可以给该 items 字段设置多个 BottomNavigationBarItem 组件 ;
BottomNavigationBarItem 组件常用设置 :
默认状态图标 : icon ;
图标下显示的标题 : title ;
激活状态的图标 : activeIcon ;
背景颜色 : backgroundColor ;
BottomNavigationBarItem 组件构造函数源码 :
class BottomNavigationBarItem { /// Creates an item that is used with [BottomNavigationBar.items]. /// /// The argument [icon] should not be null and the argument [title] should not be null when used in a Material Design's [BottomNavigationBar]. const BottomNavigationBarItem({ @required this.icon, // 默认状态图标 this.title, // 图标下显示的标题 Widget activeIcon, // 激活状态的图标 this.backgroundColor, // 背景颜色 }) : activeIcon = activeIcon ?? icon, assert(icon != null); }
三、BottomNavigationBar 底部导航栏代码示例
代码示例 :
// 底部导航栏 BottomNavigationBar 设置
// 底部导航栏 BottomNavigationBar 设置 // items 可以设置多个 BottomNavigationBarItem bottomNavigationBar: BottomNavigationBar(items: [ // 设置底部导航栏条目, 每个条目可以设置一个图标 BottomNavigationBarItem( // 默认状态下的图标 icon: Icon(Icons.home, color: Colors.grey,), // 激活状态下的图标 activeIcon: Icon(Icons.home, color: Colors.red,), // 设置标题 title: Text("主页") ), // 设置底部导航栏条目, 每个条目可以设置一个图标 BottomNavigationBarItem( // 默认状态下的图标 icon: Icon(Icons.settings, color: Colors.grey,), // 激活状态下的图标 activeIcon: Icon(Icons.settings, color: Colors.red,), // 设置标题 title: Text("设置") ) ],),
完整代码示例 :
import 'package:flutter/material.dart'; class StatefulWidgetPage extends StatefulWidget { @override _StatefulWidgetPageState createState() => _StatefulWidgetPageState(); } class _StatefulWidgetPageState extends State<StatefulWidgetPage> { // This widget is the root of your application. @override Widget build(BuildContext context) { // 文本组件样式 , 可以设置给 Text 文本组件 // 设置字体大小 20, 颜色红色 TextStyle textStyle = TextStyle(fontSize: 20, color: Colors.red); return MaterialApp( title: 'StatefulWidgetPage 组件示例', theme: ThemeData( primarySwatch: Colors.blue, ), home: Scaffold( // 顶部标题栏 appBar: AppBar(title: Text('StatefulWidgetPage 组件示例'),), // 底部导航栏 BottomNavigationBar 设置 // items 可以设置多个 BottomNavigationBarItem bottomNavigationBar: BottomNavigationBar(items: [ // 设置底部导航栏条目, 每个条目可以设置一个图标 BottomNavigationBarItem( // 默认状态下的图标 icon: Icon(Icons.home, color: Colors.grey,), // 激活状态下的图标 activeIcon: Icon(Icons.home, color: Colors.red,), // 设置标题 title: Text("主页") ), // 设置底部导航栏条目, 每个条目可以设置一个图标 BottomNavigationBarItem( // 默认状态下的图标 icon: Icon(Icons.settings, color: Colors.grey,), // 激活状态下的图标 activeIcon: Icon(Icons.settings, color: Colors.red,), // 设置标题 title: Text("设置") ) ],), // Container 容器使用 body: Container( // 设置容器的装饰器 , BoxDecoration 是最常用的装饰器 // 可以自行查看 BoxDecoration 中可以设置的属性 decoration: BoxDecoration(color: Colors.white), // 设置 child 子组件居中方式, 居中放置 alignment: Alignment.center, // 子组件, 子组件设置为一个 Column 组件 child: Column( // Column 子组件, 这里设置 Text 文本组件 children: <Widget>[ ], ), ), ), ); } }
运行效果 :