import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; import 'package:get_it/get_it.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sport/application.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/run/run_data.dart'; import 'package:sport/pages/run/setting_page.dart'; import 'package:sport/pages/social/new_social_index_page.dart'; import 'package:sport/pages/social/social_index_page.dart'; import 'package:sport/provider/bluetooth.dart'; import 'package:sport/provider/game_model.dart'; import 'package:sport/provider/message_model.dart'; import 'package:sport/provider/user_model.dart'; import 'package:sport/services/app_lifecycle_state.dart'; import 'package:sport/utils/task_queue.dart'; import 'package:sport/utils/toast.dart'; import 'package:sport/utils/update_apk.dart'; import 'package:umeng_common_sdk/umeng_common_sdk.dart'; class HomePage extends StatefulWidget { @override createState() => _HomePageState(); } class _HomePageState extends LifecycleState { var _pageController = PageController(); DateTime? _lastPressed; ValueNotifier _valueNotifierIndex = ValueNotifier(0); final Duration _exitDuration = Duration(seconds: 2); final GlobalKey _key = GlobalKey(); late MessageModel _messageModel; late UserModel userModel; late List _pages; @override void initState() { super.initState(); if (openSocial()) { _pages = [HomeInfoPage(), GamePage(), SocialIndexPage(), MyPage()]; } else { _pages = [HomeInfoPage(), GamePage(), MyPage()]; } _messageModel = GetIt.I(); GetIt.I().init(context); userModel = Provider.of(context, listen: false); SchedulerBinding.instance?.addPostFrameCallback((timeStamp) async { UserModel userModel = Provider.of(context, listen: false); await userModel.initUser(); if (userModel.user.id > 0) { updateApp(context); _messageModel.init(); _messageModel.push(context); _messageModel.loop(context); var preferences = await SharedPreferences.getInstance(); await preferences.remove(RunSetting.KEY_START); uploadRunData(); TaskQueue().upload(); } }); UmengCommonSdk.initCommon("60f23987a6f90557b7bc7505", "60f23a652a1a2a58e7db1b21", "ouj"); UmengCommonSdk.onEvent("app_launch", {}); } @override void didChangeAppLifecycleState(AppLifecycleState state) { super.didChangeAppLifecycleState(state); print("HomePage $state"); _messageModel.state = state; if (userModel.user.id > 0) { switch (state) { case AppLifecycleState.resumed: GetIt.I().listen(context); _messageModel.loop(context); GetIt.I().resumed(); // SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]); break; case AppLifecycleState.paused: GetIt.I().background(context); _messageModel.close(); break; default: break; } } appLifecycleState = state; } @override void dispose() { super.dispose(); _valueNotifierIndex.dispose(); _pageController.dispose(); } @override Widget build(BuildContext context) { Application.context = context; return Scaffold( key: _key, backgroundColor: Colors.white, bottomNavigationBar: AnnotatedRegion( value: SystemUiOverlayStyle.light, child: Container( decoration: BoxDecoration(boxShadow: [BoxShadow(offset: Offset(0.0, 3), blurRadius: 3, spreadRadius: 0, color: Colors.black38)],), child: ValueListenableBuilder( valueListenable: _valueNotifierIndex, builder: (BuildContext context, int value, Widget? child) => CupertinoTabBar( backgroundColor: Colors.white, border: null, iconSize: 22.0, activeColor: Colors.black, inactiveColor: const Color(0xff666666), items: [ BottomNavigationBarItem( icon: Padding( padding: const EdgeInsets.only(top: 6.0), child: Image.asset("lib/assets/img/tab_home_${value == 0 ? "press" : "normal"}.png"), ), label: "首页", ), BottomNavigationBarItem( icon: Padding( padding: const EdgeInsets.only(top: 6.0), child: Image.asset("lib/assets/img/tab_game_${value == 1 ? "press" : "normal"}.png"), ), label: "运动", ), if (openSocial()) BottomNavigationBarItem( icon: Stack( clipBehavior: Clip.none, alignment: Alignment.center, children: [ Center( child: Padding( padding: const EdgeInsets.only(top: 6.0), child: Image.asset("lib/assets/img/tab_bbs_${value == _pages.length - 2 ? "press" : "normal"}.png"), ), ), ValueListenableBuilder( valueListenable: _messageModel.notifierSocialTotal, builder: (BuildContext context, int value, Widget? child) { if (value == 0) return Container(); return Container( margin: EdgeInsets.only(left: 20, bottom: 16), width: 8, height: 8, decoration: BoxDecoration(shape: BoxShape.circle, color: Colors.red), ); }) ], ), label: "社区", ), BottomNavigationBarItem( icon: Padding( padding: const EdgeInsets.only(top: 6.0), child: Image.asset("lib/assets/img/tab_my_${value == _pages.length - 1 ? "press" : "normal"}.png"), ), label: "我的", ), ], currentIndex: value, onTap: (index) { _pageController.jumpToPage(index); _valueNotifierIndex.value = index; // 这里得轮询解决 // UmengCommonSdk.onEvent("index_tab_${_pages[index].runtimeType}", {}); }, ), ), ), ), body: WillPopScope( onWillPop: () async { if (_lastPressed == null || DateTime.now().difference(_lastPressed!) > _exitDuration) { //两次点击间隔超过2秒则重新计时 _lastPressed = DateTime.now(); ToastUtil.show("再次点击返回键退出"); return false; } exit(0); }, child: PageView.builder( itemBuilder: (ctx, index) => _pages[index], itemCount: _pages.length, controller: _pageController, physics: NeverScrollableScrollPhysics(), onPageChanged: (index) { Application.currentPage = index; }, ), ), ); } }