You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.3 KiB
87 lines
2.3 KiB
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import '../../../routes/app_routes.dart';
|
|
import '../../../core/themes/theme_service.dart';
|
|
|
|
class SplashController extends GetxController {
|
|
// 进度条动画控制
|
|
final _loadingProgress = 0.0.obs;
|
|
double get loadingProgress => _loadingProgress.value;
|
|
|
|
// 版本号
|
|
final String version = 'v1.0.0';
|
|
|
|
// 资源加载状态
|
|
final _isResourceLoaded = false.obs;
|
|
bool get isResourceLoaded => _isResourceLoaded.value;
|
|
|
|
// 主题服务
|
|
final ThemeService _themeService = ThemeService();
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
|
|
// 确保页面先渲染,再执行其他操作
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
// 启动进度条动画
|
|
_startLoadingAnimation();
|
|
|
|
// 异步加载应用资源
|
|
_loadAppResources();
|
|
});
|
|
}
|
|
|
|
// 进度条动画
|
|
void _startLoadingAnimation() {
|
|
// 模拟0.5秒的加载进度
|
|
Timer.periodic(const Duration(milliseconds: 5), (timer) {
|
|
if (_loadingProgress.value < 1.0) {
|
|
_loadingProgress.value += 0.01;
|
|
} else {
|
|
timer.cancel();
|
|
// 当进度条动画完成且资源加载完毕时,跳转到登录页面
|
|
if (_isResourceLoaded.value) {
|
|
_navigateToLogin();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// 异步加载应用资源
|
|
Future<void> _loadAppResources() async {
|
|
try {
|
|
// 在这里可以添加应用初始化需要的资源加载
|
|
// 例如:预加载图片、初始化数据库、加载配置等
|
|
|
|
// 模拟耗时操作
|
|
await Future.delayed(const Duration(milliseconds: 100));
|
|
|
|
// TODO: 在此处添加实际的资源加载代码
|
|
|
|
// 标记资源加载完成
|
|
_isResourceLoaded.value = true;
|
|
|
|
// 如果进度条已经走完,则跳转到登录页面
|
|
if (_loadingProgress.value >= 1.0) {
|
|
_navigateToLogin();
|
|
}
|
|
} catch (e) {
|
|
// 处理资源加载错误
|
|
// 即使加载失败,也应该允许用户继续使用应用
|
|
_isResourceLoaded.value = true;
|
|
|
|
// 记录错误日志
|
|
print('资源加载错误: $e');
|
|
}
|
|
}
|
|
|
|
// 跳转到登录页面
|
|
void _navigateToLogin() {
|
|
// 添加一个小延迟,确保UI完全渲染
|
|
Future.delayed(const Duration(milliseconds: 200), () {
|
|
Get.offAllNamed(Routes.login);
|
|
});
|
|
}
|
|
}
|
|
|