123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- 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<HomePage> {
- var _pageController = PageController();
- DateTime? _lastPressed;
- ValueNotifier<int> _valueNotifierIndex = ValueNotifier(0);
- final Duration _exitDuration = Duration(seconds: 2);
- final GlobalKey _key = GlobalKey();
- late MessageModel _messageModel;
- late UserModel userModel;
- late List<Widget> _pages;
- @override
- void initState() {
- super.initState();
- if (openSocial()) {
- _pages = <Widget>[HomeInfoPage(), GamePage(), SocialIndexPage(), MyPage()];
- } else {
- _pages = <Widget>[HomeInfoPage(), GamePage(), MyPage()];
- }
- _messageModel = GetIt.I<MessageModel>();
- GetIt.I<Bluetooth>().init(context);
- userModel = Provider.of<UserModel>(context, listen: false);
- SchedulerBinding.instance?.addPostFrameCallback((timeStamp) async {
- UserModel userModel = Provider.of<UserModel>(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<Bluetooth>().listen(context);
- _messageModel.loop(context);
- GetIt.I<GameModel>().resumed();
- // SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
- break;
- case AppLifecycleState.paused:
- GetIt.I<Bluetooth>().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<SystemUiOverlayStyle>(
- 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>[
- 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;
- },
- ),
- ),
- );
- }
- }
|