home_page.dart 5.0 KB

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