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; // 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 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, ], ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { int _counter = 0; void _incrementCounter() { setState(() { _counter++; }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), ); } }