home_page.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'package:amap_location_fluttify/amap_location_fluttify.dart';
  2. import 'package:dartin/dartin.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:provider/provider.dart';
  7. import 'package:sport/application.dart';
  8. import 'package:sport/config_inject.dart';
  9. import 'package:sport/pages/game/index.dart';
  10. import 'package:sport/pages/home/home_info_page.dart';
  11. import 'package:sport/pages/my/my_page.dart';
  12. import 'package:sport/pages/social/new_social_index_page.dart';
  13. import 'package:sport/pages/social/social_index_page.dart';
  14. import 'package:sport/provider/bluetooth.dart';
  15. import 'package:sport/provider/message_model.dart';
  16. import 'package:sport/services/app_lifecycle_state.dart';
  17. import 'package:sport/utils/toast.dart';
  18. import 'package:sport/utils/update_apk.dart';
  19. import 'package:sport/widgets/decoration.dart';
  20. import 'package:sport/widgets/dialog/share_popup.dart';
  21. import 'package:sport/widgets/misc.dart';
  22. import 'game/detail_bottom.dart';
  23. class HomePage extends StatefulWidget {
  24. @override
  25. createState() => _HomePageState();
  26. }
  27. class _HomePageState extends LifecycleState<HomePage> with ConfigInject {
  28. var _pageController = PageController();
  29. DateTime _lastPressed;
  30. ValueNotifier<int> _valueNotifierIndex = ValueNotifier(0);
  31. final Duration _exitDuration = Duration(seconds: 2);
  32. final List<Widget> _pages = <Widget>[HomeInfoPage(), GamePage(), NewSocialIndexPage(), MyPage()];
  33. @override
  34. void initState() {
  35. super.initState();
  36. updateApp(context);
  37. Provider.of<MessageModel>(context, listen: false).init(); // 这就开始轮询了?
  38. Provider.of<Bluetooth>(context, listen: false).listen();
  39. }
  40. @override
  41. void didChangeAppLifecycleState(AppLifecycleState state) {
  42. super.didChangeAppLifecycleState(state);
  43. print("$state");
  44. switch (state) {
  45. case AppLifecycleState.resumed:
  46. Provider.of<Bluetooth>(context, listen: false).listen();
  47. SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  48. break;
  49. case AppLifecycleState.paused:
  50. Provider.of<Bluetooth>(context, listen: false).disposeBluetooth();
  51. break;
  52. default:
  53. break;
  54. }
  55. Provider.of<MessageModel>(context, listen: false).state = state;
  56. }
  57. @override
  58. void dispose() {
  59. _valueNotifierIndex?.dispose();
  60. _pageController?.dispose();
  61. super.dispose();
  62. AmapLocation.instance?.dispose();
  63. }
  64. @override
  65. void didChangeDependencies() {
  66. super.didChangeDependencies();
  67. }
  68. @override
  69. void didUpdateWidget(HomePage oldWidget) {
  70. super.didUpdateWidget(oldWidget);
  71. }
  72. @override
  73. Widget build(BuildContext context) {
  74. return AnnotatedRegion<SystemUiOverlayStyle>(
  75. value: SystemUiOverlayStyle.light,
  76. child: Scaffold(
  77. backgroundColor: Colors.white,
  78. bottomNavigationBar: SafeArea(
  79. child: SizedBox(
  80. height: 49,
  81. child: Container(
  82. child: ValueListenableBuilder(
  83. valueListenable: _valueNotifierIndex,
  84. builder: (BuildContext context, int value, Widget child) => CupertinoTabBar(
  85. backgroundColor: const Color(0xff1D1D1D),
  86. border: null,
  87. iconSize: 22.0,
  88. activeColor: const Color(0xffFFC400),
  89. inactiveColor: const Color(0xff666666),
  90. items: <BottomNavigationBarItem>[
  91. BottomNavigationBarItem(
  92. icon: Padding(
  93. padding: const EdgeInsets.only(top: 6.0),
  94. child: Image.asset("lib/assets/img/tab_home_${value == 0 ? "press" : "normal"}.png"),
  95. ),
  96. title: Text(
  97. "首页",
  98. style: TextStyle(fontSize: 11.0),
  99. ),
  100. ),
  101. BottomNavigationBarItem(
  102. icon: Padding(
  103. padding: const EdgeInsets.only(top: 6.0),
  104. child: Image.asset("lib/assets/img/tab_game_${value == 1 ? "press" : "normal"}.png"),
  105. ),
  106. title: Text("运动", style: TextStyle(fontSize: 11.0)),
  107. ),
  108. BottomNavigationBarItem(
  109. icon: Padding(
  110. padding: const EdgeInsets.only(top: 6.0),
  111. child: Image.asset("lib/assets/img/tab_bbs_${value == 2 ? "press" : "normal"}.png"),
  112. ),
  113. title: Text("社区", style: TextStyle(fontSize: 11.0)),
  114. ),
  115. BottomNavigationBarItem(
  116. icon: Padding(
  117. padding: const EdgeInsets.only(top: 6.0),
  118. child: Image.asset("lib/assets/img/tab_my_${value == 3 ? "press" : "normal"}.png"),
  119. ),
  120. title: Text("我的", style: TextStyle(fontSize: 11.0)),
  121. ),
  122. ],
  123. currentIndex: value,
  124. onTap: (index) {
  125. _pageController.jumpToPage(index);
  126. _valueNotifierIndex.value = index;
  127. // 这里得轮询解决
  128. //
  129. },
  130. ),
  131. ),
  132. ),
  133. ),
  134. ),
  135. body: WillPopScope(
  136. onWillPop: () async {
  137. if (_lastPressed == null || DateTime.now().difference(_lastPressed) > _exitDuration) {
  138. //两次点击间隔超过2秒则重新计时
  139. _lastPressed = DateTime.now();
  140. ToastUtil.show("再次点击返回键退出");
  141. return false;
  142. }
  143. return true;
  144. },
  145. child: PageView.builder(
  146. itemBuilder: (ctx, index) => _pages[index],
  147. itemCount: _pages.length,
  148. controller: _pageController,
  149. physics: NeverScrollableScrollPhysics(),
  150. onPageChanged: (index) {
  151. Application.currentPage = index;
  152. },
  153. ),
  154. ),
  155. ),
  156. );
  157. }
  158. }