72 lines
2.1 KiB
Dart
72 lines
2.1 KiB
Dart
import 'package:cupertino_ui/cupertino_ui.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
|
||
import '../../../core/storage/token_storage.dart';
|
||
import '../../../l10n/app_localizations.dart';
|
||
|
||
/// 启动页。
|
||
///
|
||
/// 仅负责首屏展示与路由决策(登录态判断),不含业务请求。
|
||
class SplashPage extends ConsumerStatefulWidget {
|
||
const SplashPage({super.key});
|
||
|
||
@override
|
||
ConsumerState<SplashPage> createState() => _SplashPageState();
|
||
}
|
||
|
||
class _SplashPageState extends ConsumerState<SplashPage> {
|
||
bool _navigated = false;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
// 首帧绘制后做一次性的路由决策。
|
||
// 不使用 Future.delayed 做跳转:App 进后台再回前台时计时器可能被挂起,
|
||
// 导致恢复后跳转不可靠、画面卡在第一屏。改用首帧回调 + 幂等保护。
|
||
WidgetsBinding.instance.addPostFrameCallback((_) => _decideRoute());
|
||
}
|
||
|
||
void _decideRoute() {
|
||
if (_navigated || !mounted) return;
|
||
_navigated = true;
|
||
try {
|
||
final target = TokenStorage.isLoggedIn ? '/home' : '/login';
|
||
context.go(target);
|
||
} catch (_) {
|
||
// 极端情况下的路由异常不应卡死首屏,重试一次。
|
||
if (mounted) {
|
||
_navigated = false;
|
||
WidgetsBinding.instance.addPostFrameCallback((_) => _decideRoute());
|
||
}
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final l10n = AppLocalizations.of(context);
|
||
return CupertinoPageScaffold(
|
||
backgroundColor: CupertinoColors.systemBackground,
|
||
child: Center(
|
||
child: Column(
|
||
mainAxisAlignment: MainAxisAlignment.center,
|
||
children: [
|
||
const Icon(
|
||
CupertinoIcons.heart_fill,
|
||
size: 64,
|
||
color: CupertinoColors.activeBlue,
|
||
),
|
||
const SizedBox(height: 16),
|
||
Text(
|
||
l10n.appTitle,
|
||
style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w600),
|
||
),
|
||
SizedBox(height: 8),
|
||
CupertinoActivityIndicator(),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|