151 lines
4.6 KiB
Dart
151 lines
4.6 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:mom_kitchen/src/l10n/app_localizations.dart';
|
|
|
|
// 📋 页面注册系统 - 管理路由和页面定义
|
|
// 文件: lib/src/config/app_routes.dart
|
|
import 'package:mom_kitchen/src/config/app_routes.dart';
|
|
|
|
import 'package:mom_kitchen/src/services/core/app_service.dart';
|
|
import 'package:mom_kitchen/src/services/orientation_service.dart';
|
|
import 'package:mom_kitchen/src/services/ui/theme_service.dart';
|
|
import 'package:mom_kitchen/src/services/ui/toast_service.dart';
|
|
|
|
// 📋 页面注册表和验证器 - 管理页面注册和规范验证
|
|
// 文件: lib/src/standards/page_validator.dart
|
|
import 'package:mom_kitchen/src/standards/page_validator.dart';
|
|
|
|
// 📋 全局 Binding - 统一管理 Controller 和 Service 生命周期
|
|
import 'package:mom_kitchen/src/app_binding.dart';
|
|
|
|
import 'package:mom_kitchen/src/utils/app_logger.dart';
|
|
|
|
void main() async {
|
|
// 确保 Flutter 引擎初始化
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
FlutterError.onError = (FlutterErrorDetails details) {
|
|
FlutterError.presentError(details);
|
|
debugPrint('=== FLUTTER ERROR STACK TRACE ===');
|
|
debugPrint(details.stack.toString());
|
|
debugPrint('=== END STACK TRACE ===');
|
|
};
|
|
|
|
// 初始化所有服务
|
|
await AppService.instance.init();
|
|
|
|
// 📋 页面注册入口 - 在此处调用注册所有页面
|
|
if (kDebugMode) {
|
|
AppLogger.i('📋 开始注册页面...');
|
|
AppRoutes.registerAllPages();
|
|
AppLogger.i('✅ 页面注册完成,共注册 ${PageRegistry.pageCount} 个页面');
|
|
|
|
final config = PageRegistry.exportConfig();
|
|
AppLogger.d('📊 页面配置: $config');
|
|
}
|
|
|
|
await OrientationService().unlockOrientation();
|
|
|
|
// 运行应用
|
|
runApp(const MyApp());
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final themeService = Get.put(ThemeService.instance);
|
|
|
|
return Obx(() {
|
|
final textScale = themeService.fontSize.value / 16.0;
|
|
return GetCupertinoApp(
|
|
title: 'Mom\'s Kitchen',
|
|
key: ValueKey(
|
|
'theme_${themeService.primaryColor.value.toARGB32()}_${themeService.isDarkMode.value}_${themeService.fontSize.value}',
|
|
),
|
|
theme: themeService.cupertinoThemeData,
|
|
locale: Locale(themeService.currentLocale.value),
|
|
debugShowCheckedModeBanner: false,
|
|
localizationsDelegates: const [
|
|
AppLocalizations.delegate,
|
|
GlobalCupertinoLocalizations.delegate,
|
|
GlobalMaterialLocalizations.delegate,
|
|
GlobalWidgetsLocalizations.delegate,
|
|
],
|
|
supportedLocales: const [
|
|
Locale('en'),
|
|
Locale('zh'),
|
|
Locale('zh', 'Hant'),
|
|
],
|
|
getPages: AppRoutes.pages,
|
|
initialBinding: AppBinding(),
|
|
builder: (context, widget) {
|
|
final theme = ThemeService.instance;
|
|
return MediaQuery(
|
|
data: MediaQuery.of(
|
|
context,
|
|
).copyWith(textScaler: TextScaler.linear(textScale)),
|
|
child: ColoredBox(
|
|
color: theme.backgroundColor.value,
|
|
child: widget!,
|
|
),
|
|
);
|
|
},
|
|
routingCallback: (routing) {
|
|
if (kDebugMode && routing?.current != null) {
|
|
AppLogger.d('🔍 路由变化: ${routing?.previous} → ${routing?.current}');
|
|
}
|
|
},
|
|
home: const _InitWrapper(),
|
|
);
|
|
});
|
|
}
|
|
}
|
|
|
|
class _InitWrapper extends StatefulWidget {
|
|
const _InitWrapper();
|
|
|
|
@override
|
|
State<_InitWrapper> createState() => _InitWrapperState();
|
|
}
|
|
|
|
class _InitWrapperState extends State<_InitWrapper>
|
|
with WidgetsBindingObserver {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (mounted) {
|
|
ToastService.init(context);
|
|
Get.offAllNamed(AppRoutes.main);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didChangePlatformBrightness() {
|
|
super.didChangePlatformBrightness();
|
|
final brightness =
|
|
WidgetsBinding.instance.platformDispatcher.platformBrightness;
|
|
final themeService = Get.find<ThemeService>();
|
|
themeService.onSystemBrightnessChanged(brightness);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const CupertinoPageScaffold(
|
|
child: Center(child: CupertinoActivityIndicator()),
|
|
);
|
|
}
|
|
}
|