| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- import 'dart:async';
- import 'package:fast/constants.dart';
- import 'package:fast/model/model.dart';
- import 'package:fast/utils/api.dart';
- import 'package:fast/utils/global.dart';
- import 'package:fast/utils/http_utils.dart';
- import 'package:fast/utils/size_fit.dart';
- import 'package:fast/view/calendar_detail.dart';
- import 'package:fast/view/component/calendar_item.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- GlobalKey todayKey = GlobalKey();
- GlobalKey listKey = GlobalKey();
- class Calendar extends StatefulWidget {
- const Calendar({Key? key}) : super(key: key);
- @override
- _CalendarState createState() => _CalendarState();
- }
- class _CalendarState extends State<Calendar> {
- late List _dates = [];
- late ScrollController controller;
- List<String> weeks = ['日', '一', '二', '三', '四', '五', '六'];
- List<dynamic> calendarList = [];
- // List<Widget> widgetList = <Widget>[];
- @override
- void initState() {
- super.initState();
- // SizeFit.initialize(context);
- controller = ScrollController(initialScrollOffset: 0.0);
- getCalendar();
- // Timer(const Duration(seconds: 2), () {
- // showDialog(
- // context: context,
- // barrierDismissible: false,
- // barrierColor: Colors.transparent,
- // useSafeArea: false,
- // builder: (BuildContext context) {
- // return const Checkin();
- // });
- // });
- /*
- Timer(const Duration(seconds: 2), () {
- showDialog(
- context: context,
- barrierDismissible: false,
- barrierColor: Colors.transparent,
- builder: (BuildContext context) {
- return Toast(
- title: '准时打卡',
- content: Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Text(
- '+13',
- style: TextStyle(
- color: Colors.white,
- fontSize: 16.px,
- fontWeight: FontWeight.w800,
- fontFamily: 'Exo2',
- decoration: TextDecoration.none),
- ),
- SizedBox(
- width: 3.px,
- ),
- Image.asset(
- 'assets/images/stone.png',
- width: 24.px,
- height: 24.px,
- )
- ],
- ),
- );
- });
- });*/
- }
- Future getCalendar() async {
- Map<String, dynamic> data = await HttpUtils.get(Api.calendars,
- params: {"day_begin": "20220301", "day_end": "20221212"});
- initCalendar(data);
- }
- DateTime serverTime() {
- int milliseconds = DateTime.now().millisecondsSinceEpoch;
- milliseconds = milliseconds + Global().timeSeconds * 1000;
- return DateTime.fromMillisecondsSinceEpoch(milliseconds);
- }
- initCalendar(Map<String, dynamic> data) {
- DateTime date = serverTime(); //DateTime.now();
- int todayYear = date.year;
- int todayMonth = date.month;
- int todayDay = date.day;
- List<FinishDayBean> finishs = [];
- List days = data['days'];
- for (int i = 0; i < days.length; i++) {
- FinishDayBean bean = FinishDayBean.fromJson(days[i]);
- finishs.add(bean);
- }
- if (finishs.isNotEmpty) {
- FinishDayBean bean = finishs[0];
- date = DateTime(bean.year, bean.month, 1);
- }
- int beginYear = date.year;
- int beginMonth = date.month;
- DateTime endDate = DateTime(beginYear, beginMonth + 1, 1);
- // int endYear = endDate.year;
- // int endMonth = endDate.month;
- List<dynamic> calendars = [];
- while (date.millisecondsSinceEpoch <= endDate.millisecondsSinceEpoch) {
- int dayCount = DateTime(date.year, date.month + 1, 0).day;
- int week = date.weekday;
- int year = date.year;
- int month = date.month;
- List<CalendarItemBean> array = [];
- for (int i = 0; i < week; i++) {
- CalendarItemBean item = CalendarItemBean();
- item.year = year;
- item.month = month;
- array.add(item);
- }
- for (int i = 0; i < dayCount; i++) {
- CalendarItemBean item = CalendarItemBean();
- if (todayYear == year && todayMonth == month && i + 1 == todayDay) {
- item.isToday = true;
- }
- for (int j = 0; j < finishs.length; j++) {
- FinishDayBean bean = finishs[j];
- if (bean.year == year && bean.month == month && i + 1 == bean.day) {
- item.finishDayBean = bean;
- }
- }
- item.year = date.year;
- item.month = date.month;
- item.day = i + 1;
- array.add(item);
- }
- calendars.add(array);
- date = DateTime(date.year, date.month + 1, 1);
- }
- bool isFind = false;
- if (data['ongoing_fasting'] != null) {
- List<dynamic> list1 = data['ongoing_fasting']['checkin_days'];
- if (data['ongoing_fasting']['mode'] == 'SINGLE') {
- list1 = [
- {
- 'day_num': data['ongoing_fasting']['begin_day_num'],
- "cross_day": data['ongoing_fasting']['cross_day'],
- 'checkin_status': 'PENDING',
- 'checkout_status': 'PENDING'
- }
- ];
- }
- // List<CheckinBean> list = [];
- for (int i = 0; i < list1.length; i++) {
- CheckinBean bean = CheckinBean.fromJson(list1[i]);
- DateTime nextDay = DateTime(bean.year, bean.month, bean.day + 1);
- for (List<CalendarItemBean> array in calendars) {
- for (CalendarItemBean item in array) {
- if (bean.year == item.year &&
- bean.month == item.month &&
- bean.day == item.day) {
- if (bean.checkout_status == 'PENDING' ||
- bean.checkin_status == 'PENDING') {
- item.isHighlight = true;
- if (isFind) {
- if (item.isIng == false) {
- item.showCircle = true;
- }
- } else {
- if (bean.cross_day == false) {
- item.isIng = true;
- isFind = true;
- }
- }
- }
- }
- if (bean.cross_day == true &&
- nextDay.year == item.year &&
- nextDay.month == item.month &&
- nextDay.day == item.day) {
- if (bean.checkout_status == 'PENDING' ||
- bean.checkin_status == 'PENDING') {
- item.isHighlight = true;
- if (!isFind) {
- item.isIng = true;
- } else {
- item.showCircle = true;
- }
- isFind = true;
- }
- }
- }
- }
- // list.add(bean);
- }
- }
- // print(calendars.toString());
- if (mounted) {
- setState(() {
- calendarList = calendars;
- });
- }
- }
- @override
- void dispose() {
- controller.dispose();
- super.dispose();
- }
- @override
- Widget build(BuildContext context) {
- SizeFit.initialize(context);
- var screenW = MediaQuery.of(context).size.width;
- return Container(
- alignment: Alignment.topLeft,
- // padding: EdgeInsets.only(left: 12.px, right: 12.px),
- color: kBgColor,
- // padding: EdgeInsets.only(left: 13.px,right: 12.px),
- child: Stack(
- children: [
- Flex(
- direction: Axis.vertical,
- children: [
- SizedBox(
- height: 40.px,
- ),
- // Container(
- // color: kBgColor,
- // height: 40.px,
- // child: week(),
- // ),
- Expanded(
- child: SingleChildScrollView(
- key: listKey,
- controller: controller,
- physics: const BouncingScrollPhysics(),
- child: Container(
- padding: EdgeInsets.only(left: 12.px, right: 12.px),
- child: Column(children: [
- Column(
- children:
- List<Widget>.generate(calendarList.length, (index) {
- List<CalendarItemBean> list = calendarList[index];
- return Flex(
- direction: Axis.vertical,
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- if (list[0].month == 1)
- Container(
- margin: EdgeInsets.only(bottom: 8.px),
- child: Text(
- list[0].year.toString(),
- style: TextStyle(
- color: const Color(0x66FFFFFF),
- fontSize: 24.px,
- fontFamily: 'Exo2',
- height: 1,
- fontWeight: FontWeight.w900),
- ),
- ),
- SizedBox(
- height: 10.px,
- ),
- Row(
- children: [
- Text(
- '${list[0].month}月',
- style: TextStyle(
- fontWeight: FontWeight.bold,
- color: const Color(0x66C4CCDA),
- fontFamily: 'Exo2',
- height: 1.0,
- fontSize: 20.px),
- ),
- Expanded(
- child: Container(
- height: 1.px,
- margin: EdgeInsets.only(left: 12.px),
- // width: 297.px,
- color: const Color(0x1AFFFFFF),
- ))
- ],
- ),
- Wrap(
- direction: Axis.horizontal,
- alignment: WrapAlignment.start,
- spacing: 0,
- runSpacing: 0,
- children:
- List<Widget>.generate(list.length, (i) {
- return GestureDetector(
- onTap: () {
- if (list[i].day != null) {
- Get.to(() => CalendarDetail(
- calendarList: calendarList,
- selYear: list[i].year!,
- selMonth: list[i].month!,
- selDay: list[i].day!,
- ));
- }
- },
- child: CalendarItem(bean: list[i]));
- // return dayItem(list[i]);
- }),
- )
- ],
- );
- }),
- ),
- Container(
- width: 200.px,
- height: 100.px,
- color: Colors.transparent,
- )
- ]),
- ),
- ),
- )
- ],
- ),
- Container(
- height: 40.px,
- padding: EdgeInsets.only(left: 12.px, right: 12.px),
- decoration: BoxDecoration(
- color: kBgColor,
- boxShadow: [BoxShadow(
- color: const Color(0x80000000),
- blurRadius: 24.px,
- offset: Offset(0, 16.px))]
- ),
- child: week(),
- ),
- Positioned(
- left: 0,
- bottom: 0,
- child: Container(
- width: 375.px,
- height: 148.px,
- decoration: const BoxDecoration(
- gradient: LinearGradient(
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- colors: [Color(0x00050F1A), Color(0xFF050F1A)])),
- ))
- ],
- ),
- );
- }
- Widget week() {
- DateTime now = DateTime.now();
- int weekIndex = now.weekday;
- if (weekIndex == 7) {
- weekIndex = 0;
- }
- List<Widget> weekday = [];
- for (int i = 0; i < weeks.length; i++) {
- var widget = Expanded(
- child: Container(
- alignment: Alignment.center,
- child: Image.asset(i == weekIndex
- ? 'assets/images/week_' + i.toString() + '_sel.png'
- : 'assets/images/week_' + i.toString() + '.png'),
- width: 25.px,
- height: 24.px,
- ));
- weekday.add(widget);
- }
- return Flex(direction: Axis.horizontal, children: weekday);
- }
- }
|