main.dart 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_localizations/flutter_localizations.dart';
  5. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  6. import 'package:logger/logger.dart';
  7. import 'package:lszlgl/config/colors.dart';
  8. import 'package:lszlgl/drfit/database.dart';
  9. import 'package:lszlgl/router/my_navigator.dart';
  10. import 'config/app_config.dart';
  11. late Logger logger;
  12. late MyDatabase database;
  13. void main() async {
  14. logger = Logger(printer: PrettyPrinter(methodCount: 0));
  15. initReportException(() async {
  16. SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(statusBarColor: Colors.transparent));
  17. SystemChrome.setPreferredOrientations([
  18. DeviceOrientation.portraitUp,
  19. DeviceOrientation.portraitDown,
  20. ]);
  21. AppConfig.init().then((value) {
  22. runApp(const MyApp());
  23. });
  24. database = MyDatabase();
  25. });
  26. }
  27. /// 初始化异常上报 封装[runZonedGuarded]
  28. void initReportException(Function() callback) {
  29. runZonedGuarded(() {
  30. // 初始化Flutter
  31. WidgetsFlutterBinding.ensureInitialized();
  32. // 隔离线程异常
  33. // Isolate.current.addErrorListener(RawReceivePort((dynamic pair) {
  34. // var isolateError = pair as List<dynamic>;
  35. // onError(isolateError.first, isolateError.last);
  36. // }).sendPort);
  37. // 捕获Flutter异常
  38. FlutterError.onError = (FlutterErrorDetails error) {
  39. FlutterError.presentError(error);
  40. onError(error.exception, error.stack);
  41. };
  42. // 回调外部方法
  43. callback.call();
  44. }, (error, stack) {
  45. // 捕获程序异常
  46. onError(error, stack);
  47. });
  48. }
  49. /// 上报异常
  50. Future<void> onError(Object exception, StackTrace? stack) async {
  51. if (stack == null) return;
  52. logger.e('error:${exception.runtimeType.toString()}\n${exception.toString()}\n${stack.toString()}');
  53. }
  54. class MyApp extends StatelessWidget {
  55. const MyApp({super.key});
  56. @override
  57. Widget build(BuildContext context) {
  58. var theme = Theme.of(context);
  59. return MaterialApp(
  60. title: AppConfig.packageInfo.appName,
  61. theme: ThemeData(
  62. colorScheme: ColorScheme.fromSeed(
  63. seedColor: Colors.blue,
  64. ),
  65. scaffoldBackgroundColor: MyColor.c_background,
  66. // navigationBarTheme: const NavigationBarThemeData(height: 56),
  67. appBarTheme: const AppBarTheme(
  68. centerTitle: true,
  69. foregroundColor: Colors.white,
  70. backgroundColor: Colors.transparent,
  71. systemOverlayStyle: SystemUiOverlayStyle.light,
  72. ),
  73. bottomSheetTheme: theme.bottomSheetTheme.copyWith(
  74. backgroundColor: Colors.white,
  75. modalBackgroundColor: Colors.white,
  76. surfaceTintColor: Colors.white,
  77. ),
  78. dialogTheme: theme.dialogTheme.copyWith(
  79. backgroundColor: Colors.white,
  80. surfaceTintColor: Colors.white,
  81. titleTextStyle: const TextStyle(
  82. fontSize: 16,
  83. color: MyColor.c_333333,
  84. fontWeight: FontWeight.w500,
  85. ),
  86. contentTextStyle: const TextStyle(
  87. fontSize: 14,
  88. color: MyColor.c_666666,
  89. ),
  90. ),
  91. useMaterial3: true,
  92. ),
  93. builder: FlutterSmartDialog.init(),
  94. localizationsDelegates: const [
  95. GlobalMaterialLocalizations.delegate,
  96. GlobalCupertinoLocalizations.delegate,
  97. GlobalWidgetsLocalizations.delegate,
  98. ],
  99. supportedLocales: const [Locale('zh')],
  100. navigatorKey: MyNavigator.navigator,
  101. onGenerateRoute: MyNavigator.generateRoute,
  102. navigatorObservers: [
  103. MyNavigator.navigatorObs,
  104. MyNavigator.routeObs,
  105. FlutterSmartDialog.observer,
  106. ],
  107. );
  108. }
  109. }
  110. class MyHomePage extends StatefulWidget {
  111. const MyHomePage({super.key, required this.title});
  112. final String title;
  113. @override
  114. State<MyHomePage> createState() => _MyHomePageState();
  115. }
  116. class _MyHomePageState extends State<MyHomePage> {
  117. int _counter = 0;
  118. void _incrementCounter() {
  119. setState(() {
  120. _counter++;
  121. });
  122. }
  123. @override
  124. Widget build(BuildContext context) {
  125. return Scaffold(
  126. appBar: AppBar(
  127. backgroundColor: Theme.of(context).colorScheme.inversePrimary,
  128. title: Text(widget.title),
  129. ),
  130. body: Center(
  131. child: Column(
  132. mainAxisAlignment: MainAxisAlignment.center,
  133. children: <Widget>[
  134. const Text(
  135. 'You have pushed the button this many times:',
  136. ),
  137. Text(
  138. '$_counter',
  139. style: Theme.of(context).textTheme.headlineMedium,
  140. ),
  141. ],
  142. ),
  143. ),
  144. floatingActionButton: FloatingActionButton(
  145. onPressed: _incrementCounter,
  146. tooltip: 'Increment',
  147. child: const Icon(Icons.add),
  148. ),
  149. );
  150. }
  151. }