bottomNavigationBar 配置底部导航条。

    1. import 'package:flutter/material.dart';
    2. void main() => runApp(MyApp());
    3. class MyApp extends StatelessWidget {
    4. @override
    5. Widget build(BuildContext context) {
    6. return MaterialApp(
    7. title: 'Retrieve Text Input',
    8. home: MyBottomNavigationBar(),
    9. );
    10. }
    11. }
    12. class MyBottomNavigationBar extends StatefulWidget {
    13. @override
    14. _MyBottomNavigationBarState createState() => _MyBottomNavigationBarState();
    15. }
    16. class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
    17. // 定义当前索引值
    18. int _currentIndex = 0;
    19. @override
    20. Widget build(BuildContext context) {
    21. return Scaffold(
    22. appBar: AppBar(
    23. title: Text('Hello Flutter'),
    24. ),
    25. body: Center(
    26. child: Text('Hello Flutter'),
    27. ),
    28. // bottomNavigationBar 底部导航
    29. bottomNavigationBar: BottomNavigationBar(
    30. currentIndex: _currentIndex, // 当前选中的索引值
    31. onTap: (int index) {
    32. setState(() {
    33. this._currentIndex = index;
    34. });
    35. },
    36. items: [
    37. BottomNavigationBarItem(
    38. // icon 可以是 Icon 组件 也可以是 image
    39. icon: Icon(Icons.home),
    40. label: '首页',
    41. ),
    42. BottomNavigationBarItem(
    43. icon: Icon(Icons.search),
    44. label: '搜索',
    45. ),
    46. ],
    47. ),
    48. );
    49. }
    50. }

    切换页面:

    1. import 'package:flutter/material.dart';
    2. import 'tabs/home.dart';
    3. import 'tabs/search.dart';
    4. void main() => runApp(MyApp());
    5. class MyApp extends StatelessWidget {
    6. @override
    7. Widget build(BuildContext context) {
    8. return MaterialApp(
    9. title: 'Retrieve Text Input',
    10. home: MyBottomNavigationBar(),
    11. );
    12. }
    13. }
    14. class MyBottomNavigationBar extends StatefulWidget {
    15. @override
    16. _MyBottomNavigationBarState createState() => _MyBottomNavigationBarState();
    17. }
    18. class _MyBottomNavigationBarState extends State<MyBottomNavigationBar> {
    19. int _currentIndex = 0;
    20. // 定义页面 List
    21. List _pageList = [
    22. HomeWidget(),
    23. SearchWidget(),
    24. ];
    25. @override
    26. Widget build(BuildContext context) {
    27. return Scaffold(
    28. appBar: AppBar(
    29. title: Text('Hello Flutter'),
    30. ),
    31. // 根据当前的 currentIndex 渲染对应的页面
    32. body: this._pageList[this._currentIndex],
    33. bottomNavigationBar: BottomNavigationBar(
    34. currentIndex: _currentIndex,
    35. onTap: (int index) {
    36. setState(() {
    37. this._currentIndex = index;
    38. });
    39. },
    40. iconSize: 30, // 改变icon大小
    41. fixedColor: Colors.pink, // 改变颜色
    42. type: BottomNavigationBarType.fixed, // 配置多个tabItem
    43. items: [
    44. BottomNavigationBarItem(
    45. icon: Icon(Icons.home),
    46. label: '首页',
    47. ),
    48. BottomNavigationBarItem(
    49. icon: Icon(Icons.search),
    50. label: '搜索',
    51. ),
    52. ],
    53. ),
    54. );
    55. }
    56. }