2026-02-26 13:38:39 +08:00

85 lines
2.8 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'dart:async';
import 'dart:ui' show PlatformDispatcher;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'core/router/app_router.dart';
import 'core/services/log_center_service.dart';
import 'theme/app_theme.dart';
void main() {
runZonedGuarded(() {
WidgetsFlutterBinding.ensureInitialized();
final container = ProviderContainer();
final logCenter = container.read(logCenterServiceProvider);
// 捕获 Flutter 框架错误Widget build 异常等)
FlutterError.onError = (details) {
FlutterError.presentError(details); // 保留控制台输出
logCenter.reportFlutterError(details);
};
// 捕获非 Flutter 框架的平台异常
PlatformDispatcher.instance.onError = (error, stack) {
logCenter.reportUncaughtError(error, stack);
return true;
};
runApp(UncontrolledProviderScope(
container: container,
child: const AirhubApp(),
));
}, (error, stack) {
// Zone 兜底:捕获 runZonedGuarded 区域内的未处理异步异常
// 此处 container 不可用,直接静态发送
LogCenterService().reportUncaughtError(error, stack);
});
}
class AirhubApp extends ConsumerWidget {
const AirhubApp({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final router = ref.watch(goRouterProvider);
return MaterialApp.router(
routerConfig: router,
title: 'Airhub',
debugShowCheckedModeBanner: false,
theme: AppTheme.lightTheme,
locale: const Locale('zh', 'CN'),
supportedLocales: const [
Locale('zh', 'CN'),
Locale('en', 'US'),
],
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
// Web 调试时模拟 iPhone 安全区域(刘海 + 底部 Home 条)
// 真机上此代码不生效kIsWeb 在真机编译时为 false
builder: kIsWeb
? (context, child) {
// 模拟 iPhone 12 Pro 安全区: top=47px, bottom=34px
const simTop = 47.0;
const simBottom = 34.0;
final mq = MediaQuery.of(context);
return MediaQuery(
data: mq.copyWith(
padding: mq.padding.copyWith(
top: mq.padding.top < simTop ? simTop : mq.padding.top,
bottom: mq.padding.bottom < simBottom
? simBottom
: mq.padding.bottom,
),
),
child: child ?? const SizedBox.shrink(),
);
}
: null,
);
}
}