import 'package:amap_location_fluttify/amap_location_fluttify.dart'; import 'package:dartin/dartin.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:sport/application.dart'; import 'package:sport/config_inject.dart'; import 'package:sport/pages/game/index.dart'; import 'package:sport/pages/home/home_info_page.dart'; import 'package:sport/pages/my/my_page.dart'; import 'package:sport/pages/social/social_index_page.dart'; import 'package:sport/provider/bluetooth.dart'; import 'package:sport/provider/message_model.dart'; import 'package:sport/services/app_lifecycle_state.dart'; import 'package:sport/utils/toast.dart'; import 'package:sport/utils/update_apk.dart'; import 'package:sport/widgets/decoration.dart'; import 'package:sport/widgets/dialog/share_popup.dart'; import 'package:sport/widgets/misc.dart'; import 'game/detail_bottom.dart'; class HomePage extends StatefulWidget { @override createState() => _HomePageState(); } class _HomePageState extends LifecycleState with ConfigInject { var _pageController = PageController(); DateTime _lastPressed; ValueNotifier _valueNotifierIndex = ValueNotifier(0); final Duration _exitDuration = Duration(seconds: 2); final List _pages = [HomeInfoPage(), GamePage(), SocialIndexPage(), MyPage()]; @override void initState() { super.initState(); // Provider.of(context, listen: false).listen(); updateApp(context); Provider.of(context, listen: false).init(); // 这就开始轮询了? } @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state); print("$state"); switch (state) { case AppLifecycleState.resumed: Provider.of(context, listen: false).listen(); break; case AppLifecycleState.paused: Provider.of(context, listen: false).disposeBluetooth(); break; default: break; } } @override void dispose() { _valueNotifierIndex?.dispose(); _pageController?.dispose(); super.dispose(); AmapLocation.instance?.dispose(); } @override void didChangeDependencies() { super.didChangeDependencies(); } @override void didUpdateWidget(HomePage oldWidget) { super.didUpdateWidget(oldWidget); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, bottomNavigationBar: SizedBox( height: 49, child: Container( decoration: shadowTop(), padding: const EdgeInsets.only(top: 4), child: ValueListenableBuilder( valueListenable: _valueNotifierIndex, builder: (BuildContext context, int value, Widget child) => CupertinoTabBar( backgroundColor: Colors.white, border: null, iconSize: 22.0, activeColor: Color(0xff333333), inactiveColor: Color(0xff999999), items: [ BottomNavigationBarItem( icon: Image.asset("lib/assets/img/tab_home_${value == 0 ? "press" : "normal"}.png"), title: Text( "首页", style: TextStyle(fontSize: 11.0), ), ), BottomNavigationBarItem( icon: Image.asset("lib/assets/img/tab_game_${value == 1 ? "press" : "normal"}.png"), title: Text("运动", style: TextStyle(fontSize: 11.0)), ), BottomNavigationBarItem( icon: Image.asset("lib/assets/img/tab_bbs_${value == 2 ? "press" : "normal"}.png"), title: Text("社区", style: TextStyle(fontSize: 11.0)), ), BottomNavigationBarItem( icon: Image.asset("lib/assets/img/tab_mine_${value == 3 ? "press" : "normal"}.png"), title: Text("我的", style: TextStyle(fontSize: 11.0)), ), ], currentIndex: value, onTap: (index) { _pageController.jumpToPage(index); _valueNotifierIndex.value = index; // 这里得轮询解决 // }, ), ), ), ), body: WillPopScope( onWillPop: () async { if (_lastPressed == null || DateTime.now().difference(_lastPressed) > _exitDuration) { //两次点击间隔超过2秒则重新计时 _lastPressed = DateTime.now(); ToastUtil.show("再次点击返回键退出"); return false; } return true; }, child: PageView.builder( itemBuilder: (ctx, index) => _pages[index], itemCount: _pages.length, controller: _pageController, physics: NeverScrollableScrollPhysics(), onPageChanged: (index) { Application.currentPage = index; }, ), ), ); } }