BottomNavigationBar 和 BottomNavigationBarItem配合Scaffold控件使用可以实现底部导航效果,类似于微信底部的导航效果,
构造函数: BottomNavigationBar({
Key key,
@required this.items,
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,
})
下面是一个简单的底部导航案例:
import 'package:flutter/material.dart';
import 'package:fluttertrip/pages/home_page.dart';
import 'package:fluttertrip/pages/my_page.dart';
import 'package:fluttertrip/pages/search_page.dart';
import 'package:fluttertrip/pages/travel_page.dart';
class TabNavigator extends StatefulWidget {
@override
_TabNavigatorState createState() => _TabNavigatorState();
}
class _TabNavigatorState extends State {
final _defaultColor = Colors.grey;
final _activeColor = Colors.blue;
int _currentIndex = 0;
final PageController _controller = PageController(
initialPage: 0,
);
@override
Widget build(BuildContext context) {
return Scaffold(
body: PageView(
controller: _controller,
children: [
HomePage(),
SearchPage(true, '', '长城d', '请输入搜索关键词'),
TravelPage(),
MyPage(),
],
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _currentIndex,
onTap: (index) {
_controller.jumpToPage(index);
setState(() {
_currentIndex = index;
});
},
type: BottomNavigationBarType.fixed,
items: [
_bottomItem('首页', Icons.home, 0),
_bottomItem('搜索', Icons.search, 1),
_bottomItem('旅拍', Icons.camera_alt, 2),
_bottomItem('我的', Icons.account_circle, 3),
]),
);
}
_bottomItem(String title, IconData icon, int index) {
return BottomNavigationBarItem(
icon: Icon(
icon,
color: _defaultColor,
),
activeIcon: Icon(
icon,
color: _activeColor,
),
title: Text(
title,
style: TextStyle(
color: _currentIndex != index ? _defaultColor : _activeColor),
));
}
}
效果:
BottomNavigationBar有2种显示模式,其中一种是fixed效果,另一种是shifting效果。



