splash_page.dart 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import 'dart:async';
  2. import 'package:flustars/flustars.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_swiper/flutter_swiper.dart';
  5. import 'package:liftmanager/common/common.dart';
  6. import 'package:liftmanager/common/user_db.dart';
  7. import 'package:liftmanager/internal/account/account_router.dart';
  8. import 'package:liftmanager/internal/account/model/user_entity.dart';
  9. import 'package:liftmanager/net/api_service.dart';
  10. import 'package:liftmanager/provider/theme_provider.dart';
  11. import 'package:liftmanager/routers/fluro_navigator.dart';
  12. import 'package:liftmanager/routers/routers.dart';
  13. import 'package:liftmanager/utils/image_utils.dart';
  14. import 'package:liftmanager/utils/toast.dart';
  15. import 'package:liftmanager/utils/utils.dart';
  16. import 'package:liftmanager/widgets/load_image.dart';
  17. import 'package:provider/provider.dart';
  18. class SplashPage extends StatefulWidget {
  19. @override
  20. _SplashPageState createState() => _SplashPageState();
  21. }
  22. class _SplashPageState extends State<SplashPage> {
  23. // Push _push = Push();
  24. // StreamSubscription<String> _receiveDeviceToken;
  25. // StreamSubscription<Message> _receiveMessage;
  26. // StreamSubscription<Message> _receiveNotification;
  27. // StreamSubscription<String> _launchNotification;
  28. // StreamSubscription<String> _resumeNotification;
  29. int _status = 0;
  30. List<String> _guideList = ["app_start_1", "app_start_2", "app_start_3","app_start_4"];
  31. Timer _subscription;
  32. @override
  33. void initState() {
  34. super.initState();
  35. WidgetsBinding.instance.addPostFrameCallback((_) async {
  36. await SpUtil.getInstance();
  37. // 由于SpUtil未初始化,所以MaterialApp获取的为默认主题配置,这里同步一下。
  38. Provider.of<ThemeProvider>(context,listen: false).syncTheme();
  39. if (SpUtil.getBool(Constant.keyGuide, defValue: true)){
  40. /// 预先缓存图片,避免直接使用时因为首次加载造成闪动
  41. _guideList.forEach((image){
  42. precacheImage(ImageUtils.getAssetImage(image), context);
  43. });
  44. }
  45. _initSplash();
  46. });
  47. // _receiveDeviceToken =
  48. // _push.receiveDeviceToken().listen(_handleReceiveDeviceToken);
  49. // _receiveMessage = _push.receiveMessage().listen(_handleReceiveMessage);
  50. // _receiveNotification =
  51. // _push.receiveNotification().listen(_handleReceiveNotification);
  52. // _launchNotification =
  53. // _push.launchNotification().listen(_handleLaunchNotification);
  54. // _resumeNotification =
  55. // _push.resumeNotification().listen(_handleResumeNotification);
  56. //
  57. // _push.startWork(enableDebug: false);
  58. }
  59. // void _handleReceiveDeviceToken(String deviceToken) async {
  60. // print('receiveDeviceToken: $deviceToken - ${await _push.getDeviceToken()}');
  61. // _showTips('receiveDeviceToken', deviceToken);
  62. // }
  63. //
  64. // void _handleReceiveMessage(Message message) {
  65. // print(
  66. // 'receiveMessage: ${message.title} - ${message.content} - ${message.customContent}');
  67. // _showTips('receiveMessage',
  68. // '${message.title} - ${message.content} - ${message.customContent}');
  69. // }
  70. //
  71. // void _handleReceiveNotification(Message notification) {
  72. // print(
  73. // 'receiveNotification: ${notification.title} - ${notification.content} - ${notification.customContent}');
  74. // _showTips('receiveNotification',
  75. // '${notification.title} - ${notification.content} - ${notification.customContent}');
  76. // }
  77. //
  78. // void _handleLaunchNotification(String customContent) {
  79. // print('launchNotification: $customContent');
  80. // _showTips('launchNotification', customContent);
  81. // }
  82. //
  83. // void _handleResumeNotification(String customContent) {
  84. // print('resumeNotification: $customContent');
  85. // _showTips('resumeNotification', customContent);
  86. // }
  87. void _showTips(String title, String content) {
  88. // showCupertinoDialog(
  89. // context: context,
  90. // builder: (BuildContext context) {
  91. // return CupertinoAlertDialog(
  92. // title: Text(title),
  93. // content: Text(content),
  94. // actions: <Widget>[
  95. // CupertinoDialogAction(
  96. // child: const Text('朕知道了~'),
  97. // onPressed: () {
  98. // Navigator.of(context).pop();
  99. // },
  100. // ),
  101. // ],
  102. // );
  103. // },
  104. // );
  105. }
  106. @override
  107. void dispose() {
  108. _subscription?.cancel();
  109. // if (_receiveDeviceToken != null) {
  110. // _receiveDeviceToken.cancel();
  111. // }
  112. // if (_receiveMessage != null) {
  113. // _receiveMessage.cancel();
  114. // }
  115. // if (_receiveNotification != null) {
  116. // _receiveNotification.cancel();
  117. // }
  118. // if (_launchNotification != null) {
  119. // _launchNotification.cancel();
  120. // }
  121. // if (_resumeNotification != null) {
  122. // _resumeNotification.cancel();
  123. // }
  124. super.dispose();
  125. }
  126. void _initGuide() {
  127. setState(() {
  128. _status = 1;
  129. });
  130. }
  131. void _initSplash(){
  132. _subscription = Timer(Duration(milliseconds: 1500),()=>Stream.value(2).listen((_){
  133. if (SpUtil.getBool(Constant.keyGuide, defValue: true)) {
  134. SpUtil.putBool(Constant.keyGuide, false);
  135. _initGuide();
  136. } else {
  137. if(SpUtil.getString(Constant.accessToken).length >0){
  138. _goMain();
  139. }else{
  140. // _goLogin();
  141. custom_login();
  142. }
  143. }
  144. }));
  145. }
  146. _goLogin(){
  147. NavigatorUtils.push(context, AccountRouter.loginPage, replace: true);
  148. }
  149. _goMain(){
  150. NavigatorUtils.push(context, Routers.home, clearStack: true);
  151. }
  152. // ignore: non_constant_identifier_names
  153. void custom_login(){
  154. showLoading(context, "游客登录中...");
  155. ApiService(context: context).login("12345678910", Utils.generateMd5("123456789"),
  156. onSuccess: (UserEntity res) {
  157. // FlutterStars.SpUtil.putObject(Constant.user, res);
  158. dismissLoading(context);
  159. User().setCurrentUser(res);
  160. print("==============");
  161. NavigatorUtils.push(context, Routers.home, clearStack: true);
  162. }, onError: (code, errMsg) {
  163. toasts(errMsg);
  164. _goLogin();
  165. dismissLoading(context);
  166. });
  167. }
  168. @override
  169. Widget build(BuildContext context) {
  170. return Material(
  171. child: _status == 0 ? Image.asset(
  172. ImageUtils.getImgPath("start_page", format: "png"),
  173. width: double.infinity,
  174. fit: BoxFit.fill,
  175. height: double.infinity,
  176. ) : Swiper(
  177. key: const Key('swiper'),
  178. itemCount: _guideList.length,
  179. loop: false,
  180. itemBuilder: (_, index){
  181. return LoadAssetImage(
  182. _guideList[index],
  183. key: Key(_guideList[index]),
  184. fit: BoxFit.cover,
  185. width: double.infinity,
  186. height: double.infinity,
  187. );
  188. },
  189. onTap: (index){
  190. if (index == _guideList.length - 1){
  191. // _goLogin();
  192. custom_login();
  193. }
  194. },
  195. )
  196. );
  197. }
  198. }