import 'package:easy_refresh/easy_refresh.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:go_router/go_router.dart'; import 'package:news_app/event/logout_event.dart'; import 'package:news_app/provider/global_theme_provider.dart'; import 'package:news_app/provider/global_user_provider.dart'; import 'package:news_app/provider/gray_provider.dart'; import 'package:news_app/provider/main_provider.dart'; import 'package:news_app/ui/activity/activity_detail_page.dart'; import 'package:news_app/ui/activity/main_activity_page.dart'; import 'package:news_app/ui/login/login_register_page.dart'; import 'package:news_app/ui/login/privacy_agreement_page.dart'; import 'package:news_app/ui/login/splash_page.dart'; import 'package:news_app/ui/me/edit_user_profile_page.dart'; import 'package:news_app/ui/me/main_user_page.dart'; import 'package:news_app/ui/me/msg_center_page.dart'; import 'package:news_app/ui/me/setting_page.dart'; import 'package:news_app/ui/me/user_activity_page.dart'; import 'package:news_app/ui/me/user_change_password.dart'; import 'package:news_app/ui/me/user_favorite_page.dart'; import 'package:news_app/ui/me/user_feedback_page.dart'; import 'package:news_app/ui/me/user_read_history_page.dart'; import 'package:news_app/ui/me/user_score_page.dart'; import 'package:news_app/ui/me/user_share_page.dart'; import 'package:news_app/ui/me/user_shop_page.dart'; import 'package:news_app/ui/news/main_news_page.dart'; import 'package:news_app/ui/news/news_detail_page.dart'; import 'package:news_app/ui/news/special_list_page.dart'; import 'package:news_app/ui/search/activity_search_page.dart'; import 'package:news_app/ui/search/search_result_page.dart'; import 'package:news_app/ui/search/video_search_page.dart'; import 'package:news_app/ui/topic/main_topic_page.dart'; import 'package:news_app/ui/topic/topic_detail_page.dart'; import 'package:news_app/ui/topic/topic_list_page.dart'; import 'package:news_app/ui/video/main_video_page.dart'; import 'package:news_app/ui/video/video_detail_page.dart'; import 'package:news_app/util/log.util.dart'; import 'package:news_app/util/shared_prefs_instance_util.dart'; import 'package:news_app/util/theme_util.dart'; import 'constant/config.dart'; import 'gen/assets.gen.dart'; import 'http/http_util.dart'; import 'model/user_model.dart'; /// @author: bo.zeng /// @email: cnhbwds@gmail.com /// @date: 2025 2025/4/9 16:00 /// @description: void _initBeforeApp() async { WidgetsFlutterBinding.ensureInitialized(); //全局配置 Dio HttpUtil(); // 全局配置EasyRefresh EasyRefresh.defaultHeaderBuilder = () => ClassicHeader( dragText: '下拉刷新', armedText: '释放刷新', readyText: '正在刷新...', processingText: '刷新中...', processedText: '刷新完成', noMoreText: '没有更多数据', failedText: '刷新失败', messageText: '最后更新于 %T', messageStyle: TextStyle(color: Colors.grey[500], fontSize: 12), textStyle: TextStyle(color: Colors.grey[700], fontSize: 13), ); EasyRefresh.defaultFooterBuilder = () => ClassicFooter( dragText: '上拉加载', armedText: '释放加载', readyText: '准备加载...', processingText: '加载中...', processedText: '加载完成', noMoreText: '没有更多数据了', failedText: '加载失败', messageText: '最后加载于 %T', messageStyle: TextStyle(color: Colors.grey[500], fontSize: 12), textStyle: TextStyle(color: Colors.grey[700], fontSize: 13), ); // 初始化全局 SharedPreferences await initSharedPrefs(); } final grayProvider = NotifierProvider(() => GrayProvider()); void main() { _initBeforeApp(); runApp(ProviderScope(child: const MyApp())); } final globalThemeProvider = NotifierProvider( () { return GlobalThemeNotifier(); }, ); final globalUserProvider = NotifierProvider(() { return GlobalUserNotifier(); }); class MyApp extends ConsumerWidget { const MyApp({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { ref.read(grayProvider.notifier).fetchGray(); bool gray = ref.watch(grayProvider); return ScreenUtilInit( designSize: const Size(375, 812), splitScreenMode: true, //支持分屏尺寸 minTextAdapt: true, //否根据宽度/高度中的最小值适配文字 builder: (context, child) { return gray ? ColorFiltered( colorFilter: ColorFilter.matrix([ 0.2126, 0.7152, 0.0722, 0, 0, // R 0.2126, 0.7152, 0.0722, 0, 0, // G 0.2126, 0.7152, 0.0722, 0, 0, // B 0, 0, 0, 1, 0, // A ]), child: MaterialApp.router( debugShowCheckedModeBanner: false, theme: lightTheme, // 白天模式 darkTheme: darkTheme, // 黑夜模式 themeMode: ref.watch(globalThemeProvider), routerConfig: _routerConfig, ), ) : MaterialApp.router( debugShowCheckedModeBanner: false, theme: lightTheme, // 白天模式 darkTheme: darkTheme, // 黑夜模式 themeMode: ref.watch(globalThemeProvider), routerConfig: _routerConfig, ); }, ); } } class MainPage extends ConsumerStatefulWidget { const MainPage({super.key}); @override ConsumerState createState() => _MainPageState(); } class _MainPageState extends ConsumerState { late PageController _pageController; void _requestUserInfo() async { uuid = await getUuid() ?? ""; // 获取用户信息 await ref.read(globalUserProvider.notifier).fetchUserInfo(); } @override void initState() { super.initState(); _requestUserInfo(); _pageController = PageController(initialPage: 0); _registerEventBus(); } void _registerEventBus() { eventBus.on().listen((event) { consoleLog("用户退出了"); ref.read(globalUserProvider.notifier).clearUserInfo(); ref.invalidate(globalUserProvider); if (event.jumLogin) { final currentLocation = _routerConfig.routerDelegate.state.matchedLocation; consoleLog(currentLocation); //如果在首页,就不跳转 if (currentLocation != "/main") { rootNavigatorKey.currentContext?.go('/login'); } } else { _pageController.jumpToPage(0); ref.read(mainPageProvider.notifier).changeNavIndex(0); } }); } @override void dispose() { super.dispose(); _pageController.dispose(); } final mainPageProvider = NotifierProvider(() { return MainPageProvider(); }); @override Widget build(BuildContext context) { return Scaffold( body: PageView( scrollDirection: Axis.horizontal, controller: _pageController, physics: const NeverScrollableScrollPhysics(), children: [ MainNewsPage(), MainActivityPage(), MainVideoPage(), // MainTopicPage(), MainUserPage(), ], ), bottomNavigationBar: BottomNavigationBar( elevation: 10, type: BottomNavigationBarType.fixed, selectedItemColor: Colors.blue, unselectedItemColor: Colors.grey, currentIndex: ref.watch(mainPageProvider), items: [ BottomNavigationBarItem( icon: Image.asset( Assets.images.navNewsUnselect.path, width: 20.w, height: 20.w, ), label: "新闻", activeIcon: Image.asset( Assets.images.navNewsSelect.path, width: 20.w, height: 20.w, ), ), BottomNavigationBarItem( icon: Image.asset( Assets.images.navActivityUnselect.path, width: 20.w, height: 20.w, ), activeIcon: Image.asset( Assets.images.navActivitySelect.path, width: 20.w, height: 20.w, ), label: "活动", ), BottomNavigationBarItem( icon: Image.asset( Assets.images.navVideoUnselect.path, width: 20.w, height: 20.w, ), label: "视频", activeIcon: Image.asset( Assets.images.navVideoSelect.path, width: 20.w, height: 20.w, ), ), // BottomNavigationBarItem( // icon: Image.asset( // Assets.images.navTopicUnselect.path, // width: 20.w, // height: 20.w, // ), // label: "话题", // activeIcon: Image.asset( // Assets.images.navTopicSelect.path, // width: 20.w, // height: 20.w, // ), // ), BottomNavigationBarItem( icon: Image.asset( Assets.images.navUserUnselect.path, width: 20.w, height: 20.w, ), label: "我的", activeIcon: Image.asset( Assets.images.navUserSelect.path, width: 20.w, height: 20.w, ), ), ], onTap: (index) { if (index == 3 && uuid.isEmpty) { context.push('/login'); return; } _pageController.jumpToPage(index); ref.read(mainPageProvider.notifier).changeNavIndex(index); }, ), ); } } /// The route configuration. final GoRouter _routerConfig = GoRouter( navigatorKey: rootNavigatorKey, routes: [ GoRoute( path: '/', builder: (BuildContext context, GoRouterState state) { return const SplashPage(); }, ), GoRoute( path: '/login', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: LoginRegisterPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/login', // builder: (BuildContext context, GoRouterState state) { // return const LoginRegisterPage(); // }, // ), GoRoute( path: '/main', builder: (BuildContext context, GoRouterState state) { return const MainPage(); }, ), GoRoute( path: '/news/detail', pageBuilder: (context, state) { String contentId = state.extra as String; return CustomTransitionPage( key: state.pageKey, child: NewsDetailPage(contentId), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/news/detail', // builder: (BuildContext context, GoRouterState state) { // String contentId = state.extra as String; // return NewsDetailPage(contentId); // }, // ), GoRoute( path: '/activity/detail', pageBuilder: (context, state) { String activityId = state.extra as String; return CustomTransitionPage( key: state.pageKey, child: ActivityDetailPage(activityId), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: "/activity/detail", // builder: (BuildContext context, GoRouterState state) { // String activityId = state.extra as String; // return ActivityDetailPage(activityId); // }, // ), GoRoute( path: '/video/detail', pageBuilder: (context, state) { VideoParam data = state.extra as VideoParam; // return VideoDetailPage(param: data); return CustomTransitionPage( key: state.pageKey, child: VideoDetailPage(param: data), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/video/detail', // builder: (BuildContext context, GoRouterState state) { // VideoParam data = state.extra as VideoParam; // return VideoDetailPage(param: data); // }, // ), GoRoute( path: '/user/editProfile', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: EditProfilePage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/user/editProfile', // builder: (BuildContext context, GoRouterState state) { // return const EditProfilePage(); // }, // ), GoRoute( path: '/user/score', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: UserScorePage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/user/score', // builder: (BuildContext context, GoRouterState state) { // return const UserScorePage(); // }, // ), GoRoute( path: '/user/msgCenter', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: MsgCenterPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/user/msgCenter', // builder: (BuildContext context, GoRouterState state) { // return const MsgCenterPage(); // }, // ), GoRoute( path: '/user/favorite', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: UserFavoritePage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/user/favorite', // builder: (BuildContext context, GoRouterState state) { // return const UserFavoritePage(); // }, // ), GoRoute( path: '/user/readHistory', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: UserReadHistoryPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/user/readHistory', // builder: (BuildContext context, GoRouterState state) { // return const UserReadHistoryPage(); // }, // ), GoRoute( path: '/user/setting', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: SettingPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/user/setting', // builder: (BuildContext context, GoRouterState state) { // return const SettingPage(); // }, // ), GoRoute( path: '/user/feedback', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: UserFeedbackPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/user/feedback', // builder: (BuildContext context, GoRouterState state) { // return const UserFeedbackPage(); // }, // ), GoRoute( path: '/user/share', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: UserSharePage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: '/user/share', // builder: (BuildContext context, GoRouterState state) { // return const UserSharePage(); // }, // ), GoRoute( path: '/user/activity', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: UserActivityPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: "/user/activity", // builder: (BuildContext context, GoRouterState state) { // return UserActivityPage(); // }, // ), GoRoute( path: '/topic/list', pageBuilder: (context, state) { final contentId = state.extra as String; return CustomTransitionPage( key: state.pageKey, child: TopicListPage(contentId: contentId), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), GoRoute( path: '/topic/detail', pageBuilder: (context, state) { final contentId = state.extra as String; return CustomTransitionPage( key: state.pageKey, child: TopicDetailPage(contentId: contentId), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: "/topic/detail", // builder: (BuildContext context, GoRouterState state) { // final contentId = state.extra as String; // return TopicDetailPage(contentId: contentId); // }, // ), GoRoute( path: '/search', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: SearchResultPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: "/search", // builder: (BuildContext context, GoRouterState state) { // return SearchResultPage(); // }, // ), GoRoute( path: '/video/search', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: VideoSearchPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: "/video/search", // builder: (BuildContext context, GoRouterState state) { // return VideoSearchPage(); // }, // ), GoRoute( path: '/activity/search', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: ActivitySearchPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: "/activity/search", // builder: (BuildContext context, GoRouterState state) { // return ActivitySearchPage(); // }, // ), // GoRoute( path: '/user/privacy', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: PrivacyAgreementPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: "/user/privacy", // builder: (BuildContext context, GoRouterState state) { // return PrivacyAgreementPage(); // }, // ), GoRoute( path: '/user/changePassword', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: UserChangePassword(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), // GoRoute( // path: "/user/changePassword", // builder: (BuildContext context, GoRouterState state) { // return UserChangePassword(); // }, // ), GoRoute( path: '/user/shop', pageBuilder: (context, state) { return CustomTransitionPage( key: state.pageKey, child: UserShopPage(), transitionsBuilder: (context, animation, secondaryAnimation, child) { return FadeTransition( opacity: CurveTween( curve: Curves.easeInOutCirc, ).animate(animation), child: child, ); }, ); }, ), GoRoute( path: SpecialListPage.routeName, builder: (BuildContext context, GoRouterState state) { String cid = state.extra == null ? "670577300123717" : state.extra as String; return SpecialListPage(cid: cid); }, ), ], );