Initial commit: Flutter 无书应用项目
This commit is contained in:
105
lib/main.dart
Normal file
105
lib/main.dart
Normal file
@@ -0,0 +1,105 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'config/app_config.dart';
|
||||
import 'utils/app_theme.dart';
|
||||
import 'utils/screen_adapter.dart';
|
||||
import 'utils/force_guide_checker.dart';
|
||||
import 'routes/app_routes.dart';
|
||||
import 'constants/app_constants.dart';
|
||||
import 'controllers/sqlite_storage_controller.dart';
|
||||
|
||||
void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// 初始化SQLite存储
|
||||
await SQLiteStorageController.init();
|
||||
|
||||
// 初始化SharedPreferences
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
|
||||
debugPrint('========================================');
|
||||
debugPrint('======== 应用启动 - 强制引导页 ========');
|
||||
debugPrint('========================================');
|
||||
|
||||
// 1. 先获取保存的版本号
|
||||
final savedVersion = prefs.getInt(AppConfig.keyAppVersion);
|
||||
debugPrint('保存的版本号: $savedVersion');
|
||||
|
||||
// 2. 如果是首次安装(没有版本号)或者版本不匹配
|
||||
bool isFirstInstall =
|
||||
savedVersion == null || savedVersion != AppConfig.appVersionCode;
|
||||
debugPrint('是否首次安装/升级: $isFirstInstall');
|
||||
|
||||
if (isFirstInstall) {
|
||||
debugPrint('--- 首次安装/升级,清空所有配置 ---');
|
||||
await prefs.clear();
|
||||
debugPrint('配置已清空');
|
||||
|
||||
// 3. 强制设置引导页相关的标志
|
||||
await prefs.setBool(AppConfig.keyFirstLaunch, true);
|
||||
await prefs.setBool(AppConfig.keyAgreementAccepted, false);
|
||||
await prefs.setBool(AppConfig.keyShowGuideOnStartup, false);
|
||||
await prefs.setInt(AppConfig.keyAppVersion, AppConfig.appVersionCode);
|
||||
debugPrint('已设置 firstLaunch=true');
|
||||
debugPrint('已设置 agreementAccepted=false');
|
||||
debugPrint('已设置 showGuideOnStartup=false');
|
||||
debugPrint('已设置 appVersion=${AppConfig.appVersionCode}');
|
||||
}
|
||||
|
||||
// 4. 验证设置是否正确
|
||||
final bool firstLaunch = prefs.getBool(AppConfig.keyFirstLaunch) ?? true;
|
||||
final bool agreementAccepted =
|
||||
prefs.getBool(AppConfig.keyAgreementAccepted) ?? false;
|
||||
final int? appVersion = prefs.getInt(AppConfig.keyAppVersion);
|
||||
final allKeys = prefs.getKeys();
|
||||
|
||||
debugPrint('--- 验证配置 ---');
|
||||
debugPrint('firstLaunch: $firstLaunch');
|
||||
debugPrint('agreementAccepted: $agreementAccepted');
|
||||
debugPrint('appVersion: $appVersion');
|
||||
debugPrint('所有配置项: $allKeys');
|
||||
|
||||
// 5. 强制检查引导页状态
|
||||
GuideCheckResult? guideCheckResult;
|
||||
try {
|
||||
guideCheckResult = await ForceGuideChecker.checkAndValidate(prefs);
|
||||
debugPrint('引导页检查结果: ${guideCheckResult.needGuide ? '需要引导页' : '不需要引导页'}');
|
||||
debugPrint('原因: ${guideCheckResult.reason}');
|
||||
} catch (e) {
|
||||
debugPrint('引导页检查异常: $e');
|
||||
}
|
||||
|
||||
// 6. 设置屏幕适配
|
||||
ScreenAdapter.setPreferredOrientations();
|
||||
ScreenAdapter.enableSystemUIOverlay();
|
||||
ScreenAdapter.setSystemUIOverlayStyle(statusBarColor: Colors.transparent);
|
||||
|
||||
// 7. 获取初始路由
|
||||
String initialRoute = await AppRoutes.getInitialRoute(prefs);
|
||||
|
||||
debugPrint('--- 最终路由 ---');
|
||||
debugPrint('initialRoute: $initialRoute');
|
||||
debugPrint('========================================');
|
||||
|
||||
runApp(MyApp(initialRoute: initialRoute, guideCheckResult: guideCheckResult));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
final String initialRoute;
|
||||
final GuideCheckResult? guideCheckResult;
|
||||
|
||||
const MyApp({super.key, required this.initialRoute, this.guideCheckResult});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: AppConstants.appName,
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: AppTheme.lightTheme,
|
||||
darkTheme: AppTheme.darkTheme,
|
||||
themeMode: ThemeMode.system,
|
||||
initialRoute: initialRoute,
|
||||
onGenerateRoute: AppRoutes.generateRoute,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user