69 lines
2.1 KiB
Dart
69 lines
2.1 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
import 'utils/app_theme.dart';
|
||
import 'utils/app_initializer.dart';
|
||
import 'utils/force_guide_checker.dart';
|
||
import 'routes/app_routes.dart';
|
||
import 'constants/app_constants.dart';
|
||
import 'config/app_config.dart';
|
||
import 'controllers/shared_preferences_storage_controller.dart';
|
||
import 'services/get/theme_controller.dart';
|
||
import 'views/profile/guide/sp-guide.dart';
|
||
|
||
void main() async {
|
||
WidgetsFlutterBinding.ensureInitialized();
|
||
|
||
await SharedPreferencesStorageController.init();
|
||
|
||
// 初始化 AppConfig,动态获取版本号
|
||
await AppConfig.init();
|
||
|
||
// 初始化 ThemeController(在 AppInitializer 之前,确保主题最先加载)
|
||
Get.put(ThemeController(), permanent: true);
|
||
|
||
// 检查是否需要显示引导页
|
||
String initialRoute = await _getInitialRoute();
|
||
|
||
runApp(MyApp(initialRoute: initialRoute));
|
||
}
|
||
|
||
Future<String> _getInitialRoute() async {
|
||
final prefs = await SharedPreferences.getInstance();
|
||
bool agreementAccepted =
|
||
prefs.getBool(AppConfig.keyAgreementAccepted) ?? false;
|
||
|
||
// 如果协议未被接受,显示引导页
|
||
if (!agreementAccepted) {
|
||
return '/sp-guide';
|
||
}
|
||
|
||
// 否则使用 AppInitializer 返回的初始路由
|
||
final result = await AppInitializer.initialize();
|
||
return result.initialRoute;
|
||
}
|
||
|
||
class MyApp extends StatelessWidget {
|
||
final String initialRoute;
|
||
|
||
const MyApp({super.key, required this.initialRoute});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
// 使用 Obx 监听主题变化
|
||
return Obx(() {
|
||
final themeController = Get.find<ThemeController>();
|
||
return GetMaterialApp(
|
||
title: AppConstants.appName,
|
||
debugShowCheckedModeBanner: false,
|
||
theme: AppTheme.lightTheme,
|
||
darkTheme: AppTheme.darkTheme,
|
||
themeMode: themeController.currentThemeMode,
|
||
initialRoute: initialRoute,
|
||
onGenerateRoute: AppRoutes.generateRoute,
|
||
routes: {'/sp-guide': (context) => const SpGuidePage()},
|
||
);
|
||
});
|
||
}
|
||
}
|