main.dart 7.7 KB

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