main.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import 'dart:io';
  2. import 'dart:async';
  3. import 'package:amap_map_fluttify/amap_map_fluttify.dart';
  4. import 'package:fluro/fluro.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:flutter_bugly/flutter_bugly.dart';
  8. import 'package:flutter_localizations/flutter_localizations.dart';
  9. import 'package:liftmanager/internal/home/splash_page.dart';
  10. import 'package:liftmanager/provider/theme_provider.dart';
  11. import 'package:liftmanager/routers/application.dart';
  12. import 'package:liftmanager/routers/routers.dart';
  13. import 'package:liftmanager/utils/toast.dart';
  14. import 'package:oktoast/oktoast.dart';
  15. import 'package:provider/provider.dart';
  16. import 'package:liftmanager/internal/bbs/provide/websocket.dart';
  17. import 'package:flutter_picker/flutter_picker.dart';
  18. // import 'package:amap_location_fluttify/amap_location_fluttify.dart';
  19. import 'package:fake_push/fake_push.dart';
  20. import 'package:uni_links/uni_links.dart';
  21. import 'package:liftmanager/routers/fluro_navigator.dart';
  22. void main() {
  23. WidgetsFlutterBinding.ensureInitialized();
  24. FlutterBugly.postCatchedException(() async {
  25. // 强制竖屏
  26. SystemChrome.setPreferredOrientations(
  27. [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  28. FlutterBugly.init(androidAppId: "ae4e3d567d", iOSAppId: "4f09cdc82c");
  29. // 透明状态栏
  30. if (Platform.isAndroid) {
  31. SystemUiOverlayStyle systemUiOverlayStyle =
  32. SystemUiOverlayStyle(statusBarColor: Colors.transparent);
  33. SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  34. }
  35. await AmapService.init(
  36. iosKey: '7724c41be3ebe0cb7e5db4d79edc51b7',
  37. androidKey: '9eaae78b5b6b4ba78460b2e2539b798d');
  38. //下面是之前测试的
  39. // await AmapService.init(
  40. // iosKey: '7724c41be3ebe0cb7e5db4d79edc51b7',
  41. // androidKey: '28e7c3353ae0d66cdda8650a02d685c5');
  42. /// Fluttify引擎的日志
  43. await enableFluttifyLog(false); // 关闭log
  44. // runApp(
  45. // MultiProvider(
  46. // providers: [
  47. // // 环境配置
  48. // ChangeNotifierProvider(
  49. // create: (BuildContext context) => WebSocketProvide(),
  50. // ),
  51. // ],
  52. // child: MyApp(), // App 挂载
  53. // ),
  54. // );
  55. runZoned(() {
  56. runApp(
  57. MultiProvider(
  58. providers: [
  59. // 环境配置
  60. ChangeNotifierProvider(
  61. create: (BuildContext context) => WebSocketProvide(),
  62. ),
  63. ],
  64. child: MyApp(), // App 挂载
  65. ),
  66. );
  67. }, onError: (Object error, StackTrace stack) {
  68. // print(error);
  69. // print(stack);
  70. });
  71. });
  72. }
  73. class MyApp extends StatefulWidget {
  74. final Widget home;
  75. MyApp({this.home}) {
  76. final router = Router();
  77. Routers.configureRouters(router);
  78. Application.router = router;
  79. }
  80. @override
  81. _MyAppState createState() => _MyAppState();
  82. }
  83. class _MyAppState extends State<MyApp> {
  84. // class _MyAppState extends State<MyApp> with AmapLocationDisposeMixin {
  85. Push _push = Push();
  86. StreamSubscription<String> _receiveDeviceToken;
  87. StreamSubscription<Message> _receiveMessage;
  88. StreamSubscription<Message> _receiveNotification;
  89. StreamSubscription<String> _launchNotification;
  90. StreamSubscription<String> _resumeNotification;
  91. @override
  92. void initState() {
  93. // TODO: implement initState
  94. super.initState();
  95. _receiveDeviceToken =
  96. _push.receiveDeviceToken().listen(_handleReceiveDeviceToken);
  97. _receiveMessage = _push.receiveMessage().listen(_handleReceiveMessage);
  98. _receiveNotification =
  99. _push.receiveNotification().listen(_handleReceiveNotification);
  100. _launchNotification =
  101. _push.launchNotification().listen(_handleLaunchNotification);
  102. _resumeNotification =
  103. _push.resumeNotification().listen(_handleResumeNotification);
  104. _push.areNotificationsEnabled().then((l){
  105. print("是否开启通知:$l");
  106. });
  107. // _push.openNotificationsSettings();
  108. _push.startWork(enableDebug: false);
  109. }
  110. void _handleReceiveDeviceToken(String deviceToken) async {
  111. if(deviceToken != null){
  112. print('receiveDeviceToken: $deviceToken - ${await _push.getDeviceToken()}');
  113. _showTips('receiveDeviceToken', deviceToken);
  114. }
  115. }
  116. void _handleReceiveMessage(Message message) {
  117. print(
  118. 'receiveMessage: ${message.title} - ${message.content} - ${message.customContent}');
  119. _showTips('receiveMessage',
  120. '${message.title} - ${message.content} - ${message.customContent}');
  121. }
  122. void _handleReceiveNotification(Message notification) {
  123. print(
  124. 'receiveNotification: ${notification.title} - ${notification.content} - ${notification.customContent}');
  125. _showTips('receiveNotification',
  126. '${notification.title} - ${notification.content} - ${notification.customContent}');
  127. }
  128. void _handleLaunchNotification(String customContent) {
  129. print('launchNotification: $customContent');
  130. _showTips('launchNotification', customContent);
  131. }
  132. void _handleResumeNotification(String customContent) {
  133. print('resumeNotification: $customContent');
  134. _showTips('resumeNotification', customContent);
  135. }
  136. void _showTips(String title, String content) {
  137. // showToast(content);
  138. }
  139. @override
  140. void dispose() {
  141. if (_receiveDeviceToken != null) {
  142. _receiveDeviceToken.cancel();
  143. }
  144. if (_receiveMessage != null) {
  145. _receiveMessage.cancel();
  146. }
  147. if (_receiveNotification != null) {
  148. _receiveNotification.cancel();
  149. }
  150. if (_launchNotification != null) {
  151. _launchNotification.cancel();
  152. }
  153. if (_resumeNotification != null) {
  154. _resumeNotification.cancel();
  155. }
  156. super.dispose();
  157. }
  158. @override
  159. Widget build(BuildContext context) {
  160. return OKToast(
  161. child: ChangeNotifierProvider<ThemeProvider>(
  162. create: (_) => ThemeProvider(),
  163. child: Consumer<ThemeProvider>(
  164. builder: (_, provider, __) {
  165. return MaterialApp(
  166. title: '电梯管家',
  167. //showPerformanceOverlay: true, //显示性能标签
  168. //debugShowCheckedModeBanner: false,
  169. //checkerboardRasterCacheImages: true,
  170. theme: provider.getTheme(),
  171. darkTheme: provider.getTheme(isDarkMode: true),
  172. home: widget.home ?? SplashPage(),
  173. onGenerateRoute: Application.router.generator,
  174. localizationsDelegates: const [
  175. GlobalMaterialLocalizations.delegate,
  176. GlobalWidgetsLocalizations.delegate,
  177. GlobalCupertinoLocalizations.delegate,
  178. ],
  179. supportedLocales: const [
  180. // Locale('zh', 'CH'),
  181. // Locale('en', 'US')
  182. const Locale('zh', 'Hans'), // China
  183. const Locale('zh', ''), // China
  184. ]);
  185. },
  186. ),
  187. ),
  188. backgroundColor: Colors.black54,
  189. textPadding:
  190. const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
  191. radius: 20.0,
  192. position: ToastPosition.bottom);
  193. }
  194. }