splash_page.dart 6.8 KB

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