2025-07-11 11:03:21 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2025-07-18 17:13:38 +08:00
|
|
|
import 'package:qhd_prevention/pages/home/study/study_detail_page.dart';
|
2025-07-11 11:03:21 +08:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2025-07-18 17:13:38 +08:00
|
|
|
import './pages/login_page.dart';
|
|
|
|
import './pages/main_tab.dart';
|
2025-07-11 11:03:21 +08:00
|
|
|
import 'package:intl/date_symbol_data_local.dart';
|
2025-07-18 17:13:38 +08:00
|
|
|
import 'http/HttpManager.dart';
|
|
|
|
|
|
|
|
// 全局导航键
|
|
|
|
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
|
|
|
|
|
|
|
// 全局消息控制器
|
|
|
|
class GlobalMessage {
|
|
|
|
static void showError(String message) {
|
|
|
|
final context = navigatorKey.currentContext;
|
|
|
|
if (context != null) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(message),
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
duration: const Duration(seconds: 3),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-07-11 11:03:21 +08:00
|
|
|
|
|
|
|
void main() async {
|
|
|
|
// 确保Flutter引擎初始化完成
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
await initializeDateFormatting('zh_CN', null);
|
|
|
|
|
|
|
|
// 获取共享首选项实例
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
|
|
|
|
// 获取登录状态
|
|
|
|
final isLoggedIn = prefs.getBool('isLoggedIn') ?? false;
|
|
|
|
|
2025-07-18 17:13:38 +08:00
|
|
|
// 初始化HTTP管理器
|
|
|
|
HttpManager.onUnauthorized = () async {
|
|
|
|
// 清除登录状态
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
await prefs.setBool('isLoggedIn', false);
|
|
|
|
await prefs.remove('token'); // 如果有token的话
|
|
|
|
|
|
|
|
// 导航到登录页
|
|
|
|
navigatorKey.currentState?.pushNamedAndRemoveUntil(
|
|
|
|
'/login',
|
|
|
|
(route) => false,
|
|
|
|
);
|
|
|
|
|
|
|
|
// 等一帧让页面构建完成
|
|
|
|
Future.delayed(const Duration(milliseconds: 100), () {
|
|
|
|
GlobalMessage.showError('会话已过期,请重新登录');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2025-07-11 11:03:21 +08:00
|
|
|
runApp(MyApp(isLoggedIn: isLoggedIn));
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
|
final bool isLoggedIn;
|
|
|
|
|
|
|
|
const MyApp({super.key, required this.isLoggedIn});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
title: '',
|
2025-07-18 17:13:38 +08:00
|
|
|
navigatorKey: navigatorKey, // 设置全局导航键
|
2025-07-11 11:03:21 +08:00
|
|
|
builder: (context, child) {
|
|
|
|
return GestureDetector(
|
|
|
|
behavior: HitTestBehavior.translucent, // 让空白区域也能点击
|
|
|
|
onTap: () {
|
|
|
|
// 收起键盘
|
|
|
|
FocusScope.of(context).unfocus();
|
|
|
|
},
|
|
|
|
child: child,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
theme: ThemeData(
|
|
|
|
dividerTheme: const DividerThemeData(
|
|
|
|
color: Color(0xF1F1F1FF),
|
|
|
|
thickness: 1, // 线高
|
|
|
|
indent: 0, // 左缩进
|
|
|
|
endIndent: 0, // 右缩进
|
|
|
|
),
|
|
|
|
primarySwatch: Colors.blue,
|
|
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
|
|
|
inputDecorationTheme: const InputDecorationTheme(
|
|
|
|
border: InputBorder.none,
|
|
|
|
contentPadding: EdgeInsets.symmetric(horizontal: 8),
|
|
|
|
),
|
2025-07-18 17:13:38 +08:00
|
|
|
snackBarTheme: const SnackBarThemeData(
|
|
|
|
behavior: SnackBarBehavior.floating,
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(8)),
|
|
|
|
),
|
|
|
|
),
|
2025-07-11 11:03:21 +08:00
|
|
|
),
|
|
|
|
// 根据登录状态决定初始页面
|
|
|
|
home: isLoggedIn ? const MainPage() : const LoginPage(),
|
|
|
|
debugShowCheckedModeBanner: false,
|
2025-07-18 17:13:38 +08:00
|
|
|
routes: {
|
|
|
|
'/login': (_) => const LoginPage(),
|
|
|
|
// ... 其他路由
|
|
|
|
},
|
2025-07-11 11:03:21 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|