main.dart 7.1 KB

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