splash_page.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import 'dart:async';
  2. import 'dart:html';
  3. import 'package:amap_map_fluttify/amap_map_fluttify.dart';
  4. import 'package:flustars/flustars.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_swiper/flutter_swiper.dart';
  9. import 'package:jpush_flutter/jpush_flutter.dart';
  10. import 'package:liftmanager/common/common.dart';
  11. import 'package:liftmanager/common/user_db.dart';
  12. import 'package:liftmanager/internal/account/account_router.dart';
  13. import 'package:liftmanager/internal/account/model/user_entity.dart';
  14. import 'package:liftmanager/net/api_service.dart';
  15. import 'package:liftmanager/provider/theme_provider.dart';
  16. import 'package:liftmanager/routers/fluro_navigator.dart';
  17. import 'package:liftmanager/routers/routers.dart';
  18. import 'package:liftmanager/utils/image_utils.dart';
  19. import 'package:liftmanager/utils/toast.dart';
  20. import 'package:liftmanager/utils/utils.dart';
  21. import 'package:liftmanager/widgets/load_image.dart';
  22. import 'package:provider/provider.dart';
  23. import 'package:umeng_common_sdk/umeng_common_sdk.dart';
  24. class SplashPage extends StatefulWidget {
  25. @override
  26. _SplashPageState createState() => _SplashPageState();
  27. }
  28. class _SplashPageState extends State<SplashPage> {
  29. // Push _push = Push();
  30. // StreamSubscription<String> _receiveDeviceToken;
  31. // StreamSubscription<Message> _receiveMessage;
  32. // StreamSubscription<Message> _receiveNotification;
  33. // StreamSubscription<String> _launchNotification;
  34. // StreamSubscription<String> _resumeNotification;
  35. int _status = 0;
  36. List<String> _guideList = [
  37. "app_start_1",
  38. "app_start_2",
  39. "app_start_3",
  40. "app_start_4"
  41. ];
  42. Timer _subscription;
  43. @override
  44. Future<void> initState() async {
  45. super.initState();
  46. FlutterBugly.init(androidAppId: "ae4e3d567d", iOSAppId: "4f09cdc82c");
  47. UmengCommonSdk.initCommon(
  48. '606d0db3de41b946ab3fe2bf', '60711efede41b946ab45daad', 'App Store');
  49. UmengCommonSdk.setPageCollectionModeManual();
  50. await AmapService.init(
  51. iosKey: '7724c41be3ebe0cb7e5db4d79edc51b7',
  52. androidKey: '9eaae78b5b6b4ba78460b2e2539b798d');
  53. try {
  54. jpush.addEventHandler(
  55. onReceiveNotification: (Map<String, dynamic> message) async {
  56. print("flutter onReceiveNotification: $message");
  57. }, onOpenNotification: (Map<String, dynamic> message) async {
  58. print("flutter onOpenNotification: $message");
  59. }, onReceiveMessage: (Map<String, dynamic> message) async {
  60. print("flutter onReceiveMessage: $message");
  61. }, onReceiveNotificationAuthorization:
  62. (Map<String, dynamic> message) async {
  63. print("flutter onReceiveNotificationAuthorization: $message");
  64. });
  65. } on PlatformException {
  66. print('Failed to get platform version.');
  67. }
  68. jpush.setup(
  69. appKey: "bd76ee7fb5dac9d04b12a521",
  70. // channel: "theChannel",
  71. production: false,
  72. debug: true,
  73. );
  74. jpush.applyPushAuthority(
  75. NotificationSettingsIOS(sound: true, alert: true, badge: false));
  76. // Platform messages may fail, so we use a try/catch PlatformException.
  77. jpush.getRegistrationID().then((rid) {
  78. print("flutter get registration id : $rid");
  79. });
  80. WidgetsBinding.instance.addPostFrameCallback((_) async {
  81. await SpUtil.getInstance();
  82. // 由于SpUtil未初始化,所以MaterialApp获取的为默认主题配置,这里同步一下。
  83. Provider.of<ThemeProvider>(context, listen: false).syncTheme();
  84. if (SpUtil.getBool(Constant.keyGuide, defValue: true)) {
  85. /// 预先缓存图片,避免直接使用时因为首次加载造成闪动
  86. _guideList.forEach((image) {
  87. precacheImage(ImageUtils.getAssetImage(image), context);
  88. });
  89. }
  90. _initSplash();
  91. });
  92. // _receiveDeviceToken =
  93. // _push.receiveDeviceToken().listen(_handleReceiveDeviceToken);
  94. // _receiveMessage = _push.receiveMessage().listen(_handleReceiveMessage);
  95. // _receiveNotification =
  96. // _push.receiveNotification().listen(_handleReceiveNotification);
  97. // _launchNotification =
  98. // _push.launchNotification().listen(_handleLaunchNotification);
  99. // _resumeNotification =
  100. // _push.resumeNotification().listen(_handleResumeNotification);
  101. //
  102. // _push.startWork(enableDebug: false);
  103. }
  104. // void _handleReceiveDeviceToken(String deviceToken) async {
  105. // print('receiveDeviceToken: $deviceToken - ${await _push.getDeviceToken()}');
  106. // _showTips('receiveDeviceToken', deviceToken);
  107. // }
  108. //
  109. // void _handleReceiveMessage(Message message) {
  110. // print(
  111. // 'receiveMessage: ${message.title} - ${message.content} - ${message.customContent}');
  112. // _showTips('receiveMessage',
  113. // '${message.title} - ${message.content} - ${message.customContent}');
  114. // }
  115. //
  116. // void _handleReceiveNotification(Message notification) {
  117. // print(
  118. // 'receiveNotification: ${notification.title} - ${notification.content} - ${notification.customContent}');
  119. // _showTips('receiveNotification',
  120. // '${notification.title} - ${notification.content} - ${notification.customContent}');
  121. // }
  122. //
  123. // void _handleLaunchNotification(String customContent) {
  124. // print('launchNotification: $customContent');
  125. // _showTips('launchNotification', customContent);
  126. // }
  127. //
  128. // void _handleResumeNotification(String customContent) {
  129. // print('resumeNotification: $customContent');
  130. // _showTips('resumeNotification', customContent);
  131. // }
  132. void _showTips(String title, String content) {
  133. // showCupertinoDialog(
  134. // context: context,
  135. // builder: (BuildContext context) {
  136. // return CupertinoAlertDialog(
  137. // title: Text(title),
  138. // content: Text(content),
  139. // actions: <Widget>[
  140. // CupertinoDialogAction(
  141. // child: const Text('朕知道了~'),
  142. // onPressed: () {
  143. // Navigator.of(context).pop();
  144. // },
  145. // ),
  146. // ],
  147. // );
  148. // },
  149. // );
  150. }
  151. @override
  152. void dispose() {
  153. _subscription?.cancel();
  154. // if (_receiveDeviceToken != null) {
  155. // _receiveDeviceToken.cancel();
  156. // }
  157. // if (_receiveMessage != null) {
  158. // _receiveMessage.cancel();
  159. // }
  160. // if (_receiveNotification != null) {
  161. // _receiveNotification.cancel();
  162. // }
  163. // if (_launchNotification != null) {
  164. // _launchNotification.cancel();
  165. // }
  166. // if (_resumeNotification != null) {
  167. // _resumeNotification.cancel();
  168. // }
  169. super.dispose();
  170. }
  171. void _initGuide() {
  172. setState(() {
  173. _status = 1;
  174. });
  175. }
  176. void _initSplash() {
  177. _subscription = Timer(
  178. Duration(milliseconds: 1500),
  179. () => Stream.value(2).listen((_) {
  180. if (SpUtil.getBool(Constant.keyGuide, defValue: true)) {
  181. SpUtil.putBool(Constant.keyGuide, false);
  182. _initGuide();
  183. } else {
  184. if (SpUtil.getString(Constant.accessToken).length > 0) {
  185. _goMain();
  186. } else {
  187. // _goLogin();
  188. custom_login();
  189. }
  190. }
  191. }));
  192. }
  193. _goLogin() {
  194. NavigatorUtils.push(context, AccountRouter.loginPage, replace: true);
  195. }
  196. _goMain() {
  197. NavigatorUtils.push(context, Routers.home, clearStack: true);
  198. }
  199. // ignore: non_constant_identifier_names
  200. void custom_login() {
  201. showLoading(context, "游客登录中...");
  202. ApiService(context: context)
  203. .login("12345678910", Utils.generateMd5("123456789"),
  204. onSuccess: (UserEntity res) {
  205. // FlutterStars.SpUtil.putObject(Constant.user, res);
  206. dismissLoading(context);
  207. User().setCurrentUser(res);
  208. print("==============");
  209. NavigatorUtils.push(context, Routers.home, clearStack: true);
  210. }, onError: (code, errMsg) {
  211. toasts(errMsg);
  212. _goLogin();
  213. dismissLoading(context);
  214. });
  215. }
  216. @override
  217. Widget build(BuildContext context) {
  218. return Material(
  219. child: _status == 0
  220. ? Image.asset(
  221. ImageUtils.getImgPath("start_page", format: "png"),
  222. width: double.infinity,
  223. fit: BoxFit.fill,
  224. height: double.infinity,
  225. )
  226. : Swiper(
  227. key: const Key('swiper'),
  228. itemCount: _guideList.length,
  229. loop: false,
  230. itemBuilder: (_, index) {
  231. return LoadAssetImage(
  232. _guideList[index],
  233. key: Key(_guideList[index]),
  234. fit: BoxFit.cover,
  235. width: double.infinity,
  236. height: double.infinity,
  237. );
  238. },
  239. onTap: (index) {
  240. if (index == _guideList.length - 1) {
  241. // _goLogin();
  242. custom_login();
  243. }
  244. },
  245. ));
  246. }
  247. }