fix(mobile): move update check to HomeShellPage with valid context
- Move checkForAppUpdate from SplashPage to HomeShellPage - Use WidgetsBindingObserver to check on app resume - Add 24-hour interval check to avoid frequent API calls - Fix: context.go() invalidates context, so check was never executed 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
12116ff164
commit
908f50ab11
|
|
@ -69,8 +69,8 @@ class _SplashPageState extends ConsumerState<SplashPage> {
|
|||
context.go(RoutePaths.onboarding);
|
||||
}
|
||||
|
||||
// 总是检查应用更新(跳转后执行,避免阻塞启动)
|
||||
checkForAppUpdate(context);
|
||||
// 注意:更新检查已移至 HomeShellPage,在主页面加载后执行
|
||||
// 这里不再调用 checkForAppUpdate,因为 context.go() 后 context 已失效
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -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<HomeShellPage> createState() => _HomeShellPageState();
|
||||
}
|
||||
|
||||
class _HomeShellPageState extends ConsumerState<HomeShellPage>
|
||||
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<void> _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),
|
||||
|
|
|
|||
Loading…
Reference in New Issue