splash_page.dart 5.9 KB

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