main_tab_page.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'HomePage/presentation/home_page.dart';
  4. import 'HospitalPage/presentation/hospital_page.dart';
  5. import 'MallPage/presentation/mall_page.dart';
  6. import 'MinePage/presentation/mine_page.dart';
  7. import 'main_tab_provider.dart';
  8. class MainTabPage extends ConsumerWidget {
  9. const MainTabPage({super.key});
  10. static const List<Widget> _pages = [
  11. HomePage(),
  12. HospitalPage(),
  13. MallPage(),
  14. MinePage(),
  15. ];
  16. @override
  17. Widget build(BuildContext context, WidgetRef ref) {
  18. // 使用 ref.watch 监听当前 tab 索引的变化
  19. final currentIndex = ref.watch(currentTabIndexProvider);
  20. return Scaffold(
  21. body: IndexedStack(
  22. index: currentIndex,
  23. children: _pages,
  24. ),
  25. bottomNavigationBar: BottomNavigationBar(
  26. currentIndex: currentIndex,
  27. type: BottomNavigationBarType.fixed,
  28. onTap: (index) {
  29. // 使用 ref.read 更新状态
  30. ref.read(currentTabIndexProvider.notifier).state = index;
  31. },
  32. items: const [
  33. BottomNavigationBarItem(
  34. icon: Icon(Icons.home_outlined),
  35. activeIcon: Icon(Icons.home),
  36. label: '首页',
  37. ),
  38. BottomNavigationBarItem(
  39. icon: Icon(Icons.local_hospital_outlined),
  40. activeIcon: Icon(Icons.local_hospital),
  41. label: '医院',
  42. ),
  43. BottomNavigationBarItem(
  44. icon: Icon(Icons.shopping_cart_outlined),
  45. activeIcon: Icon(Icons.shopping_cart),
  46. label: '商城',
  47. ),
  48. BottomNavigationBarItem(
  49. icon: Icon(Icons.person_outlined),
  50. activeIcon: Icon(Icons.person),
  51. label: '我的',
  52. ),
  53. ],
  54. ),
  55. );
  56. }
  57. }