home_page.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. }
  56. @override
  57. void dispose() {
  58. _valueNotifierIndex?.dispose();
  59. _pageController?.dispose();
  60. super.dispose();
  61. AmapLocation.instance?.dispose();
  62. }
  63. @override
  64. void didChangeDependencies() {
  65. super.didChangeDependencies();
  66. }
  67. @override
  68. void didUpdateWidget(HomePage oldWidget) {
  69. super.didUpdateWidget(oldWidget);
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. return AnnotatedRegion<SystemUiOverlayStyle>(
  74. value: SystemUiOverlayStyle.light,
  75. child: Scaffold(
  76. backgroundColor: Colors.white,
  77. bottomNavigationBar: SafeArea(
  78. child: SizedBox(
  79. height: 49,
  80. child: Container(
  81. child: ValueListenableBuilder(
  82. valueListenable: _valueNotifierIndex,
  83. builder: (BuildContext context, int value, Widget child) => CupertinoTabBar(
  84. backgroundColor: const Color(0xff1D1D1D),
  85. border: null,
  86. iconSize: 22.0,
  87. activeColor: const Color(0xffFFC400),
  88. inactiveColor: const Color(0xff666666),
  89. items: <BottomNavigationBarItem>[
  90. BottomNavigationBarItem(
  91. icon: Padding(
  92. padding: const EdgeInsets.only(top: 6.0),
  93. child: Image.asset("lib/assets/img/tab_home_${value == 0 ? "press" : "normal"}.png"),
  94. ),
  95. title: Text(
  96. "首页",
  97. style: TextStyle(fontSize: 11.0),
  98. ),
  99. ),
  100. BottomNavigationBarItem(
  101. icon: Padding(
  102. padding: const EdgeInsets.only(top: 6.0),
  103. child: Image.asset("lib/assets/img/tab_game_${value == 1 ? "press" : "normal"}.png"),
  104. ),
  105. title: Text("运动", style: TextStyle(fontSize: 11.0)),
  106. ),
  107. BottomNavigationBarItem(
  108. icon: Padding(
  109. padding: const EdgeInsets.only(top: 6.0),
  110. child: Image.asset("lib/assets/img/tab_bbs_${value == 2 ? "press" : "normal"}.png"),
  111. ),
  112. title: Text("社区", style: TextStyle(fontSize: 11.0)),
  113. ),
  114. BottomNavigationBarItem(
  115. icon: Padding(
  116. padding: const EdgeInsets.only(top: 6.0),
  117. child: Image.asset("lib/assets/img/tab_my_${value == 3 ? "press" : "normal"}.png"),
  118. ),
  119. title: Text("我的", style: TextStyle(fontSize: 11.0)),
  120. ),
  121. ],
  122. currentIndex: value,
  123. onTap: (index) {
  124. _pageController.jumpToPage(index);
  125. _valueNotifierIndex.value = index;
  126. // 这里得轮询解决
  127. //
  128. },
  129. ),
  130. ),
  131. ),
  132. ),
  133. ),
  134. body: WillPopScope(
  135. onWillPop: () async {
  136. if (_lastPressed == null || DateTime.now().difference(_lastPressed) > _exitDuration) {
  137. //两次点击间隔超过2秒则重新计时
  138. _lastPressed = DateTime.now();
  139. ToastUtil.show("再次点击返回键退出");
  140. return false;
  141. }
  142. return true;
  143. },
  144. child: PageView.builder(
  145. itemBuilder: (ctx, index) => _pages[index],
  146. itemCount: _pages.length,
  147. controller: _pageController,
  148. physics: NeverScrollableScrollPhysics(),
  149. onPageChanged: (index) {
  150. Application.currentPage = index;
  151. },
  152. ),
  153. ),
  154. ),
  155. );
  156. }
  157. }