123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- import 'package:flutter/material.dart';
- import 'package:paged_vertical_calendar/paged_vertical_calendar.dart';
- import 'package:provider/provider.dart';
- import 'package:sport/bean/sport_detail.dart';
- import 'package:sport/services/api/inject_api.dart';
- import 'package:sport/services/api/resp.dart';
- import 'package:sport/widgets/image.dart';
- class SportDataCalendarPage extends StatefulWidget {
- final DateTime time;
- const SportDataCalendarPage({
- Key? key,
- required this.time,
- }) : super(key: key);
- @override
- State<StatefulWidget> createState() => _PageState();
- }
- class _PageState extends State<SportDataCalendarPage> with InjectApi {
- late PageController _pageController;
- @override
- void initState() {
- super.initState();
- DateTime t = DateTime.now();
- DateTime now = DateTime(t.year, t.month, t.day);
- _pageController = PageController(initialPage: now.year - widget.time.year);
- }
- @override
- void dispose() {
- _pageController.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- var today = DateTime.now().difference(widget.time).inDays == 0 ? true : false;
- return Scaffold(
- backgroundColor: Colors.transparent,
- body: Column(
- children: [
- Padding(
- padding: const EdgeInsets.all(16.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text("选择时间"),
- InkWell(
- onTap: () {
- Navigator.maybePop(context, DateTime.now());
- },
- child: Image.asset(
- "lib/assets/img/icon_table_today_select.png",
- width: 16,
- color: today ? Color(0xffFF5B1D) : null,
- ),
- ),
- ],
- ),
- ),
- Expanded(
- child: FutureProvider<List<SportHistory>>(
- create: (context) async {
- DateTime t = DateTime.now();
- try {
- RespList<SportHistory> resp = await api.getSportRecordHistory(t.year);
- return resp.results;
- } catch (e) {
- print(e);
- }
- return [];
- },
- initialData: [],
- builder: (context, child) {
- return PageView.builder(
- controller: _pageController,
- reverse: true,
- itemCount: 1000,
- itemBuilder: (context, index) {
- DateTime t = DateTime.now();
- DateTime now = DateTime(t.year, t.month, t.day);
- DateTime startDate = DateTime(now.year - index, 1, 1);
- DateTime endDate = DateTime(now.year + 1 - index, 1, 1);
- if (index == 0) {
- endDate = DateTime(now.year, now.month + 1, 1);
- }
- DateTime ee = endDate.subtract(Duration(days: 1));
- DateTime initialDate = DateTime(startDate.year, widget.time.month, widget.time.day);
- var _dot = Align(
- alignment: Alignment.bottomCenter,
- child: Container(
- width: 4,
- height: 4,
- margin: EdgeInsets.only(bottom: 6),
- decoration: BoxDecoration(shape: BoxShape.circle, color: const Color(0xffFF5B1D)),
- ));
- List<SportHistory> items = context.watch<List<SportHistory>>();
- SportHistory? history;
- for (var i in items) {
- if (i.year == startDate.year) {
- history = i;
- break;
- }
- }
- print("11111111 == $index == $now $startDate $endDate $ee ${widget.time} $initialDate items ${items.length}");
- return Column(
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceEvenly,
- children: <Widget>[
- GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: () => _pageController.nextPage(duration: Duration(milliseconds: 500), curve: Curves.linear),
- child: Padding(
- padding: const EdgeInsets.all(18.0),
- child: arrowLeft(),
- ),
- ),
- Text(
- "${startDate.year}",
- style: Theme.of(context).textTheme.headline3,
- ),
- index == 0
- ? Padding(
- padding: const EdgeInsets.all(18.0),
- child: arrowRight(color: Color(0x90cccccc)),
- )
- : GestureDetector(
- behavior: HitTestBehavior.opaque,
- onTap: () => _pageController.previousPage(duration: Duration(milliseconds: 500), curve: Curves.linear),
- child: Padding(
- padding: const EdgeInsets.all(18.0),
- child: arrowRight(),
- ),
- ),
- ],
- ),
- Row(
- children: ["一", "二", "三", "四", "五", "六", "日"].map((e) => Container(margin: const EdgeInsets.all(6.0), child: Text("$e"))).toList(),
- mainAxisAlignment: MainAxisAlignment.spaceAround,
- ),
- Expanded(
- child: PagedVerticalCalendar(
- startDate: startDate,
- endDate: ee,
- initialDate: initialDate,
- listPadding: EdgeInsets.all(8),
- monthBuilder: (context, month, year) {
- return Padding(
- padding: const EdgeInsets.all(12.0),
- child: Text(
- "$year年$month月",
- style: Theme.of(context).textTheme.subtitle1!,
- ),
- );
- },
- dayBuilder: (context, date) {
- // print("2222222222222222 == $now $date ${widget.time} ${now.difference(date).inDays}");
- bool hasData = false;
- if (history != null) {
- // print("3333333333333333 == ${history.year} ${history.data.length}");
- for (var i = 0; i < history.data.length; i++) {
- var m = history.data[i];
- // print("3333333333333333 == $m ");
- for (var d in m) {
- if (i == date.month - 1 && d == date.day) {
- // print("3333333333333333 == $m ${date.month} $d ${date.day}");
- hasData = true;
- }
- }
- }
- }
- return Container(
- margin: const EdgeInsets.all(6.0),
- decoration: widget.time.difference(date).inDays == 0 ? BoxDecoration(shape: BoxShape.circle, border: Border.all(width: 1, color: const Color(0xffFF5B1D))) : null,
- child: Stack(
- fit: StackFit.expand,
- children: [
- Center(
- child: Text(
- "${date.day}",
- style: now.difference(date).inDays >= 0 ? Theme.of(context).textTheme.subtitle1!.copyWith(fontSize: 16.0) : Theme.of(context).textTheme.bodyText2!.copyWith(fontSize: 16.0),
- )),
- if (hasData == true) _dot,
- ],
- ),
- );
- },
- onDayPressed: (date) {
- if (DateTime.now().difference(date).inSeconds < 0) return;
- Navigator.maybePop(context, date);
- },
- invisibleMonthsThreshold: 2,
- onMonthLoaded: (y, m) {},
- ),
- ),
- ],
- );
- });
- }),
- ),
- ],
- ),
- );
- }
- }
|