import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:sport/pages/data/sport_data_calendar.dart'; import 'package:sport/pages/data/sport_data_detail.dart'; import 'package:sport/utils/date.dart'; import 'package:sport/widgets/appbar.dart'; import 'package:sport/widgets/round_tab_indicator.dart'; import 'package:umeng_common_sdk/umeng_common_sdk.dart'; class SportDataPage extends StatefulWidget { final int initialPage; const SportDataPage({Key? key, this.initialPage = 0}) : super(key: key); @override State createState() => SportDataPageState(); static SportDataPageState? of(BuildContext context) { return context.findAncestorStateOfType(); } } class SportDataPageState extends State { final List TABS = ["日", "周", "月", "年"]; int _page = 0; int _type = 0; int _sportType = 0; late PageController _pageController; DateTime? _selectDate; @override void initState() { _type = widget.initialPage; _pageController = PageController(); super.initState(); } Future showCalendar(DateTime time) async { DateTime? select = await showModalBottomSheet( context: context, shape: RoundedRectangleBorder( borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10)), ), builder: (context) => SportDataCalendarPage( time: time, )); if (select != null) { _selectDate = select; _pageController.jumpToPage(getInitialPage()); } } int getInitialPage(){ DateTime? select = _selectDate; if(select == null) return 0; DateTime t = DateTime.now(); DateTime now = DateTime(t.year, t.month, t.day); int page = 0; if (_type == 0) { page = now.difference(select).inDays.abs(); } else if (_type == 1) { DateTime now = DateTime(t.year, t.month, t.day - t.weekday + 1); DateTime ss = DateTime(select.year, select.month, select.day - select.weekday + 1); page = (now.difference(ss).inDays.abs() / 7).round(); } else if (_type == 2) { page = (now.year - select.year) * 12 + ( now.month - select.month); } else if (_type == 3) { page = now.year - select.year; } return page; } @override Widget build(BuildContext context) { var _bg = const Color(0xff241D19); return DefaultTabController( initialIndex: widget.initialPage, length: TABS.length, child: Scaffold( appBar: AppBar( leading: buildBackButton(context, white: true), systemOverlayStyle: SystemUiOverlayStyle.light, centerTitle: false, backgroundColor: _bg, title: Container( height: 40, color: _bg, padding: const EdgeInsets.only(right: kMinInteractiveDimension), child: Center( child: TabBar( isScrollable: false, labelColor: Colors.white, labelStyle: const TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600, height: 1.2), unselectedLabelColor: Color(0xff999999), unselectedLabelStyle: const TextStyle(fontSize: 18.0, fontWeight: FontWeight.w600, height: 1.2), indicatorSize: TabBarIndicatorSize.label, indicator: RoundUnderlineTabIndicator(borderSide: BorderSide(color: Theme.of(context).indicatorColor, width: 3), width: 0), onTap: (index) { setState(() { _type = index; _selectDate = null; _page = 0; _pageController = PageController(initialPage: _page); }); UmengCommonSdk.onEvent("sport_data_date_type_$index", {}); }, tabs: TABS .map((e) => Tab( text: e, )) .toList(), ), ), ), actions: [ Container() // GestureDetector( // onTap: () {}, // child: Image.asset("lib/assets/img/bbs_icon_share.png", color: Colors.white), // ), ], ), body: NotificationListener( onNotification: (details) { if (details is PageNotification) { if (details.page > 0) { _pageController.animateToPage(min(10240,++_page), duration: Duration(milliseconds: 500), curve: Curves.linear); } else { _pageController.animateToPage(max(0,--_page), duration: Duration(milliseconds: 500), curve: Curves.linear); } } else if (details is SportTypeNotification) { _sportType = details.index; } return true; }, child: PageView.builder( key: ValueKey("${_type}_${_selectDate?.toString()}"), controller: _pageController, // physics: NeverScrollableScrollPhysics(), onPageChanged: (page){ _page = page; }, reverse: true, itemCount: 10240, itemBuilder: (context, index) { var type = _type; var time = offsetDate(type, -index); return SportDataDetailPage( type: type, sportType: _sportType, time: time, index: index, selectDate: _selectDate, ); }, ), )), ); } } class PageNotification extends Notification { PageNotification({ required this.page, }); final int page; } class SportTypeNotification extends Notification { SportTypeNotification({ required this.index, }); final int index; }