https://api.flutter.dev/flutter/material/BottomNavigationBar-class.html
BottomNavigationBar 是底部导航条,可以让我们定义底部Tab 切换,bottomNavigationBar
是Scaffold 组件的参数。
定义
BottomNavigationBar({Key key,@required this.items, //List<BottomNavigationBarItem> 底部导航条按钮集合this.onTap, //选中变化回调函数this.currentIndex = 0, //默认选中第几个this.elevation,this.type, //BottomNavigationBarType.fixed shiftingColor fixedColor, //选中的颜色this.backgroundColor, //导航条背景色this.iconSize = 24.0,Color selectedItemColor, //选中项的颜色this.unselectedItemColor,this.selectedIconTheme, //选中icon的样式 IconThemeData(color: Colors.blue, opacity: 0.3, size: 30),this.unselectedIconTheme,this.selectedFontSize = 14.0, //选中项文字大小this.unselectedFontSize = 12.0,this.selectedLabelStyle, //选中项文字样式this.unselectedLabelStyle,this.showSelectedLabels = true, //是否展示 选中项文字this.showUnselectedLabels, //是否展示 非选中项文字this.mouseCursor,})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,})
简单使用

import 'package:flutter/material.dart';class HomePage extends StatefulWidget {HomePage({key,this.index = '0',}) : super(key: key);final index;@override_HomePageState createState() => _HomePageState();}class _HomePageState extends State<HomePage> {int _currentIndex;List<Widget> pages = [Text('首页', textScaleFactor: 4),Text('教室', textScaleFactor: 4),Text('我的', textScaleFactor: 4),];@overridevoid initState() {super.initState();_currentIndex = int.parse(widget.index);}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('首页'),),body: pages[_currentIndex],bottomNavigationBar: BottomNavigationBar(type: BottomNavigationBarType.fixed, //fixed 固定效果(默认);shiftingcurrentIndex: _currentIndex,unselectedItemColor: Colors.black,selectedItemColor: Theme.of(context).primaryColor,onTap: (value) {setState(() {_currentIndex = value;});},items: [BottomNavigationBarItem(label: '首页', icon: Icon(Icons.home)),BottomNavigationBarItem(label: '教室', icon: Icon(Icons.class_)),BottomNavigationBarItem(label: '我的', icon: Icon(Icons.sanitizer)),],),);}}
type为 shifting 效果
BottomNavigationBar有2种显示模式,其中一种是fixed效果,上面的展示就是fixed效果,这也是默认值,另一种是shifting效果,设置shifting时需要设置selectedItemColor和unselectedItemColor,效果如下:
示例:结合PageView

import 'package:app1/routes/route_push.dart';import 'package:flutter/material.dart';import 'package:app1/routes/routes.dart';// import 'package:app1/routes/application.dart';class HomePage extends StatefulWidget {HomePage({key,this.index = '0',}) : super(key: key);final index;@override_HomePageState createState() => _HomePageState();}class _HomePageState extends State<HomePage> {PageController _pageController; //页面控制器int _currentIndex; //页面索引// 页面数组List<Widget> pages = [Text('首页', textScaleFactor: 4),Text('教室', textScaleFactor: 4),Text('我的', textScaleFactor: 4),];@overridevoid initState() {super.initState();_currentIndex = int.parse(widget.index);_pageController = PageController(initialPage: _currentIndex, //表示当前加载第几页,默认第一页。keepPage: true,viewportFraction: 1, //slider 左右缩小倍数);}@overrideWidget build(BuildContext context) {print('home => $_currentIndex');return Scaffold(appBar: AppBar(title: Text('首页'),),body: PageView(controller: _pageController,children: pages,// physics: NeverScrollableScrollPhysics(), // 禁止滚动onPageChanged: (value) {print('onPageChanged => $value');setState(() {_currentIndex = value;});},),bottomNavigationBar: BottomNavigationBar(currentIndex: _currentIndex,unselectedItemColor: Colors.black,selectedItemColor: Theme.of(context).primaryColor,onTap: (value) {print('onTap => $value');//_pageController.jumpToPage(index); //直接跳转_pageController.animateToPage(value,duration: Duration(milliseconds: 200),curve: Curves.linear,);},items: [BottomNavigationBarItem(label: '首页', icon: Icon(Icons.home)),BottomNavigationBarItem(label: '教室', icon: Icon(Icons.class_)),BottomNavigationBarItem(label: '我的', icon: Icon(Icons.sanitizer)),],),);}}
示例:结合floatingActionButton

import 'package:flutter/material.dart';class HomePage extends StatefulWidget {HomePage({key,this.index = '0',}) : super(key: key);final index;@override_HomePageState createState() => _HomePageState();}class _HomePageState extends State<HomePage> {int _currentIndex;List<Widget> pages = [Text('首页', textScaleFactor: 4),Text('教室', textScaleFactor: 4),Text('我的', textScaleFactor: 4),];@overridevoid initState() {super.initState();_currentIndex = int.parse(widget.index);}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('首页'),),body: pages[_currentIndex],bottomNavigationBar: BottomNavigationBar(currentIndex: _currentIndex,unselectedItemColor: Colors.black,selectedItemColor: Theme.of(context).primaryColor,onTap: (value) {setState(() {_currentIndex = value;});},items: [BottomNavigationBarItem(label: '首页', icon: Icon(Icons.home)),BottomNavigationBarItem(label: '教室', icon: Icon(Icons.class_)),BottomNavigationBarItem(label: '我的', icon: Icon(Icons.sanitizer)),],),floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,floatingActionButton: Container(height: 70,width: 70,padding: EdgeInsets.all(8),margin: EdgeInsets.only(top: 5),decoration: BoxDecoration(borderRadius: BorderRadius.circular(35),color: Colors.white,),child: FloatingActionButton(child: Icon(Icons.add, size: 36),onPressed: () {setState(() {this._currentIndex = 1;});},backgroundColor: this._currentIndex == 1 ? Colors.red : Colors.blue,foregroundColor: Colors.white,),),);}}
