123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- 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/new_social_index_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<HomePage> with ConfigInject {
- var _pageController = PageController();
- DateTime _lastPressed;
- ValueNotifier<int> _valueNotifierIndex = ValueNotifier(0);
- final Duration _exitDuration = Duration(seconds: 2);
- final List<Widget> _pages = <Widget>[HomeInfoPage(), GamePage(), NewSocialIndexPage(), MyPage()];
- @override
- void initState() {
- super.initState();
- // Provider.of<Bluetooth>(context, listen: false).listen();
- updateApp(context);
- Provider.of<MessageModel>(context, listen: false).init(); // 这就开始轮询了?
- }
- @override
- void didChangeAppLifecycleState(AppLifecycleState state) {
- super.didChangeAppLifecycleState(state);
- print("$state");
- switch (state) {
- case AppLifecycleState.resumed:
- Provider.of<Bluetooth>(context, listen: false).listen();
- break;
- case AppLifecycleState.paused:
- Provider.of<Bluetooth>(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>[
- 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;
- },
- ),
- ),
- );
- }
- }
|