diff --git a/frontend/mobile-app/lib/features/auth/presentation/pages/splash_page.dart b/frontend/mobile-app/lib/features/auth/presentation/pages/splash_page.dart index 484c4595..ae406378 100644 --- a/frontend/mobile-app/lib/features/auth/presentation/pages/splash_page.dart +++ b/frontend/mobile-app/lib/features/auth/presentation/pages/splash_page.dart @@ -69,8 +69,8 @@ class _SplashPageState extends ConsumerState { context.go(RoutePaths.onboarding); } - // 总是检查应用更新(跳转后执行,避免阻塞启动) - checkForAppUpdate(context); + // 注意:更新检查已移至 HomeShellPage,在主页面加载后执行 + // 这里不再调用 checkForAppUpdate,因为 context.go() 后 context 已失效 } @override diff --git a/frontend/mobile-app/lib/features/home/presentation/pages/home_shell_page.dart b/frontend/mobile-app/lib/features/home/presentation/pages/home_shell_page.dart index 6229141f..cbae7a33 100644 --- a/frontend/mobile-app/lib/features/home/presentation/pages/home_shell_page.dart +++ b/frontend/mobile-app/lib/features/home/presentation/pages/home_shell_page.dart @@ -4,13 +4,61 @@ import 'package:go_router/go_router.dart'; import '../../../../core/theme/app_colors.dart'; import '../../../../core/theme/app_dimensions.dart'; import '../../../../routes/route_paths.dart'; +import '../../../../bootstrap.dart'; import '../widgets/bottom_nav_bar.dart'; -class HomeShellPage extends ConsumerWidget { +class HomeShellPage extends ConsumerStatefulWidget { final Widget child; const HomeShellPage({super.key, required this.child}); + @override + ConsumerState createState() => _HomeShellPageState(); +} + +class _HomeShellPageState extends ConsumerState + with WidgetsBindingObserver { + /// 上次检查更新的时间 + static DateTime? _lastCheckTime; + + /// 检查间隔(24小时) + static const _checkInterval = Duration(hours: 24); + + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addObserver(this); + // 首次进入时检查更新 + WidgetsBinding.instance.addPostFrameCallback((_) { + _checkForUpdateIfNeeded(); + }); + } + + @override + void dispose() { + WidgetsBinding.instance.removeObserver(this); + super.dispose(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + // 从后台恢复时检查更新 + if (state == AppLifecycleState.resumed) { + _checkForUpdateIfNeeded(); + } + } + + Future _checkForUpdateIfNeeded() async { + final now = DateTime.now(); + + // 如果从未检查过,或者距上次检查超过24小时,则检查 + if (_lastCheckTime == null || + now.difference(_lastCheckTime!) > _checkInterval) { + _lastCheckTime = now; + await checkForAppUpdate(context); + } + } + int _getCurrentIndex(BuildContext context) { final location = GoRouterState.of(context).uri.path; if (location.startsWith(RoutePaths.ranking)) return 0; @@ -38,9 +86,9 @@ class HomeShellPage extends ConsumerWidget { } @override - Widget build(BuildContext context, WidgetRef ref) { + Widget build(BuildContext context) { return Scaffold( - body: child, + body: widget.child, bottomNavigationBar: BottomNavBar( currentIndex: _getCurrentIndex(context), onTap: (index) => _onTabTapped(context, index),