main.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import 'dart:async';
  2. import 'dart:io';
  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/bbs/provide/websocket.dart';
  10. import 'package:liftmanager/internal/home/splash_page.dart';
  11. import 'package:liftmanager/provider/theme_provider.dart';
  12. import 'package:liftmanager/routers/application.dart';
  13. import 'package:liftmanager/routers/routers.dart';
  14. import 'package:oktoast/oktoast.dart';
  15. import 'package:provider/provider.dart';
  16. import 'package:jpush_flutter/jpush_flutter.dart';
  17. import 'package:umeng_common_sdk/umeng_common_sdk.dart';
  18. void main() {
  19. WidgetsFlutterBinding.ensureInitialized();
  20. FlutterBugly.postCatchedException(() async {
  21. // 强制竖屏
  22. SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  23. // OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
  24. FlutterBugly.init(androidAppId: "ae4e3d567d", iOSAppId: "4f09cdc82c");
  25. UmengCommonSdk.initCommon(
  26. '606d0db3de41b946ab3fe2bf', '60711efede41b946ab45daad', 'App Store');
  27. UmengCommonSdk.setPageCollectionModeManual();
  28. // 透明状态栏
  29. if (Platform.isAndroid) {
  30. SystemUiOverlayStyle systemUiOverlayStyle =
  31. SystemUiOverlayStyle(statusBarColor: Colors.transparent);
  32. SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  33. }
  34. await AmapService.init(
  35. iosKey: '7724c41be3ebe0cb7e5db4d79edc51b7',
  36. androidKey: '9eaae78b5b6b4ba78460b2e2539b798d');
  37. /// Fluttify引擎的日志
  38. await enableFluttifyLog(false); // 关闭log
  39. runZoned(
  40. () {
  41. runApp(
  42. MultiProvider(
  43. providers: [
  44. ChangeNotifierProvider(
  45. create: (BuildContext context) => WebSocketProvide(),
  46. ),
  47. ],
  48. child: MyApp(),
  49. ),
  50. );
  51. },
  52. onError: (Object error, StackTrace stack) {
  53. print('In-zoned error: $error');
  54. print('In-zoned stack: $stack');
  55. },
  56. );
  57. });
  58. }
  59. final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
  60. class MyApp extends StatefulWidget {
  61. final Widget home;
  62. MyApp({this.home}) {
  63. final router = FluroRouter();
  64. Routers.configureRouters(router);
  65. Application.router = router;
  66. }
  67. @override
  68. _MyAppState createState() => _MyAppState();
  69. }
  70. class _MyAppState extends State<MyApp> {
  71. // class _MyAppState extends State<MyApp> with AmapLocationDisposeMixin {
  72. StreamSubscription<String> _receiveDeviceToken;
  73. // StreamSubscription<Message> _receiveMessage;
  74. // StreamSubscription<Message> _receiveNotification;
  75. StreamSubscription<String> _launchNotification;
  76. StreamSubscription<String> _resumeNotification;
  77. final JPush jpush = JPush();
  78. @override
  79. void initState() {
  80. // TODO: implement initState
  81. super.initState();
  82. // _receiveDeviceToken =
  83. // _push.receiveDeviceToken().listen(_handleReceiveDeviceToken);
  84. // _receiveMessage = _push.receiveMessage().listen(_handleReceiveMessage);
  85. // _receiveNotification =
  86. // _push.receiveNotification().listen(_handleReceiveNotification);
  87. // _launchNotification =
  88. // _push.launchNotification().listen(_handleLaunchNotification);
  89. // _resumeNotification =
  90. // _push.resumeNotification().listen(_handleResumeNotification);
  91. // _push.areNotificationsEnabled().then((l) {
  92. // print("是否开启通知:$l");
  93. // });
  94. // UmengCommonSdk.setPageCollectionModeAuto();
  95. //
  96. // UmengCommonSdk.initCommon(
  97. // '606d0db3de41b946ab3fe2bf', '60711efede41b946ab45daad', 'App Store');
  98. //
  99. // UmengCommonSdk.setPageCollectionModeManual();
  100. // _push.startWork(enableDebug: false);
  101. try {
  102. jpush.addEventHandler(
  103. onReceiveNotification: (Map<String, dynamic> message) async {
  104. print("flutter onReceiveNotification: $message");
  105. }, onOpenNotification: (Map<String, dynamic> message) async {
  106. print("flutter onOpenNotification: $message");
  107. }, onReceiveMessage: (Map<String, dynamic> message) async {
  108. print("flutter onReceiveMessage: $message");
  109. }, onReceiveNotificationAuthorization:
  110. (Map<String, dynamic> message) async {
  111. print("flutter onReceiveNotificationAuthorization: $message");
  112. });
  113. } on PlatformException {
  114. print('Failed to get platform version.');
  115. }
  116. jpush.setup(
  117. appKey: "bd76ee7fb5dac9d04b12a521",
  118. // channel: "theChannel",
  119. production: false,
  120. debug: true,
  121. );
  122. jpush.applyPushAuthority(
  123. NotificationSettingsIOS(sound: true, alert: true, badge: false));
  124. // Platform messages may fail, so we use a try/catch PlatformException.
  125. jpush.getRegistrationID().then((rid) {
  126. print("flutter get registration id : $rid");
  127. });
  128. }
  129. // void _handleReceiveDeviceToken(String deviceToken) async {
  130. // if (deviceToken != null) {
  131. // print(
  132. // 'receiveDeviceToken: $deviceToken - ${await _push.getDeviceToken()}');
  133. // _showTips('receiveDeviceToken', deviceToken);
  134. // }
  135. // }
  136. //
  137. // void _handleReceiveMessage(Message message) {
  138. // print(
  139. // 'receiveMessage: ${message.title} - ${message.content} - ${message.customContent}');
  140. // _showTips('receiveMessage',
  141. // '${message.title} - ${message.content} - ${message.customContent}');
  142. // }
  143. //
  144. // void _handleReceiveNotification(Message notification) {
  145. // print(
  146. // 'receiveNotification: ${notification.title} - ${notification.content} - ${notification.customContent}');
  147. // _showTips('receiveNotification',
  148. // '${notification.title} - ${notification.content} - ${notification.customContent}');
  149. // }
  150. void _handleLaunchNotification(String customContent) {
  151. print('launchNotification: $customContent');
  152. _showTips('launchNotification', customContent);
  153. }
  154. void _handleResumeNotification(String customContent) {
  155. print('resumeNotification: $customContent');
  156. _showTips('resumeNotification', customContent);
  157. }
  158. void _showTips(String title, String content) {
  159. // showToast(content);
  160. }
  161. @override
  162. void dispose() {
  163. if (_receiveDeviceToken != null) {
  164. _receiveDeviceToken.cancel();
  165. }
  166. // if (_receiveMessage != null) {
  167. // _receiveMessage.cancel();
  168. // }
  169. // if (_receiveNotification != null) {
  170. // _receiveNotification.cancel();
  171. // }
  172. if (_launchNotification != null) {
  173. _launchNotification.cancel();
  174. }
  175. if (_resumeNotification != null) {
  176. _resumeNotification.cancel();
  177. }
  178. super.dispose();
  179. }
  180. @override
  181. Widget build(BuildContext context) {
  182. return OKToast(
  183. child: ChangeNotifierProvider<ThemeProvider>(
  184. create: (_) => ThemeProvider(),
  185. child: Consumer<ThemeProvider>(
  186. builder: (_, provider, __) {
  187. return MaterialApp(
  188. navigatorKey: navigatorKey,
  189. title: '电梯管家',
  190. //showPerformanceOverlay: true, //显示性能标签
  191. debugShowCheckedModeBanner: false,
  192. //checkerboardRasterCacheImages: true,
  193. theme: provider.getTheme(),
  194. darkTheme: provider.getTheme(isDarkMode: true),
  195. home: widget.home ?? SplashPage(),
  196. onGenerateRoute: Application.router.generator,
  197. localizationsDelegates: const [
  198. GlobalMaterialLocalizations.delegate,
  199. GlobalWidgetsLocalizations.delegate,
  200. GlobalCupertinoLocalizations.delegate,
  201. ],
  202. supportedLocales: const [
  203. // Locale('zh', 'CH'),
  204. // Locale('en', 'US')
  205. const Locale('zh', 'Hans'), // China
  206. const Locale('zh', ''), // China
  207. ]);
  208. },
  209. ),
  210. ),
  211. backgroundColor: Colors.black54,
  212. textPadding:
  213. const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
  214. radius: 20.0,
  215. position: ToastPosition.bottom);
  216. }
  217. }