这是CaiYouHui前端,一个关于flutter的安卓app,前端使用flutter实现

bottom_nav_bar.dart 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import '../../core/constants/route_constants.dart';
  4. import '../../presentation/providers/auth_provider.dart';
  5. class BottomNavBar extends StatefulWidget {
  6. final int initialIndex;
  7. const BottomNavBar({
  8. super.key,
  9. this.initialIndex = 0,
  10. });
  11. @override
  12. State<BottomNavBar> createState() => _BottomNavBarState();
  13. }
  14. class _BottomNavBarState extends State<BottomNavBar> {
  15. int _selectedIndex = 0;
  16. @override
  17. void initState() {
  18. super.initState();
  19. _selectedIndex = widget.initialIndex;
  20. }
  21. void _onItemTapped(int index) {
  22. setState(() {
  23. _selectedIndex = index;
  24. });
  25. switch (index) {
  26. case 0:
  27. if (ModalRoute.of(context)?.settings.name != RouteConstants.home) {
  28. Navigator.of(context).pushReplacementNamed(RouteConstants.home);
  29. }
  30. break;
  31. case 1:
  32. if (ModalRoute.of(context)?.settings.name != RouteConstants.news) {
  33. Navigator.of(context).pushReplacementNamed(RouteConstants.news);
  34. }
  35. break;
  36. case 2:
  37. if (ModalRoute.of(context)?.settings.name != RouteConstants.services) {
  38. Navigator.of(context).pushReplacementNamed(RouteConstants.services);
  39. }
  40. break;
  41. case 3:
  42. if (ModalRoute.of(context)?.settings.name != RouteConstants.profile) {
  43. Navigator.of(context).pushReplacementNamed(RouteConstants.profile);
  44. }
  45. break;
  46. }
  47. }
  48. @override
  49. Widget build(BuildContext context) {
  50. return BottomNavigationBar(
  51. items: const [
  52. BottomNavigationBarItem(
  53. icon: Icon(Icons.home_outlined),
  54. activeIcon: Icon(Icons.home),
  55. label: '首页',
  56. ),
  57. BottomNavigationBarItem(
  58. icon: Icon(Icons.newspaper_outlined),
  59. activeIcon: Icon(Icons.newspaper),
  60. label: '推荐',
  61. ),
  62. BottomNavigationBarItem(
  63. icon: Icon(Icons.work_outline),
  64. activeIcon: Icon(Icons.work),
  65. label: '服务',
  66. ),
  67. BottomNavigationBarItem(
  68. icon: Icon(Icons.person_outline),
  69. activeIcon: Icon(Icons.person),
  70. label: '我的',
  71. ),
  72. ],
  73. currentIndex: _selectedIndex,
  74. selectedItemColor: Theme.of(context).primaryColor,
  75. unselectedItemColor: Colors.grey,
  76. showUnselectedLabels: true,
  77. type: BottomNavigationBarType.fixed,
  78. onTap: _onItemTapped,
  79. );
  80. }
  81. }