123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import 'dart:async';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter_localizations/flutter_localizations.dart';
- import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
- import 'package:logger/logger.dart';
- import 'package:lszlgl/config/colors.dart';
- import 'package:lszlgl/drfit/database.dart';
- import 'package:lszlgl/router/my_navigator.dart';
- import 'config/app_config.dart';
- late Logger logger;
- late MyDatabase database;
- void main() async {
- logger = Logger(printer: PrettyPrinter(methodCount: 0));
- initReportException(() async {
- SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(statusBarColor: Colors.transparent));
- SystemChrome.setPreferredOrientations([
- DeviceOrientation.portraitUp,
- DeviceOrientation.portraitDown,
- ]);
- AppConfig.init().then((value) {
- runApp(const MyApp());
- });
- database = MyDatabase();
- });
- }
- /// 初始化异常上报 封装[runZonedGuarded]
- void initReportException(Function() callback) {
- runZonedGuarded(() {
- // 初始化Flutter
- WidgetsFlutterBinding.ensureInitialized();
- // 隔离线程异常
- // Isolate.current.addErrorListener(RawReceivePort((dynamic pair) {
- // var isolateError = pair as List<dynamic>;
- // onError(isolateError.first, isolateError.last);
- // }).sendPort);
- // 捕获Flutter异常
- FlutterError.onError = (FlutterErrorDetails error) {
- FlutterError.presentError(error);
- onError(error.exception, error.stack);
- };
- // 回调外部方法
- callback.call();
- }, (error, stack) {
- // 捕获程序异常
- onError(error, stack);
- });
- }
- /// 上报异常
- Future<void> onError(Object exception, StackTrace? stack) async {
- if (stack == null) return;
- logger.e('error:${exception.runtimeType.toString()}\n${exception.toString()}\n${stack.toString()}');
- }
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- @override
- Widget build(BuildContext context) {
- var theme = Theme.of(context);
- return MaterialApp(
- title: AppConfig.packageInfo.appName,
- theme: ThemeData(
- colorScheme: ColorScheme.fromSeed(
- seedColor: Colors.blue,
- ),
- scaffoldBackgroundColor: MyColor.c_background,
- // navigationBarTheme: const NavigationBarThemeData(height: 56),
- appBarTheme: const AppBarTheme(
- centerTitle: true,
- foregroundColor: Colors.white,
- backgroundColor: Colors.transparent,
- systemOverlayStyle: SystemUiOverlayStyle.light,
- ),
- bottomSheetTheme: theme.bottomSheetTheme.copyWith(
- backgroundColor: Colors.white,
- modalBackgroundColor: Colors.white,
- surfaceTintColor: Colors.white,
- ),
- dialogTheme: theme.dialogTheme.copyWith(
- backgroundColor: Colors.white,
- surfaceTintColor: Colors.white,
- titleTextStyle: const TextStyle(
- fontSize: 16,
- color: MyColor.c_333333,
- fontWeight: FontWeight.w500,
- ),
- contentTextStyle: const TextStyle(
- fontSize: 14,
- color: MyColor.c_666666,
- ),
- ),
- useMaterial3: true,
- ),
- builder: FlutterSmartDialog.init(),
- localizationsDelegates: const [
- GlobalMaterialLocalizations.delegate,
- GlobalCupertinoLocalizations.delegate,
- GlobalWidgetsLocalizations.delegate,
- ],
- supportedLocales: const [Locale('zh')],
- navigatorKey: MyNavigator.navigator,
- onGenerateRoute: MyNavigator.generateRoute,
- navigatorObservers: [
- MyNavigator.navigatorObs,
- MyNavigator.routeObs,
- FlutterSmartDialog.observer,
- ],
- );
- }
- }
|