main_page.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'package:cs_resources/theme/app_colors_theme.dart';
  2. import 'package:flutter/src/widgets/framework.dart';
  3. import 'package:flutter_hooks/flutter_hooks.dart';
  4. import 'package:hooks_riverpod/hooks_riverpod.dart';
  5. import 'package:auto_route/auto_route.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:plugin_basic/constants/app_constant.dart';
  8. import 'package:shared/utils/device_utils.dart';
  9. import 'package:shared/utils/event_bus.dart';
  10. import 'package:shared/utils/log_utils.dart';
  11. import '../../router/page/main_page_router.dart';
  12. import 'package:router/ext/auto_router_extensions.dart';
  13. import 'main_view_model.dart';
  14. @RoutePage()
  15. class MainPage extends HookConsumerWidget {
  16. const MainPage({super.key});
  17. //启动当前页面
  18. static void startInstance({BuildContext? context}) {
  19. if (context != null) {
  20. context.router.popUntilRoot();
  21. context.router.replace(const MainPageRoute());
  22. } else {
  23. appRouter.popUntilRoot();
  24. appRouter.replace(const MainPageRoute());
  25. }
  26. }
  27. @override
  28. Widget build(BuildContext context, WidgetRef ref) {
  29. final viewModel = ref.watch(mainViewModelProvider.notifier);
  30. final state = ref.watch(mainViewModelProvider);
  31. useEffect(() {
  32. Future.microtask(() => viewModel.fetchUserInfo());
  33. return () {
  34. };
  35. }, []);
  36. return AutoTabsScaffold(
  37. routes: const [
  38. HomePageRoute(),
  39. VisitorPageRoute(),
  40. FeedbackPageRoute(),
  41. MePageRoute(),
  42. ],
  43. transitionBuilder: (context, child, animation) => FadeTransition(
  44. opacity: animation,
  45. child: child,
  46. ),
  47. bottomNavigationBuilder: (context, tabsRouter) {
  48. return BottomNavigationBar(
  49. elevation: 10,
  50. backgroundColor: context.appColors.backgroundWhite,
  51. type: BottomNavigationBarType.fixed,
  52. currentIndex: tabsRouter.activeIndex,
  53. onTap: tabsRouter.setActiveIndex,
  54. unselectedLabelStyle: const TextStyle(color: AppColorsTheme.color666666, fontWeight: FontWeight.w400),
  55. selectedLabelStyle: TextStyle(color: context.appColors.textPrimary, fontWeight: FontWeight.w400),
  56. unselectedFontSize: 12,
  57. selectedFontSize: 12,
  58. items: () {
  59. var items = <BottomNavigationBarItem>[];
  60. state.bottomMap.forEach((k, v) {
  61. items.add(BottomNavigationBarItem(
  62. label: k,
  63. icon: v[0],
  64. activeIcon: DeviceUtils.isDarkMode(context) ? v[2] : v[1],
  65. ));
  66. });
  67. return items;
  68. }(),
  69. );
  70. },
  71. );
  72. }
  73. }