main.dart 4.8 KB

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