116 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Dart
		
	
	
			
		
		
	
	
			116 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Dart
		
	
	
| import 'package:flutter/material.dart';
 | |
| import 'package:shared_preferences/shared_preferences.dart';
 | |
| import './pages/login_page.dart';
 | |
| import './pages/main_tab.dart';
 | |
| import 'package:intl/date_symbol_data_local.dart';
 | |
| 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),
 | |
|         ),
 | |
|       );
 | |
|     }
 | |
|   }
 | |
| }
 | |
| 
 | |
| void main() async {
 | |
|   // 确保Flutter引擎初始化完成
 | |
|   WidgetsFlutterBinding.ensureInitialized();
 | |
|   await initializeDateFormatting('zh_CN', null);
 | |
| 
 | |
|   // 获取共享首选项实例
 | |
|   final prefs = await SharedPreferences.getInstance();
 | |
| 
 | |
|   // 获取登录状态
 | |
|   final isLoggedIn = prefs.getBool('isLoggedIn') ?? false;
 | |
| 
 | |
|   // 初始化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('会话已过期,请重新登录');
 | |
|     });
 | |
|   };
 | |
| 
 | |
|   runApp(MyApp(isLoggedIn: isLoggedIn));
 | |
| }
 | |
| 
 | |
| class MyApp extends StatelessWidget {
 | |
|   final bool isLoggedIn;
 | |
| 
 | |
|   MyApp({super.key, required this.isLoggedIn});
 | |
|   final FocusNode _blankFocusNode = FocusNode(); // 全局空焦点节点
 | |
| 
 | |
|   @override
 | |
|   Widget build(BuildContext context) {
 | |
|     return MaterialApp(
 | |
|       title: '',
 | |
|       navigatorKey: navigatorKey, // 设置全局导航键
 | |
|       builder: (context, child) {
 | |
|         return GestureDetector(
 | |
|           behavior: HitTestBehavior.translucent, // 让空白区域也能点击
 | |
|           onTap: () {
 | |
|             // 收起键盘
 | |
|             FocusScope.of(context).unfocus();
 | |
|           },
 | |
|           child: child,
 | |
|         );
 | |
|       },
 | |
|       theme: ThemeData(
 | |
|         dividerTheme: const DividerThemeData(
 | |
|           color: Colors.black12,
 | |
|           thickness: .5,      // 线高
 | |
|           indent: 0,         // 左缩进
 | |
|           endIndent: 0,      // 右缩进
 | |
|         ),
 | |
|         primarySwatch: Colors.blue,
 | |
|         // 统一设置页面背景颜色
 | |
|         scaffoldBackgroundColor: const Color(0xFFF1F1F1), // 浅灰色背景
 | |
|         colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
 | |
|         inputDecorationTheme: const InputDecorationTheme(
 | |
|           border: InputBorder.none,
 | |
|           contentPadding: EdgeInsets.symmetric(horizontal: 8),
 | |
|         ),
 | |
|         snackBarTheme: const SnackBarThemeData(
 | |
|           behavior: SnackBarBehavior.floating,
 | |
|           shape: RoundedRectangleBorder(
 | |
|             borderRadius: BorderRadius.all(Radius.circular(8)),
 | |
|           ),
 | |
|         ),
 | |
|         progressIndicatorTheme: ProgressIndicatorThemeData(
 | |
|           color: Colors.blue, // 统一颜色
 | |
|         ),
 | |
|       ),
 | |
|       // 根据登录状态决定初始页面
 | |
|       home: isLoggedIn ? const MainPage() : const LoginPage(),
 | |
|       debugShowCheckedModeBanner: false,
 | |
|       routes: {
 | |
|         '/login': (_) => const LoginPage(),
 | |
| 
 | |
|         // ... 其他路由
 | |
|       },
 | |
|     );
 | |
|   }
 | |
| } |