flutter学习之底部导航栏制作

迷南。 2021-09-17 11:50 513阅读 0赞

首先我们先写一个主入口文件,这个文件只是简单的APP通用结构,最主要的是要引入自定义的BottomNavigationWidget组件。

main.dart

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

BottomNaivgationWidget自定义

  1. import 'package:flutter/material.dart';
  2. class BottomNavigationWidget extends StatefulWidget {
  3. _BottomNavigationWidgetState createState() => _BottomNavigationWidgetState();
  4. }
  5. class _BottomNavigationWidgetState extends State<BottomNavigationWidget> {
  6. final _BottomNavigationColor = Colors.blue;
  7. @override
  8. Widget build(BuildContext context) {
  9. return Scaffold(
  10. bottomNavigationBar: BottomNavigationBar(
  11. items: [
  12. BottomNavigationBarItem(
  13. icon:Icon(
  14. Icons.home,
  15. color:_BottomNavigationColor,
  16. ),
  17. title:Text(
  18. 'Home',
  19. style:TextStyle(color:_BottomNavigationColor)
  20. )
  21. ),
  22. BottomNavigationBarItem(
  23. icon:Icon(
  24. Icons.email,
  25. color:_BottomNavigationColor,
  26. ),
  27. title:Text(
  28. 'Email',
  29. style:TextStyle(color:_BottomNavigationColor)
  30. )
  31. ),
  32. BottomNavigationBarItem(
  33. icon:Icon(
  34. Icons.pages,
  35. color:_BottomNavigationColor,
  36. ),
  37. title:Text(
  38. 'Pages',
  39. style:TextStyle(color:_BottomNavigationColor)
  40. )
  41. ),
  42. BottomNavigationBarItem(
  43. icon:Icon(
  44. Icons.airplay,
  45. color:_BottomNavigationColor,
  46. ),
  47. title:Text(
  48. 'AipPlay',
  49. style:TextStyle(color:_BottomNavigationColor)
  50. )
  51. ),
  52. ],
  53. type:BottomNavigationBarType.fixed
  54. ),
  55. );
  56. }
  57. }

先来写一个HomeScreen组件,新建一个pages目录,然后在目录下面新建home_screen.dart文件。

  1. import 'package:flutter/material.dart';
  2. class HomeScreen extends StatelessWidget {
  3. @override
  4. Widget build(BuildContext context) {
  5. return Scaffold(
  6. appBar:AppBar(
  7. title: Text('HOME'),
  8. ),
  9. body:Center(
  10. child: Text('HOME'),
  11. )
  12. );
  13. }
  14. }

bottom_navigation_widget.dart的全部代码

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

ok,完整的底部导航可以了,这个笔记值得记录,以后就可以直接copy了

原文来自前端大牛技术胖的博客

http://jspang.com/post/flutterDemo.html#toc-ff4

发表评论

表情:
评论列表 (有 0 条评论,513人围观)

还没有评论,来说两句吧...

相关阅读

    相关 底部导航

    1.添加相应的文件 2.分别加入4个Fragment以及布局文件 3.MianActivity的引用 4.MainActivity的布局文件   添加相应的文件: