home_page.dart 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/scheduler.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:get_it/get_it.dart';
  7. import 'package:provider/provider.dart';
  8. import 'package:shared_preferences/shared_preferences.dart';
  9. import 'package:sport/application.dart';
  10. import 'package:sport/pages/game/index.dart';
  11. import 'package:sport/pages/home/home_info_page.dart';
  12. import 'package:sport/pages/my/my_page.dart';
  13. import 'package:sport/pages/run/run_data.dart';
  14. import 'package:sport/pages/run/setting_page.dart';
  15. import 'package:sport/pages/social/new_social_index_page.dart';
  16. import 'package:sport/pages/social/social_index_page.dart';
  17. import 'package:sport/provider/bluetooth.dart';
  18. import 'package:sport/provider/game_model.dart';
  19. import 'package:sport/provider/message_model.dart';
  20. import 'package:sport/provider/user_model.dart';
  21. import 'package:sport/services/app_lifecycle_state.dart';
  22. import 'package:sport/utils/task_queue.dart';
  23. import 'package:sport/utils/toast.dart';
  24. import 'package:sport/utils/update_apk.dart';
  25. import 'package:umeng_common_sdk/umeng_common_sdk.dart';
  26. class HomePage extends StatefulWidget {
  27. @override
  28. createState() => _HomePageState();
  29. }
  30. class _HomePageState extends LifecycleState<HomePage> {
  31. var _pageController = PageController();
  32. DateTime? _lastPressed;
  33. ValueNotifier<int> _valueNotifierIndex = ValueNotifier(0);
  34. final Duration _exitDuration = Duration(seconds: 2);
  35. final GlobalKey _key = GlobalKey();
  36. late MessageModel _messageModel;
  37. late UserModel userModel;
  38. late List<Widget> _pages;
  39. @override
  40. void initState() {
  41. super.initState();
  42. if (openSocial()) {
  43. _pages = <Widget>[HomeInfoPage(), GamePage(), SocialIndexPage(), MyPage()];
  44. } else {
  45. _pages = <Widget>[HomeInfoPage(), GamePage(), MyPage()];
  46. }
  47. _messageModel = GetIt.I<MessageModel>();
  48. GetIt.I<Bluetooth>().init(context);
  49. userModel = Provider.of<UserModel>(context, listen: false);
  50. SchedulerBinding.instance?.addPostFrameCallback((timeStamp) async {
  51. UserModel userModel = Provider.of<UserModel>(context, listen: false);
  52. await userModel.initUser();
  53. if (userModel.user.id > 0) {
  54. updateApp(context);
  55. _messageModel.init();
  56. _messageModel.push(context);
  57. _messageModel.loop(context);
  58. var preferences = await SharedPreferences.getInstance();
  59. await preferences.remove(RunSetting.KEY_START);
  60. uploadRunData();
  61. TaskQueue().upload();
  62. }
  63. });
  64. UmengCommonSdk.initCommon("60f23987a6f90557b7bc7505", "60f23a652a1a2a58e7db1b21", "ouj");
  65. UmengCommonSdk.onEvent("app_launch", {});
  66. }
  67. @override
  68. void didChangeAppLifecycleState(AppLifecycleState state) {
  69. super.didChangeAppLifecycleState(state);
  70. print("HomePage $state");
  71. _messageModel.state = state;
  72. if (userModel.user.id > 0) {
  73. switch (state) {
  74. case AppLifecycleState.resumed:
  75. GetIt.I<Bluetooth>().listen(context);
  76. _messageModel.loop(context);
  77. GetIt.I<GameModel>().resumed();
  78. // SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  79. break;
  80. case AppLifecycleState.paused:
  81. GetIt.I<Bluetooth>().background(context);
  82. _messageModel.close();
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. appLifecycleState = state;
  89. }
  90. @override
  91. void dispose() {
  92. super.dispose();
  93. _valueNotifierIndex.dispose();
  94. _pageController.dispose();
  95. }
  96. @override
  97. Widget build(BuildContext context) {
  98. Application.context = context;
  99. return Scaffold(
  100. key: _key,
  101. backgroundColor: Colors.white,
  102. bottomNavigationBar: AnnotatedRegion<SystemUiOverlayStyle>(
  103. value: SystemUiOverlayStyle.light,
  104. child: Container(
  105. decoration: BoxDecoration(boxShadow: [BoxShadow(offset: Offset(0.0, 3), blurRadius: 3, spreadRadius: 0, color: Colors.black38)],),
  106. child: ValueListenableBuilder(
  107. valueListenable: _valueNotifierIndex,
  108. builder: (BuildContext context, int value, Widget? child) => CupertinoTabBar(
  109. backgroundColor: Colors.white,
  110. border: null,
  111. iconSize: 22.0,
  112. activeColor: Colors.black,
  113. inactiveColor: const Color(0xff666666),
  114. items: <BottomNavigationBarItem>[
  115. BottomNavigationBarItem(
  116. icon: Padding(
  117. padding: const EdgeInsets.only(top: 6.0),
  118. child: Image.asset("lib/assets/img/tab_home_${value == 0 ? "press" : "normal"}.png"),
  119. ),
  120. label: "首页",
  121. ),
  122. BottomNavigationBarItem(
  123. icon: Padding(
  124. padding: const EdgeInsets.only(top: 6.0),
  125. child: Image.asset("lib/assets/img/tab_game_${value == 1 ? "press" : "normal"}.png"),
  126. ),
  127. label: "运动",
  128. ),
  129. if (openSocial())
  130. BottomNavigationBarItem(
  131. icon: Stack(
  132. clipBehavior: Clip.none,
  133. alignment: Alignment.center,
  134. children: [
  135. Center(
  136. child: Padding(
  137. padding: const EdgeInsets.only(top: 6.0),
  138. child: Image.asset("lib/assets/img/tab_bbs_${value == _pages.length - 2 ? "press" : "normal"}.png"),
  139. ),
  140. ),
  141. ValueListenableBuilder(
  142. valueListenable: _messageModel.notifierSocialTotal,
  143. builder: (BuildContext context, int value, Widget? child) {
  144. if (value == 0) return Container();
  145. return Container(
  146. margin: EdgeInsets.only(left: 20, bottom: 16),
  147. width: 8,
  148. height: 8,
  149. decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.red),
  150. );
  151. })
  152. ],
  153. ),
  154. label: "社区",
  155. ),
  156. BottomNavigationBarItem(
  157. icon: Padding(
  158. padding: const EdgeInsets.only(top: 6.0),
  159. child: Image.asset("lib/assets/img/tab_my_${value == _pages.length - 1 ? "press" : "normal"}.png"),
  160. ),
  161. label: "我的",
  162. ),
  163. ],
  164. currentIndex: value,
  165. onTap: (index) {
  166. _pageController.jumpToPage(index);
  167. _valueNotifierIndex.value = index;
  168. // 这里得轮询解决
  169. //
  170. UmengCommonSdk.onEvent("index_tab_${_pages[index].runtimeType}", {});
  171. },
  172. ),
  173. ),
  174. ),
  175. ),
  176. body: WillPopScope(
  177. onWillPop: () async {
  178. if (_lastPressed == null || DateTime.now().difference(_lastPressed!) > _exitDuration) {
  179. //两次点击间隔超过2秒则重新计时
  180. _lastPressed = DateTime.now();
  181. ToastUtil.show("再次点击返回键退出");
  182. return false;
  183. }
  184. exit(0);
  185. },
  186. child: PageView.builder(
  187. itemBuilder: (ctx, index) => _pages[index],
  188. itemCount: _pages.length,
  189. controller: _pageController,
  190. physics: NeverScrollableScrollPhysics(),
  191. onPageChanged: (index) {
  192. Application.currentPage = index;
  193. },
  194. ),
  195. ),
  196. );
  197. }
  198. }