import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'HomePage/presentation/home_page.dart'; import 'HospitalPage/presentation/hospital_page.dart'; import 'MallPage/presentation/mall_page.dart'; import 'MinePage/presentation/mine_page.dart'; import 'main_tab_provider.dart'; class MainTabPage extends ConsumerWidget { const MainTabPage({super.key}); static const List _pages = [ HomePage(), HospitalPage(), MallPage(), MinePage(), ]; @override Widget build(BuildContext context, WidgetRef ref) { // 使用 ref.watch 监听当前 tab 索引的变化 final currentIndex = ref.watch(currentTabIndexProvider); return Scaffold( body: IndexedStack( index: currentIndex, children: _pages, ), bottomNavigationBar: BottomNavigationBar( currentIndex: currentIndex, type: BottomNavigationBarType.fixed, onTap: (index) { // 使用 ref.read 更新状态 ref.read(currentTabIndexProvider.notifier).state = index; }, items: const [ BottomNavigationBarItem( icon: Icon(Icons.home_outlined), activeIcon: Icon(Icons.home), label: '首页', ), BottomNavigationBarItem( icon: Icon(Icons.local_hospital_outlined), activeIcon: Icon(Icons.local_hospital), label: '医院', ), BottomNavigationBarItem( icon: Icon(Icons.shopping_cart_outlined), activeIcon: Icon(Icons.shopping_cart), label: '商城', ), BottomNavigationBarItem( icon: Icon(Icons.person_outlined), activeIcon: Icon(Icons.person), label: '我的', ), ], ), ); } }