1. import 'package:flutter/material.dart';
    2. import 'home.dart';
    3. import 'search.dart';
    4. import 'me.dart';
    5. import 'detail.dart';
    6. import 'news.dart';
    7. class MyBottomNavigationBar extends StatefulWidget {
    8. final int index;
    9. MyBottomNavigationBar({this.index = 0});
    10. @override
    11. _MyBottomNavigationBarState createState() =>
    12. _MyBottomNavigationBarState(this.index);
    13. }
    14. class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
    15. _MyBottomNavigationBarState(index) {
    16. this._currentIndex = index;
    17. }
    18. int _currentIndex = 0;
    19. List _pageList = [
    20. HomePage(),
    21. SearchPage(),
    22. DetailPage(),
    23. NewsPage(),
    24. MePage(),
    25. ];
    26. @override
    27. Widget build(BuildContext context) {
    28. return Scaffold(
    29. appBar: AppBar(
    30. title: Text('Lynn'),
    31. ),
    32. floatingActionButton: FloatingActionButton(
    33. onPressed: () {
    34. setState(() {
    35. this._currentIndex = 2;
    36. });
    37. },
    38. child: Icon(
    39. Icons.add,
    40. color: Colors.white,
    41. ),
    42. ),
    43. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    44. body: this._pageList[this._currentIndex],
    45. bottomNavigationBar: BottomNavigationBar(
    46. currentIndex: _currentIndex,
    47. onTap: (int index) {
    48. setState(() {
    49. if(index == 2) return; // 如果你想要独立这个按钮的事件, 可以把这个事件禁止,这样就不会出现该页面
    50. this._currentIndex = index;
    51. });
    52. },
    53. // iconSize: 20,
    54. // fixedColor: Colors.pink,
    55. type: BottomNavigationBarType.fixed, // 配置多个tabItem
    56. items: [
    57. BottomNavigationBarItem(
    58. icon: Icon(Icons.home),
    59. label: '首页',
    60. ),
    61. BottomNavigationBarItem(
    62. icon: Icon(Icons.search),
    63. label: '搜索',
    64. ),
    65. BottomNavigationBarItem(
    66. icon: Icon(Icons.home, color: Colors.white.withOpacity(0.05)),
    67. label: '详情',
    68. ),
    69. BottomNavigationBarItem(
    70. icon: Icon(Icons.forum),
    71. label: '新闻',
    72. ),
    73. BottomNavigationBarItem(
    74. icon: Icon(Icons.near_me),
    75. label: '我的',
    76. ),
    77. ],
    78. ),
    79. );
    80. }
    81. }

    底部导航凹进去的布局:

    1. import 'package:flutter/material.dart';
    2. import 'home.dart';
    3. import 'search.dart';
    4. import 'me.dart';
    5. import 'detail.dart';
    6. import 'news.dart';
    7. class MyBottomNavigationBar extends StatefulWidget {
    8. final int index;
    9. MyBottomNavigationBar({this.index = 0});
    10. @override
    11. _MyBottomNavigationBarState createState() =>
    12. _MyBottomNavigationBarState(this.index);
    13. }
    14. class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
    15. _MyBottomNavigationBarState(index) {
    16. this._currentIndex = index;
    17. }
    18. int _currentIndex = 0;
    19. List _pageList = [
    20. HomePage(),
    21. SearchPage(),
    22. DetailPage(),
    23. NewsPage(),
    24. MePage(),
    25. ];
    26. void onPageChanged(int value) {
    27. setState(() {
    28. this._currentIndex = value;
    29. });
    30. }
    31. Color getColor(int value) {
    32. return this._currentIndex == value
    33. ? Theme.of(context).cardColor
    34. : Color(0XFFBBBBBB);
    35. }
    36. @override
    37. Widget build(BuildContext context) {
    38. return Scaffold(
    39. appBar: AppBar(
    40. title: Text('Lynn'),
    41. ),
    42. floatingActionButton: FloatingActionButton(
    43. onPressed: () {
    44. setState(() {
    45. this._currentIndex = 2;
    46. });
    47. },
    48. child: Icon(
    49. Icons.add,
    50. color: Colors.white,
    51. ),
    52. ),
    53. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    54. body: this._pageList[this._currentIndex],
    55. // BottomAppBar 组件 配置 shape: CircularNotchedRectangle(), 这样就会在中间有个 凹进入的半圆用于放FloatingActionButton按钮
    56. bottomNavigationBar: BottomAppBar(
    57. color: Theme.of(context).accentColor,
    58. shape: CircularNotchedRectangle(),
    59. child: Padding(
    60. padding: EdgeInsets.fromLTRB(0, 6, 0, 6),
    61. child: Row(
    62. mainAxisSize: MainAxisSize.max,
    63. mainAxisAlignment: MainAxisAlignment.spaceAround,
    64. children: [
    65. GestureDetector(
    66. onTap: () {
    67. onPageChanged(0);
    68. },
    69. child: Column(
    70. mainAxisSize: MainAxisSize.min,
    71. children: <Widget>[
    72. Icon(Icons.home, color: getColor(0)),
    73. Text("首页", style: TextStyle(color: getColor(0)))
    74. ],
    75. )),
    76. GestureDetector(
    77. onTap: () {
    78. onPageChanged(1);
    79. },
    80. child: Column(
    81. mainAxisSize: MainAxisSize.min,
    82. children: <Widget>[
    83. Icon(Icons.search, color: getColor(1)),
    84. Text("搜索", style: TextStyle(color: getColor(1)))
    85. ],
    86. )),
    87. GestureDetector(
    88. onTap: () {
    89. onPageChanged(2);
    90. },
    91. child: Column(
    92. mainAxisSize: MainAxisSize.min,
    93. children: <Widget>[
    94. Icon(Icons.home, color: getColor(2)),
    95. Text("详情", style: TextStyle(color: getColor(2)))
    96. ],
    97. )),
    98. GestureDetector(
    99. onTap: () {
    100. onPageChanged(3);
    101. },
    102. child: Column(
    103. mainAxisSize: MainAxisSize.min,
    104. children: <Widget>[
    105. Icon(Icons.forum, color: getColor(3)),
    106. Text("新闻", style: TextStyle(color: getColor(3)))
    107. ],
    108. )),
    109. GestureDetector(
    110. onTap: () {
    111. onPageChanged(4);
    112. },
    113. child: Column(
    114. mainAxisSize: MainAxisSize.min,
    115. children: <Widget>[
    116. Icon(Icons.near_me, color: getColor(4)),
    117. Text("我的", style: TextStyle(color: getColor(4)))
    118. ],
    119. )),
    120. ],
    121. ),
    122. ),
    123. ),
    124. );
    125. }
    126. }

    底部导航左右滑动页面切换:

    1. import 'package:flutter/material.dart';
    2. import 'home.dart';
    3. import 'search.dart';
    4. import 'me.dart';
    5. import 'detail.dart';
    6. import 'news.dart';
    7. class MyBottomNavigationBar extends StatefulWidget {
    8. final int index;
    9. MyBottomNavigationBar({this.index = 0});
    10. @override
    11. _MyBottomNavigationBarState createState() =>
    12. _MyBottomNavigationBarState(this.index);
    13. }
    14. class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
    15. _MyBottomNavigationBarState(index) {
    16. this._currentIndex = index;
    17. }
    18. PageController pageController; // 控制器
    19. int _currentIndex = 0;
    20. List _pageList = <Widget>[
    21. HomePage(),
    22. SearchPage(),
    23. DetailPage(),
    24. NewsPage(),
    25. MePage(),
    26. ];
    27. @override
    28. void initState() {
    29. super.initState();
    30. // 初始化 pageController , 默认显示第一个页面
    31. pageController = PageController(initialPage: this._currentIndex);
    32. }
    33. // 定义页面切换方法
    34. void onPageChanged(int value) {
    35. setState(() {
    36. this._currentIndex = value;
    37. });
    38. }
    39. Color getColor(int value) {
    40. return this._currentIndex == value
    41. ? Theme.of(context).cardColor
    42. : Color(0XFFBBBBBB);
    43. }
    44. // 点击 tabbar 切换页面 需要使用 pageController.animateToPage , 传递页面的索引值。
    45. void onTap(int value) {
    46. pageController.animateToPage(value,
    47. duration: const Duration(milliseconds: 100), curve: Curves.ease);
    48. }
    49. @override
    50. Widget build(BuildContext context) {
    51. return Scaffold(
    52. appBar: AppBar(
    53. title: Text('Lynn'),
    54. ),
    55. floatingActionButton: FloatingActionButton(
    56. onPressed: () {
    57. setState(() {
    58. onTap(2);
    59. });
    60. },
    61. child: Icon(
    62. Icons.add,
    63. color: Colors.white,
    64. ),
    65. ),
    66. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    67. // 利用 PageView 实现页面左右滑动切换
    68. body: PageView(
    69. children: _pageList,
    70. onPageChanged: onPageChanged, // 切换页面的方法
    71. controller: pageController, // 页面控制器
    72. ),
    73. bottomNavigationBar: BottomAppBar(
    74. color: Theme.of(context).accentColor,
    75. shape: CircularNotchedRectangle(), // 凹园
    76. child: Padding(
    77. padding: EdgeInsets.fromLTRB(0, 10, 0, 10),
    78. child: Row(
    79. mainAxisSize: MainAxisSize.max,
    80. mainAxisAlignment: MainAxisAlignment.spaceAround,
    81. children: [
    82. // GestureDetector 手势检测器
    83. GestureDetector(
    84. // 定义点击事件
    85. onTap: () {
    86. onTap(0); // 调用方法
    87. },
    88. child: Column(
    89. mainAxisSize: MainAxisSize.min,
    90. children: <Widget>[
    91. Icon(Icons.home, color: getColor(0)),
    92. Text("首页", style: TextStyle(color: getColor(0)))
    93. ],
    94. )),
    95. GestureDetector(
    96. onTap: () {
    97. onTap(1);
    98. },
    99. child: Column(
    100. mainAxisSize: MainAxisSize.min,
    101. children: <Widget>[
    102. Icon(Icons.search, color: getColor(1)),
    103. Text("搜索", style: TextStyle(color: getColor(1)))
    104. ],
    105. )),
    106. GestureDetector(
    107. onTap: () {
    108. onTap(2);
    109. },
    110. child: Column(
    111. mainAxisSize: MainAxisSize.min,
    112. children: <Widget>[
    113. Icon(Icons.home, color: Colors.transparent), // 设置透明
    114. Text("详情", style: TextStyle(color: getColor(2)))
    115. ],
    116. )),
    117. GestureDetector(
    118. onTap: () {
    119. onTap(3);
    120. },
    121. child: Column(
    122. mainAxisSize: MainAxisSize.min,
    123. children: <Widget>[
    124. Icon(Icons.forum, color: getColor(3)),
    125. Text("新闻", style: TextStyle(color: getColor(3)))
    126. ],
    127. )),
    128. GestureDetector(
    129. onTap: () {
    130. onTap(4);
    131. },
    132. child: Column(
    133. mainAxisSize: MainAxisSize.min,
    134. children: <Widget>[
    135. Icon(Icons.near_me, color: getColor(4)),
    136. Text("我的", style: TextStyle(color: getColor(4)))
    137. ],
    138. )),
    139. ],
    140. ),
    141. ),
    142. ),
    143. );
    144. }
    145. }