flutter_integrated_whb/lib/main.dart

138 lines
4.1 KiB
Dart
Raw Normal View History

2025-07-11 11:03:21 +08:00
import 'package:flutter/material.dart';
2025-08-07 17:33:16 +08:00
import 'package:qhd_prevention/pages/badge_manager.dart';
2025-08-19 11:06:16 +08:00
import 'package:qhd_prevention/tools/auth_service.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';
2025-08-08 10:52:15 +08:00
import 'package:flutter_easyloading/flutter_easyloading.dart';
2025-07-18 17:13:38 +08:00
// 全局导航键
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 {
WidgetsFlutterBinding.ensureInitialized();
2025-08-08 10:52:15 +08:00
// 初始化 EasyLoading
EasyLoading.instance
2025-08-11 17:40:03 +08:00
..displayDuration = const Duration(seconds: 20)
2025-08-19 11:06:16 +08:00
..indicatorType = EasyLoadingIndicatorType.ring
..loadingStyle = EasyLoadingStyle.custom
..indicatorSize = 36.0
..radius = 0
..progressColor = Colors.blue
2025-08-12 10:57:07 +08:00
..backgroundColor = Colors.grey.shade300
2025-08-19 11:06:16 +08:00
..indicatorColor = Colors.blue
..textColor = Colors.black
..userInteractions = false
2025-08-08 10:52:15 +08:00
..dismissOnTap = false;
2025-07-11 11:03:21 +08:00
await initializeDateFormatting('zh_CN', null);
2025-08-19 11:06:16 +08:00
// 初始化HTTP管理器未授权回调
2025-07-18 17:13:38 +08:00
HttpManager.onUnauthorized = () async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('isLoggedIn', false);
2025-08-19 11:06:16 +08:00
await prefs.remove('token');
2025-07-18 17:13:38 +08:00
navigatorKey.currentState?.pushNamedAndRemoveUntil(
'/login',
(route) => false,
);
Future.delayed(const Duration(milliseconds: 100), () {
GlobalMessage.showError('会话已过期,请重新登录');
});
};
2025-08-19 11:06:16 +08:00
// 自动登录逻辑
final prefs = await SharedPreferences.getInstance();
final savedLogin = prefs.getBool('isLoggedIn') ?? false;
bool isLoggedIn = false;
if (savedLogin) {
// 如果本地标记已登录,进一步验证 token 是否有效
try {
isLoggedIn = await AuthService.isLoggedIn();
// 这里建议 AuthService.isLoggedIn() 内部实际请求一次用户信息接口
// 如果失败,就返回 false
} catch (e) {
isLoggedIn = false;
}
}
2025-07-11 11:03:21 +08:00
runApp(MyApp(isLoggedIn: isLoggedIn));
}
class MyApp extends StatelessWidget {
final bool isLoggedIn;
2025-08-19 11:06:16 +08:00
const MyApp({super.key, required this.isLoggedIn});
2025-07-11 11:03:21 +08:00
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '',
2025-08-19 11:06:16 +08:00
navigatorKey: navigatorKey,
2025-07-11 11:03:21 +08:00
builder: (context, child) {
2025-08-08 10:52:15 +08:00
return EasyLoading.init(
builder: (context, widget) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
FocusScope.of(context).unfocus();
},
child: widget,
);
2025-07-11 11:03:21 +08:00
},
2025-08-08 10:52:15 +08:00
)(context, child);
2025-07-11 11:03:21 +08:00
},
theme: ThemeData(
dividerTheme: const DividerThemeData(
2025-07-22 13:34:34 +08:00
color: Colors.black12,
2025-08-19 11:06:16 +08:00
thickness: .5,
indent: 0,
endIndent: 0,
2025-07-11 11:03:21 +08:00
),
primarySwatch: Colors.blue,
2025-08-19 11:06:16 +08:00
scaffoldBackgroundColor: const Color(0xFFF1F1F1),
2025-07-11 11:03:21 +08:00
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-08-08 10:52:15 +08:00
progressIndicatorTheme: const ProgressIndicatorThemeData(
2025-08-19 11:06:16 +08:00
color: Colors.blue,
2025-07-22 13:34:34 +08:00
),
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
);
}
2025-08-19 11:06:16 +08:00
}