Flutter布局基础——BottomNavigationBar

背景

FlutterBottomNavigationBar类似于 iOS 中的UITabbarController,是导航控制器的一种,常用于首页 Tab 切换。
BottomNavigationBar的 type 属性,默认值会根据items的个数而定;当items个数小于4个时,默认值为BottomNavigationBarType.fixed;当items个数大于等于4个,默认值为BottomNavigationBarType.shifting

两种效果对比如下:左侧为BottomNavigationBarType.fixed,右侧为BottomNavigationBarType.shifting
Flutter布局基础——BottomNavigationBar - 图1Flutter布局基础——BottomNavigationBar - 图2常用属性如下:

  • backgroundColor: 背景色
  • currentIndex: 当前选中哪一个
  • fixedColor: 选中 Item 的颜色
  • iconSize: 图片大小
  • items: 子元素
  • onTap: 点击事件
  • selectedFontSize: 选中字体大小
  • selectedItemColor: 选中 Icon 颜色
  • selectedLabelStyle: 选中文字的 style
  • type: icon 和文字的样式
  • unselectedItemColor: 未选中 Icon 的颜色
  • unselectedLabelStyle: 未选中文字样式

代码如下:

  1. void main() => runApp(new MyApp());
  2. class MyApp extends StatelessWidget {
  3. const MyApp({Key? key}) : super(key: key);
  4. static const String _title = 'Flutter Code Sample';
  5. @override
  6. Widget build(BuildContext context) {
  7. return const MaterialApp(
  8. title: _title,
  9. home: MyStatefulWidget(),
  10. );
  11. }
  12. }
  13. class MyStatefulWidget extends StatefulWidget {
  14. const MyStatefulWidget({Key? key}) : super(key: key);
  15. @override
  16. State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
  17. }
  18. class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  19. int _selectedIndex = 0;
  20. static const TextStyle optionStyle =
  21. TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
  22. static const List<Widget> _widgetOptions = <Widget>[
  23. Text(
  24. 'Index 0: Home',
  25. style: optionStyle,
  26. ),
  27. Text(
  28. 'Index 1: Business',
  29. style: optionStyle,
  30. ),
  31. Text(
  32. 'Index 2: School',
  33. style: optionStyle,
  34. )
  35. ];
  36. void _onItemTapped(int index) {
  37. setState(() {
  38. _selectedIndex = index;
  39. });
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return Scaffold(
  44. appBar: AppBar(
  45. title: const Text('BottomNavigationBar Sample'),
  46. ),
  47. body: Center(
  48. child: _widgetOptions.elementAt(_selectedIndex),
  49. ),
  50. bottomNavigationBar: BottomNavigationBar(
  51. items: const <BottomNavigationBarItem>[
  52. BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Home'),
  53. BottomNavigationBarItem(
  54. icon: Icon(Icons.business), label: 'Business'),
  55. BottomNavigationBarItem(icon: Icon(Icons.school), label: 'School'),
  56. ],
  57. currentIndex: _selectedIndex,
  58. selectedItemColor: Colors.amber[800],
  59. onTap: _onItemTapped,
  60. ),
  61. );
  62. }
  63. }

也可以如下使用,

main.dart 中,声明home 为 BottomNavigationWidget

  1. void main() => runApp(new MyApp());
  2. class MyApp extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return MaterialApp(
  6. title: 'Flutter bottomNavigationBar',
  7. theme: ThemeData.light(),
  8. home: BottomNavigationWidget(),
  9. );
  10. }
  11. }

然后创建 bottom_navigation_widget.dart,内容如下:

  1. import 'dart:ffi';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_application_1/pages/airplay_screen.dart';
  4. import 'package:flutter_application_1/pages/email_screen.dart';
  5. import 'package:flutter_application_1/pages/home_screen.dart';
  6. import 'package:flutter_application_1/pages/pages_screen.dart';
  7. class BottomNavigationWidget extends StatefulWidget {
  8. _BottomNavigationWidgetState createState() => _BottomNavigationWidgetState();
  9. }
  10. class _BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  11. final _BottomNavigationColor = Colors.blue;
  12. int _currentIndex = 0;
  13. List<Widget> list = [];
  14. @override
  15. void initState() {
  16. list
  17. ..add(HomeScreen())
  18. ..add(EmailScreen())
  19. ..add(PageScreen())
  20. ..add(AirplayScreen());
  21. super.initState();
  22. }
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. body: list[_currentIndex],
  27. bottomNavigationBar: BottomNavigationBar(
  28. items: [
  29. BottomNavigationBarItem(
  30. icon: Icon(
  31. Icons.home,
  32. color: _BottomNavigationColor,
  33. ),
  34. label: 'Home',
  35. ),
  36. BottomNavigationBarItem(
  37. icon: Icon(
  38. Icons.email,
  39. color: _BottomNavigationColor,
  40. ),
  41. label: 'Email',
  42. ),
  43. BottomNavigationBarItem(
  44. icon: Icon(
  45. Icons.pages,
  46. color: _BottomNavigationColor,
  47. ),
  48. label: 'Pages',
  49. ),
  50. BottomNavigationBarItem(
  51. icon: Icon(
  52. Icons.airplay,
  53. color: _BottomNavigationColor,
  54. ),
  55. label: 'AirPlay',
  56. ),
  57. ],
  58. currentIndex: _currentIndex,
  59. // type: BottomNavigationBarType.fixed,
  60. onTap: (int index) {
  61. setState(() {
  62. _currentIndex = index;
  63. });
  64. },
  65. ),
  66. );
  67. }
  68. }

然后创建 pages 目录,pages 目录中创建home_screen.dartairplay_screen.dartemail_screen.dartpages_screen.dart
代码如下:

  1. /// home_screen.dart
  2. import 'package:flutter/material.dart';
  3. class HomeScreen extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return Scaffold(
  7. appBar: AppBar(
  8. title: Text('Home'),
  9. ),
  10. body: Center(
  11. child: Text('Home'),
  12. ),
  13. );
  14. }
  15. }
  16. /// airplay_screen.dart
  17. import 'package:flutter/material.dart';
  18. class AirplayScreen extends StatelessWidget {
  19. @override
  20. Widget build(BuildContext context) {
  21. return Scaffold(
  22. appBar: AppBar(
  23. title: Text('Airplay'),
  24. ),
  25. body: Center(
  26. child: Text('Airplay'),
  27. ),
  28. );
  29. }
  30. }
  31. /// email_screen.dart
  32. import 'package:flutter/material.dart';
  33. class EmailScreen extends StatelessWidget {
  34. @override
  35. Widget build(BuildContext context) {
  36. return Scaffold(
  37. appBar: AppBar(
  38. title: Text('Email'),
  39. ),
  40. body: Center(
  41. child: Text('Email'),
  42. ),
  43. );
  44. }
  45. }
  46. /// pages_screen.dart
  47. import 'package:flutter/material.dart';
  48. class PageScreen extends StatelessWidget {
  49. @override
  50. Widget build(BuildContext context) {
  51. return Scaffold(
  52. appBar: AppBar(
  53. title: Text('Pages'),
  54. ),
  55. body: Center(
  56. child: Text('Pages'),
  57. ),
  58. );
  59. }
  60. }

这样使用,把首页 BottomNavigationWidget 拆出来到单独的类,再把 items 中每个类单独拆出来,方便修改使用。

参考

BottomNavigationBar Dev Doc
20个Flutter实例视频教程 让你轻松上手工作