calendar.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import 'dart:async';
  2. import 'package:fast/constants.dart';
  3. import 'package:fast/model/model.dart';
  4. import 'package:fast/utils/api.dart';
  5. import 'package:fast/utils/global.dart';
  6. import 'package:fast/utils/http_utils.dart';
  7. import 'package:fast/utils/size_fit.dart';
  8. import 'package:fast/view/calendar_detail.dart';
  9. import 'package:fast/view/component/calendar_item.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:get/get.dart';
  12. GlobalKey todayKey = GlobalKey();
  13. GlobalKey listKey = GlobalKey();
  14. class Calendar extends StatefulWidget {
  15. const Calendar({Key? key}) : super(key: key);
  16. @override
  17. _CalendarState createState() => _CalendarState();
  18. }
  19. class _CalendarState extends State<Calendar> {
  20. late List _dates = [];
  21. late ScrollController controller;
  22. List<String> weeks = ['日', '一', '二', '三', '四', '五', '六'];
  23. List<dynamic> calendarList = [];
  24. // List<Widget> widgetList = <Widget>[];
  25. @override
  26. void initState() {
  27. super.initState();
  28. // SizeFit.initialize(context);
  29. controller = ScrollController(initialScrollOffset: 0.0);
  30. getCalendar();
  31. // Timer(const Duration(seconds: 2), () {
  32. // showDialog(
  33. // context: context,
  34. // barrierDismissible: false,
  35. // barrierColor: Colors.transparent,
  36. // useSafeArea: false,
  37. // builder: (BuildContext context) {
  38. // return const Checkin();
  39. // });
  40. // });
  41. /*
  42. Timer(const Duration(seconds: 2), () {
  43. showDialog(
  44. context: context,
  45. barrierDismissible: false,
  46. barrierColor: Colors.transparent,
  47. builder: (BuildContext context) {
  48. return Toast(
  49. title: '准时打卡',
  50. content: Row(
  51. mainAxisAlignment: MainAxisAlignment.center,
  52. children: [
  53. Text(
  54. '+13',
  55. style: TextStyle(
  56. color: Colors.white,
  57. fontSize: 16.px,
  58. fontWeight: FontWeight.w800,
  59. fontFamily: 'Exo2',
  60. decoration: TextDecoration.none),
  61. ),
  62. SizedBox(
  63. width: 3.px,
  64. ),
  65. Image.asset(
  66. 'assets/images/stone.png',
  67. width: 24.px,
  68. height: 24.px,
  69. )
  70. ],
  71. ),
  72. );
  73. });
  74. });*/
  75. }
  76. Future getCalendar() async {
  77. Map<String, dynamic> data = await HttpUtils.get(Api.calendars,
  78. params: {"day_begin": "20220301", "day_end": "20221212"});
  79. initCalendar(data);
  80. }
  81. DateTime serverTime() {
  82. int milliseconds = DateTime.now().millisecondsSinceEpoch;
  83. milliseconds = milliseconds + Global().timeSeconds * 1000;
  84. return DateTime.fromMillisecondsSinceEpoch(milliseconds);
  85. }
  86. initCalendar(Map<String, dynamic> data) {
  87. DateTime date = serverTime(); //DateTime.now();
  88. int todayYear = date.year;
  89. int todayMonth = date.month;
  90. int todayDay = date.day;
  91. List<FinishDayBean> finishs = [];
  92. List days = data['days'];
  93. for (int i = 0; i < days.length; i++) {
  94. FinishDayBean bean = FinishDayBean.fromJson(days[i]);
  95. finishs.add(bean);
  96. }
  97. if (finishs.isNotEmpty) {
  98. FinishDayBean bean = finishs[0];
  99. date = DateTime(bean.year, bean.month, 1);
  100. }
  101. int beginYear = date.year;
  102. int beginMonth = date.month;
  103. DateTime endDate = DateTime(beginYear, beginMonth + 1, 1);
  104. // int endYear = endDate.year;
  105. // int endMonth = endDate.month;
  106. List<dynamic> calendars = [];
  107. while (date.millisecondsSinceEpoch <= endDate.millisecondsSinceEpoch) {
  108. int dayCount = DateTime(date.year, date.month + 1, 0).day;
  109. int week = date.weekday;
  110. int year = date.year;
  111. int month = date.month;
  112. List<CalendarItemBean> array = [];
  113. for (int i = 0; i < week; i++) {
  114. CalendarItemBean item = CalendarItemBean();
  115. item.year = year;
  116. item.month = month;
  117. array.add(item);
  118. }
  119. for (int i = 0; i < dayCount; i++) {
  120. CalendarItemBean item = CalendarItemBean();
  121. if (todayYear == year && todayMonth == month && i + 1 == todayDay) {
  122. item.isToday = true;
  123. }
  124. for (int j = 0; j < finishs.length; j++) {
  125. FinishDayBean bean = finishs[j];
  126. if (bean.year == year && bean.month == month && i + 1 == bean.day) {
  127. item.finishDayBean = bean;
  128. }
  129. }
  130. item.year = date.year;
  131. item.month = date.month;
  132. item.day = i + 1;
  133. array.add(item);
  134. }
  135. calendars.add(array);
  136. date = DateTime(date.year, date.month + 1, 1);
  137. }
  138. bool isFind = false;
  139. if (data['ongoing_fasting'] != null) {
  140. List<dynamic> list1 = data['ongoing_fasting']['checkin_days'];
  141. if (data['ongoing_fasting']['mode'] == 'SINGLE') {
  142. list1 = [
  143. {
  144. 'day_num': data['ongoing_fasting']['begin_day_num'],
  145. "cross_day": data['ongoing_fasting']['cross_day'],
  146. 'checkin_status': 'PENDING',
  147. 'checkout_status': 'PENDING'
  148. }
  149. ];
  150. }
  151. // List<CheckinBean> list = [];
  152. for (int i = 0; i < list1.length; i++) {
  153. CheckinBean bean = CheckinBean.fromJson(list1[i]);
  154. DateTime nextDay = DateTime(bean.year, bean.month, bean.day + 1);
  155. for (List<CalendarItemBean> array in calendars) {
  156. for (CalendarItemBean item in array) {
  157. if (bean.year == item.year &&
  158. bean.month == item.month &&
  159. bean.day == item.day) {
  160. if (bean.checkout_status == 'PENDING' ||
  161. bean.checkin_status == 'PENDING') {
  162. item.isHighlight = true;
  163. if (isFind) {
  164. if (item.isIng == false) {
  165. item.showCircle = true;
  166. }
  167. } else {
  168. if (bean.cross_day == false) {
  169. item.isIng = true;
  170. isFind = true;
  171. }
  172. }
  173. }
  174. }
  175. if (bean.cross_day == true &&
  176. nextDay.year == item.year &&
  177. nextDay.month == item.month &&
  178. nextDay.day == item.day) {
  179. if (bean.checkout_status == 'PENDING' ||
  180. bean.checkin_status == 'PENDING') {
  181. item.isHighlight = true;
  182. if (!isFind) {
  183. item.isIng = true;
  184. } else {
  185. item.showCircle = true;
  186. }
  187. isFind = true;
  188. }
  189. }
  190. }
  191. }
  192. // list.add(bean);
  193. }
  194. }
  195. // print(calendars.toString());
  196. if (mounted) {
  197. setState(() {
  198. calendarList = calendars;
  199. });
  200. }
  201. }
  202. @override
  203. void dispose() {
  204. controller.dispose();
  205. super.dispose();
  206. }
  207. @override
  208. Widget build(BuildContext context) {
  209. SizeFit.initialize(context);
  210. var screenW = MediaQuery.of(context).size.width;
  211. return Container(
  212. alignment: Alignment.topLeft,
  213. // padding: EdgeInsets.only(left: 12.px, right: 12.px),
  214. color: kBgColor,
  215. // padding: EdgeInsets.only(left: 13.px,right: 12.px),
  216. child: Stack(
  217. children: [
  218. Flex(
  219. direction: Axis.vertical,
  220. children: [
  221. SizedBox(
  222. height: 40.px,
  223. ),
  224. // Container(
  225. // color: kBgColor,
  226. // height: 40.px,
  227. // child: week(),
  228. // ),
  229. Expanded(
  230. child: SingleChildScrollView(
  231. key: listKey,
  232. controller: controller,
  233. physics: const BouncingScrollPhysics(),
  234. child: Container(
  235. padding: EdgeInsets.only(left: 12.px, right: 12.px),
  236. child: Column(children: [
  237. Column(
  238. children:
  239. List<Widget>.generate(calendarList.length, (index) {
  240. List<CalendarItemBean> list = calendarList[index];
  241. return Flex(
  242. direction: Axis.vertical,
  243. crossAxisAlignment: CrossAxisAlignment.start,
  244. children: [
  245. if (list[0].month == 1)
  246. Container(
  247. margin: EdgeInsets.only(bottom: 8.px),
  248. child: Text(
  249. list[0].year.toString(),
  250. style: TextStyle(
  251. color: const Color(0x66FFFFFF),
  252. fontSize: 24.px,
  253. fontFamily: 'Exo2',
  254. height: 1,
  255. fontWeight: FontWeight.w900),
  256. ),
  257. ),
  258. SizedBox(
  259. height: 10.px,
  260. ),
  261. Row(
  262. children: [
  263. Text(
  264. '${list[0].month}月',
  265. style: TextStyle(
  266. fontWeight: FontWeight.bold,
  267. color: const Color(0x66C4CCDA),
  268. fontFamily: 'Exo2',
  269. height: 1.0,
  270. fontSize: 20.px),
  271. ),
  272. Expanded(
  273. child: Container(
  274. height: 1.px,
  275. margin: EdgeInsets.only(left: 12.px),
  276. // width: 297.px,
  277. color: const Color(0x1AFFFFFF),
  278. ))
  279. ],
  280. ),
  281. Wrap(
  282. direction: Axis.horizontal,
  283. alignment: WrapAlignment.start,
  284. spacing: 0,
  285. runSpacing: 0,
  286. children:
  287. List<Widget>.generate(list.length, (i) {
  288. return GestureDetector(
  289. onTap: () {
  290. if (list[i].day != null) {
  291. Get.to(() => CalendarDetail(
  292. calendarList: calendarList,
  293. selYear: list[i].year!,
  294. selMonth: list[i].month!,
  295. selDay: list[i].day!,
  296. ));
  297. }
  298. },
  299. child: CalendarItem(bean: list[i]));
  300. // return dayItem(list[i]);
  301. }),
  302. )
  303. ],
  304. );
  305. }),
  306. ),
  307. Container(
  308. width: 200.px,
  309. height: 100.px,
  310. color: Colors.transparent,
  311. )
  312. ]),
  313. ),
  314. ),
  315. )
  316. ],
  317. ),
  318. Container(
  319. height: 40.px,
  320. padding: EdgeInsets.only(left: 12.px, right: 12.px),
  321. decoration: BoxDecoration(
  322. color: kBgColor,
  323. boxShadow: [BoxShadow(
  324. color: const Color(0x80000000),
  325. blurRadius: 24.px,
  326. offset: Offset(0, 16.px))]
  327. ),
  328. child: week(),
  329. ),
  330. Positioned(
  331. left: 0,
  332. bottom: 0,
  333. child: Container(
  334. width: 375.px,
  335. height: 148.px,
  336. decoration: const BoxDecoration(
  337. gradient: LinearGradient(
  338. begin: Alignment.topCenter,
  339. end: Alignment.bottomCenter,
  340. colors: [Color(0x00050F1A), Color(0xFF050F1A)])),
  341. ))
  342. ],
  343. ),
  344. );
  345. }
  346. Widget week() {
  347. DateTime now = DateTime.now();
  348. int weekIndex = now.weekday;
  349. if (weekIndex == 7) {
  350. weekIndex = 0;
  351. }
  352. List<Widget> weekday = [];
  353. for (int i = 0; i < weeks.length; i++) {
  354. var widget = Expanded(
  355. child: Container(
  356. alignment: Alignment.center,
  357. child: Image.asset(i == weekIndex
  358. ? 'assets/images/week_' + i.toString() + '_sel.png'
  359. : 'assets/images/week_' + i.toString() + '.png'),
  360. width: 25.px,
  361. height: 24.px,
  362. ));
  363. weekday.add(widget);
  364. }
  365. return Flex(direction: Axis.horizontal, children: weekday);
  366. }
  367. }