home_page.dart 5.1 KB

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