| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- import 'package:flutter/material.dart';
- import 'package:provider/provider.dart';
- import '../../core/constants/route_constants.dart';
- import '../../presentation/providers/auth_provider.dart';
-
- class BottomNavBar extends StatefulWidget {
- final int initialIndex;
-
- const BottomNavBar({
- super.key,
- this.initialIndex = 0,
- });
-
- @override
- State<BottomNavBar> createState() => _BottomNavBarState();
- }
-
- class _BottomNavBarState extends State<BottomNavBar> {
- int _selectedIndex = 0;
-
- @override
- void initState() {
- super.initState();
- _selectedIndex = widget.initialIndex;
- }
-
- void _onItemTapped(int index) {
-
- setState(() {
- _selectedIndex = index;
- });
-
- switch (index) {
- case 0:
- if (ModalRoute.of(context)?.settings.name != RouteConstants.home) {
- Navigator.of(context).pushReplacementNamed(RouteConstants.home);
- }
- break;
- case 1:
- if (ModalRoute.of(context)?.settings.name != RouteConstants.news) {
- Navigator.of(context).pushReplacementNamed(RouteConstants.news);
- }
- break;
- case 2:
- if (ModalRoute.of(context)?.settings.name != RouteConstants.services) {
- Navigator.of(context).pushReplacementNamed(RouteConstants.services);
- }
- break;
- case 3:
- if (ModalRoute.of(context)?.settings.name != RouteConstants.profile) {
- Navigator.of(context).pushReplacementNamed(RouteConstants.profile);
- }
- break;
- }
- }
-
- @override
- Widget build(BuildContext context) {
- return BottomNavigationBar(
- items: const [
- BottomNavigationBarItem(
- icon: Icon(Icons.home_outlined),
- activeIcon: Icon(Icons.home),
- label: '首页',
- ),
- BottomNavigationBarItem(
- icon: Icon(Icons.newspaper_outlined),
- activeIcon: Icon(Icons.newspaper),
- label: '推荐',
- ),
- BottomNavigationBarItem(
- icon: Icon(Icons.work_outline),
- activeIcon: Icon(Icons.work),
- label: '服务',
- ),
- BottomNavigationBarItem(
- icon: Icon(Icons.person_outline),
- activeIcon: Icon(Icons.person),
- label: '我的',
- ),
- ],
- currentIndex: _selectedIndex,
- selectedItemColor: Theme.of(context).primaryColor,
- unselectedItemColor: Colors.grey,
- showUnselectedLabels: true,
- type: BottomNavigationBarType.fixed,
- onTap: _onItemTapped,
- );
- }
- }
|