main.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:amap_location_flutter_plugin/amap_location_flutter_plugin.dart';
  4. import 'package:amap_location_flutter_plugin/amap_location_option.dart';
  5. import 'package:amap_map_fluttify/amap_map_fluttify.dart';
  6. import 'package:fake_push/fake_push.dart';
  7. import 'package:fluro/fluro.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter/services.dart';
  10. import 'package:flutter_bugly/flutter_bugly.dart';
  11. import 'package:flutter_localizations/flutter_localizations.dart';
  12. import 'package:liftmanager/internal/home/splash_page.dart';
  13. import 'package:liftmanager/provider/theme_provider.dart';
  14. import 'package:liftmanager/routers/application.dart';
  15. import 'package:liftmanager/routers/routers.dart';
  16. import 'package:liftmanager/utils/log_utils.dart';
  17. import 'package:oktoast/oktoast.dart';
  18. import 'package:provider/provider.dart' as p;
  19. void main() {
  20. WidgetsFlutterBinding.ensureInitialized();
  21. FlutterBugly.postCatchedException(() async {
  22. FlutterBugly.init(androidAppId: "ae4e3d567d", iOSAppId: "4f09cdc82c");
  23. // 透明状态栏
  24. if (Platform.isAndroid) {
  25. SystemUiOverlayStyle systemUiOverlayStyle =
  26. SystemUiOverlayStyle(statusBarColor: Colors.transparent);
  27. SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  28. }
  29. // AmapCore.init("7724c41be3ebe0cb7e5db4d79edc51b7");
  30. // await AmapService.init(iosKey: '7724c41be3ebe0cb7e5db4d79edc51b7', androidKey: '9eaae78b5b6b4ba78460b2e2539b798d');
  31. AmapLocationFlutterPlugin.setApiKey(
  32. "9eaae78b5b6b4ba78460b2e2539b798d", "7724c41be3ebe0cb7e5db4d79edc51b7");
  33. /// Fluttify引擎的日志
  34. await enableFluttifyLog(false); // 关闭log
  35. Log.init();
  36. runZoned(() {
  37. runApp(MyApp());
  38. }, onError: (Object error, StackTrace stack) {
  39. print(error);
  40. print(stack);
  41. });
  42. });
  43. }
  44. class MyApp extends StatefulWidget {
  45. final Widget home;
  46. MyApp({this.home}) {
  47. final router = Router();
  48. Routers.configureRouters(router);
  49. Application.router = router;
  50. }
  51. @override
  52. _MyAppState createState() => _MyAppState();
  53. }
  54. class _MyAppState extends State<MyApp> {
  55. // Map<String, Object> _locationResult;
  56. // StreamSubscription<Map<String, Object>> _locationListener;
  57. // AmapLocationFlutterPlugin _locationPlugin = new AmapLocationFlutterPlugin();
  58. Push _push = Push();
  59. StreamSubscription<String> _receiveDeviceToken;
  60. StreamSubscription<Message> _receiveMessage;
  61. StreamSubscription<Message> _receiveNotification;
  62. StreamSubscription<String> _launchNotification;
  63. StreamSubscription<String> _resumeNotification;
  64. @override
  65. void initState() {
  66. // TODO: implement initState
  67. super.initState();
  68. // _locationListener = _locationPlugin
  69. // .onLocationChanged()
  70. // .listen((Map<String, Object> result) {
  71. // setState(() {
  72. // _locationResult = result;
  73. // _locationResult.forEach((key, value) {
  74. // print('key:$key :');
  75. // print('value:$value :');
  76. // });
  77. // });
  78. // });
  79. // if (null != _locationPlugin) {
  80. // ///开始定位之前设置定位参数
  81. // _setLocationOption();
  82. // _locationPlugin.startLocation();
  83. // }
  84. // _setLocationOption();
  85. _receiveDeviceToken =
  86. _push.receiveDeviceToken().listen(_handleReceiveDeviceToken);
  87. _receiveMessage = _push.receiveMessage().listen(_handleReceiveMessage);
  88. _receiveNotification =
  89. _push.receiveNotification().listen(_handleReceiveNotification);
  90. _launchNotification =
  91. _push.launchNotification().listen(_handleLaunchNotification);
  92. _resumeNotification =
  93. _push.resumeNotification().listen(_handleResumeNotification);
  94. _push.areNotificationsEnabled().then((l){
  95. print("是否开启通知:$l");
  96. });
  97. // _push.openNotificationsSettings();
  98. _push.startWork(enableDebug: false);
  99. }
  100. void _handleReceiveDeviceToken(String deviceToken) async {
  101. if(deviceToken != null){
  102. print('receiveDeviceToken: $deviceToken - ${await _push.getDeviceToken()}');
  103. _showTips('receiveDeviceToken', deviceToken);
  104. }
  105. }
  106. void _handleReceiveMessage(Message message) {
  107. print(
  108. 'receiveMessage: ${message.title} - ${message.content} - ${message.customContent}');
  109. _showTips('receiveMessage',
  110. '${message.title} - ${message.content} - ${message.customContent}');
  111. }
  112. void _handleReceiveNotification(Message notification) {
  113. print(
  114. 'receiveNotification: ${notification.title} - ${notification.content} - ${notification.customContent}');
  115. _showTips('receiveNotification',
  116. '${notification.title} - ${notification.content} - ${notification.customContent}');
  117. }
  118. void _handleLaunchNotification(String customContent) {
  119. print('launchNotification: $customContent');
  120. _showTips('launchNotification', customContent);
  121. }
  122. void _handleResumeNotification(String customContent) {
  123. print('resumeNotification: $customContent');
  124. _showTips('resumeNotification', customContent);
  125. }
  126. void _showTips(String title, String content) {
  127. // showToast(content);
  128. }
  129. @override
  130. void dispose() {
  131. if (_receiveDeviceToken != null) {
  132. _receiveDeviceToken.cancel();
  133. }
  134. if (_receiveMessage != null) {
  135. _receiveMessage.cancel();
  136. }
  137. if (_receiveNotification != null) {
  138. _receiveNotification.cancel();
  139. }
  140. if (_launchNotification != null) {
  141. _launchNotification.cancel();
  142. }
  143. if (_resumeNotification != null) {
  144. _resumeNotification.cancel();
  145. }
  146. super.dispose();
  147. // ///移除定位监听
  148. // if (null != _locationListener) {
  149. // _locationListener.cancel();
  150. // }
  151. //
  152. // ///销毁定位
  153. // if (null != _locationPlugin) {
  154. // _locationPlugin.destroy();
  155. // }
  156. }
  157. // void _setLocationOption() {
  158. // if (null != _locationPlugin) {
  159. // AMapLocationOption locationOption = new AMapLocationOption();
  160. //
  161. // ///是否单次定位
  162. // locationOption.onceLocation = false;
  163. // ///是否需要返回逆地理信息
  164. // locationOption.needAddress = true;
  165. // ///逆地理信息的语言类型
  166. // locationOption.geoLanguage = GeoLanguage.DEFAULT;
  167. //
  168. // ///设置Android端连续定位的定位间隔
  169. // locationOption.locationInterval = 20000;
  170. // ///设置Android端的定位模式<br>
  171. // ///可选值:<br>
  172. // ///<li>[AMapLocationMode.Battery_Saving]</li>
  173. // ///<li>[AMapLocationMode.Device_Sensors]</li>
  174. // ///<li>[AMapLocationMode.Hight_Accuracy]</li>
  175. // locationOption.locationMode = AMapLocationMode.Hight_Accuracy;
  176. //
  177. // ///设置iOS端的定位最小更新距离<br>
  178. // locationOption.distanceFilter = -1;
  179. // ///设置iOS端期望的定位精度
  180. // /// 可选值:<br>
  181. // /// <li>[DesiredAccuracy.Best] 最高精度</li>
  182. // /// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>
  183. // /// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>
  184. // /// <li>[DesiredAccuracy.Kilometer] 1000米</li>
  185. // /// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>
  186. // locationOption.desiredAccuracy = DesiredAccuracy.NearestTenMeters;
  187. // ///设置iOS端是否允许系统暂停定位
  188. // locationOption.pausesLocationUpdatesAutomatically = false;
  189. //
  190. // ///将定位参数设置给定位插件
  191. // _locationPlugin.setLocationOption(locationOption);
  192. // }
  193. // }
  194. @override
  195. Widget build(BuildContext context) {
  196. return OKToast(
  197. child: p.ChangeNotifierProvider<ThemeProvider>(
  198. create: (_) => ThemeProvider(),
  199. child: p.Consumer<ThemeProvider>(
  200. builder: (_, provider, __) {
  201. return MaterialApp(
  202. title: '电梯管家',
  203. //showPerformanceOverlay: true, //显示性能标签
  204. //debugShowCheckedModeBanner: false,
  205. //checkerboardRasterCacheImages: true,
  206. theme: provider.getTheme(),
  207. darkTheme: provider.getTheme(isDarkMode: true),
  208. home: widget.home ?? SplashPage(),
  209. onGenerateRoute: Application.router.generator,
  210. localizationsDelegates: const [
  211. GlobalMaterialLocalizations.delegate,
  212. GlobalWidgetsLocalizations.delegate,
  213. GlobalCupertinoLocalizations.delegate,
  214. ],
  215. supportedLocales: const [
  216. // Locale('zh', 'CH'),
  217. // Locale('en', 'US')
  218. const Locale('zh', 'Hans'), // China
  219. const Locale('zh', ''), // China
  220. ]);
  221. },
  222. ),
  223. ),
  224. backgroundColor: Colors.black54,
  225. textPadding:
  226. const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),
  227. radius: 20.0,
  228. position: ToastPosition.bottom);
  229. }
  230. }