main_tab_page.dart 1.9 KB

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