duration_page.dart 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. import 'dart:math';
  2. import 'dart:ui';
  3. import 'package:dartin/dartin.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  7. import 'package:sport/bean/sport_target_index.dart';
  8. import 'package:sport/pages/home/duration_setting_page.dart';
  9. import 'package:sport/router/navigator_util.dart';
  10. import 'package:sport/services/api/rest_client.dart';
  11. import 'package:sport/utils/date.dart';
  12. import 'package:sport/utils/toast.dart';
  13. import 'package:sport/widgets/appbar.dart';
  14. import 'package:sport/widgets/button_primary.dart';
  15. import 'package:sport/widgets/circular_percent_indicator.dart';
  16. import 'package:sport/widgets/decoration.dart';
  17. import 'package:sport/widgets/dialog/request_dialog.dart';
  18. import 'package:sport/widgets/image.dart';
  19. import 'package:sport/widgets/loading.dart';
  20. import 'package:sport/widgets/misc.dart';
  21. import 'package:sport/widgets/space.dart';
  22. var _succColor = const Color(0xff5498FF);
  23. var _failColor = const Color(0xffFF5B1D);
  24. class DurationPage extends StatefulWidget {
  25. @override
  26. State<StatefulWidget> createState() => _PageState();
  27. }
  28. class _PageState extends State<DurationPage> {
  29. ValueNotifier<DateTime> _valueNotifierDate = ValueNotifier(DateTime.now());
  30. PageController _pageController;
  31. @override
  32. void initState() {
  33. super.initState();
  34. _pageController = PageController(initialPage: 0);
  35. }
  36. @override
  37. void dispose() {
  38. _pageController?.dispose();
  39. super.dispose();
  40. _valueNotifierDate?.dispose();
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. var _dot = Container(
  45. width: 5,
  46. height: 5,
  47. decoration: BoxDecoration(shape: BoxShape.circle, color: _succColor),
  48. );
  49. var _dotFailed = Container(
  50. width: 5,
  51. height: 5,
  52. decoration: BoxDecoration(shape: BoxShape.circle, color: _failColor),
  53. );
  54. return Scaffold(
  55. body: FutureBuilder(
  56. future: inject<RestClient>().getSportTargetIndex().asStream().map((event) => event.data).last,
  57. builder: (BuildContext context, AsyncSnapshot<SportTargetIndex> snapshot) => snapshot.connectionState != ConnectionState.done
  58. ? RequestLoadingWidget()
  59. : CustomScrollView(
  60. slivers: <Widget>[
  61. // SizeBox(),
  62. // SliverToBoxAdapter(
  63. // child: Space(height: 20.0,),
  64. // ),
  65. buildSliverAppBar(context, "运动目标", backgroundColor: Theme.of(context).scaffoldBackgroundColor, actions: [
  66. IconButton(
  67. icon: Image.asset(
  68. "lib/assets/img/setgoals_icon_set.png",
  69. width: 22.0,
  70. ),
  71. highlightColor: Colors.red,
  72. onPressed: () async {
  73. await NavigatorUtil.goPage(context, (context) => DurationSettingPage());
  74. setState(() {});
  75. },
  76. )
  77. ]),
  78. SliverToBoxAdapter(
  79. child: Container(
  80. margin: const EdgeInsets.symmetric(horizontal: 12.0),
  81. padding: const EdgeInsets.only(top:20.0,bottom: 10.0,left: 16.0,right: 16.0),
  82. decoration: circular(),
  83. child: Column(
  84. children: <Widget>[
  85. Center(
  86. child: CircularPercentIndicator(
  87. radius: 140.0,
  88. lineWidth: 12.0,
  89. percent: 1.0 * (snapshot.data?.today?.value ?? 0) / snapshot.data?.today?.valueTarget ?? 0.01,
  90. center: Column(
  91. children: <Widget>[
  92. Text(
  93. "${snapshot.data?.today?.value ?? 0}",
  94. style: Theme.of(context).textTheme.headline2.copyWith(fontSize: 40.0, fontFamily: "DIN"),
  95. ),
  96. Text(
  97. "${snapshot.data?.today?.label}",
  98. style: Theme.of(context).textTheme.subtitle2,
  99. strutStyle: fixedLine,
  100. )
  101. ],
  102. mainAxisSize: MainAxisSize.min,
  103. ),
  104. animation: true,
  105. animationDuration: 1000,
  106. animateFromLastPercent: true,
  107. startAngle: 200.0,
  108. arcType: ArcType.CUSTOM,
  109. backgroundColor: Color(0xfff1f1f1),
  110. circularStrokeCap: CircularStrokeCap.round,
  111. rotateLinearGradient: true,
  112. linearGradient: LinearGradient(
  113. colors: <Color>[Color(0xff8DF7FF), Color(0xff16A2FF)],
  114. ),
  115. ),
  116. ),
  117. SizedBox(
  118. width: 15.0,
  119. ),
  120. Padding(
  121. padding: const EdgeInsets.symmetric(vertical: 12.0),
  122. child: Text(
  123. "运动目标:${snapshot.data?.target?.valueTarget ?? 0} ${snapshot.data?.today?.label}",
  124. style: Theme.of(context).textTheme.subtitle1,
  125. ),
  126. ),
  127. Divider(),
  128. Padding(
  129. padding: const EdgeInsets.symmetric(vertical: 12.0),
  130. child: Column(
  131. children: <Widget>[
  132. Row(
  133. children: <Widget>[
  134. Text(
  135. "目标状态",
  136. style: Theme.of(context).textTheme.subtitle1,
  137. ),
  138. snapshot.data?.today?.finish == true
  139. ? Text(
  140. "已达标",
  141. style: Theme.of(context).textTheme.bodyText2.copyWith(color: _succColor),
  142. )
  143. : Text(
  144. "未达标",
  145. style: Theme.of(context).textTheme.bodyText2.copyWith(color: _failColor),
  146. )
  147. ],
  148. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  149. ),
  150. const SizedBox(
  151. height: 8.0,
  152. ),
  153. Row(
  154. children: <Widget>[
  155. Text(
  156. "今日目标",
  157. style: Theme.of(context).textTheme.subtitle1,
  158. ),
  159. Text(
  160. "${snapshot.data?.today?.valueTarget ?? 0}${snapshot.data?.today?.label}",
  161. style: Theme.of(context).textTheme.bodyText2,
  162. )
  163. ],
  164. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  165. ),
  166. const SizedBox(
  167. height: 8.0,
  168. ),
  169. Row(
  170. children: <Widget>[
  171. Text(
  172. "累计消耗",
  173. style: Theme.of(context).textTheme.subtitle1,
  174. ),
  175. Text(
  176. "${snapshot.data?.today?.consume ?? 0}卡",
  177. style: Theme.of(context).textTheme.bodyText2,
  178. )
  179. ],
  180. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  181. ),
  182. const SizedBox(
  183. height: 8.0,
  184. ),
  185. Row(
  186. children: <Widget>[
  187. Text(
  188. "本月达标天数",
  189. style: Theme.of(context).textTheme.subtitle1,
  190. ),
  191. Text(
  192. "${snapshot.data?.target?.monthFinish ?? 0}天",
  193. style: Theme.of(context).textTheme.bodyText2,
  194. )
  195. ],
  196. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  197. )
  198. ],
  199. ))
  200. ],
  201. ),
  202. ),
  203. ),
  204. SliverToBoxAdapter(
  205. child: Padding(
  206. padding: const EdgeInsets.fromLTRB(12.0, 16.0 , 12.0,12.0),
  207. child: Column(
  208. crossAxisAlignment: CrossAxisAlignment.start,
  209. children: <Widget>[
  210. Text(
  211. "达标记录",
  212. style: Theme.of(context).textTheme.headline1.copyWith(fontSize: 16.0),
  213. ),
  214. const SizedBox(
  215. height: 16.0,
  216. ),
  217. Container(
  218. padding: const EdgeInsets.all(12.0),
  219. decoration: circular(),
  220. height: 406.0,
  221. child: Column(
  222. children: <Widget>[
  223. Center(
  224. child: ValueListenableBuilder<DateTime>(
  225. valueListenable: _valueNotifierDate,
  226. builder: (_, time, ___) {
  227. return Row(
  228. mainAxisSize: MainAxisSize.min,
  229. children: <Widget>[
  230. GestureDetector(
  231. behavior: HitTestBehavior.opaque,
  232. onTap: () {
  233. _pageController?.nextPage(duration: Duration(milliseconds: 500), curve: Curves.linear);
  234. },
  235. child: Padding(
  236. padding: const EdgeInsets.all(18.0),
  237. child: arrowLeft(),
  238. ),
  239. ),
  240. SizedBox(
  241. width: 30,
  242. ),
  243. Text(
  244. "${time.year}年${'${time.month}'.padLeft(2, '0')}月",
  245. style: Theme.of(context).textTheme.bodyText2.copyWith(color: Color(0xff333333)),
  246. strutStyle: fixedLine,
  247. ),
  248. SizedBox(
  249. width: 30,
  250. ),
  251. GestureDetector(
  252. behavior: HitTestBehavior.opaque,
  253. onTap: () {
  254. if (_pageController?.page == 0.0) {
  255. return;
  256. }
  257. _pageController?.previousPage(duration: Duration(milliseconds: 500), curve: Curves.linear);
  258. },
  259. child: Padding(
  260. padding: const EdgeInsets.all(18.0),
  261. child: arrowRight(),
  262. ),
  263. ),
  264. ],
  265. );
  266. }),
  267. ),
  268. Expanded(
  269. child: PageView.builder(
  270. reverse: true,
  271. itemCount: 10240,
  272. controller: _pageController,
  273. onPageChanged: (page) {
  274. DateTime next = offsetDate(2, -page);
  275. _valueNotifierDate.value = next;
  276. },
  277. itemBuilder: (context, index) {
  278. DateTime time = offsetDate(2, -index);
  279. return FutureBuilder<List<_DataItem>>(
  280. future: createFuture(time),
  281. builder: (BuildContext context, AsyncSnapshot<List<_DataItem>> snapshot) {
  282. var _value = snapshot?.data;
  283. return snapshot.connectionState != ConnectionState.done
  284. ? RequestLoadingWidget()
  285. : Padding(
  286. padding: const EdgeInsets.all(12.0),
  287. child: GridView.builder(
  288. physics: NeverScrollableScrollPhysics(),
  289. padding: EdgeInsets.zero,
  290. gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
  291. crossAxisCount: 7,
  292. ),
  293. itemBuilder: (_, index) {
  294. var item = _value[index];
  295. return Center(
  296. child: item.today == true
  297. ? Container(
  298. padding: const EdgeInsets.all(6.0),
  299. decoration: BoxDecoration(shape: BoxShape.circle, color: Theme.of(context).accentColor),
  300. child: Text("${item.title}",
  301. style: Theme.of(context).textTheme.subtitle1.copyWith(color: Colors.white)),
  302. )
  303. : Stack(
  304. children: <Widget>[
  305. Text("${item.title}",
  306. style: item.enable
  307. ? Theme.of(context).textTheme.bodyText2
  308. : Theme.of(context).textTheme.subtitle1),
  309. if (item.state > 0)
  310. Align(
  311. alignment: Alignment.bottomCenter,
  312. child: Padding(
  313. child: item.state == 2 ? _dot : _dotFailed,
  314. padding: const EdgeInsets.only(bottom: 4.0),
  315. ))
  316. ],
  317. alignment: Alignment.center,
  318. )
  319. // Stack(
  320. // children: <Widget>[
  321. // Text("${item.title}",
  322. // style: item.enable
  323. // ? Theme.of(context).textTheme.bodyText2
  324. // : Theme.of(context).textTheme.subtitle1),
  325. // Image.asset("lib/assets/img/date_state_day.png")
  326. // ],
  327. // alignment: Alignment.center,
  328. // )
  329. // : Image.asset("lib/assets/img/date_state_select.png")
  330. );
  331. },
  332. itemCount: 49,
  333. ),
  334. );
  335. });
  336. },
  337. ),
  338. ),
  339. Divider(),
  340. SizedBox(
  341. height: 5,
  342. ),
  343. Row(
  344. children: <Widget>[
  345. SizedBox(
  346. width: 16,
  347. ),
  348. _dot,
  349. SizedBox(
  350. width: 6,
  351. ),
  352. Text(
  353. "运动达标",
  354. style: Theme.of(context).textTheme.bodyText1,
  355. strutStyle: fixedLine,
  356. ),
  357. SizedBox(
  358. width: 45,
  359. ),
  360. _dotFailed,
  361. SizedBox(
  362. width: 6,
  363. ),
  364. Text(
  365. "运动不达标",
  366. style: Theme.of(context).textTheme.bodyText1,
  367. strutStyle: fixedLine,
  368. ),
  369. ],
  370. )
  371. ],
  372. ),
  373. ),
  374. ],
  375. ),
  376. ),
  377. ),
  378. SliverToBoxAdapter(
  379. child: Padding(
  380. padding: const EdgeInsets.fromLTRB(12.0, 0.0 , 12.0,12.0),
  381. child: Column(
  382. crossAxisAlignment: CrossAxisAlignment.start,
  383. children: <Widget>[
  384. Text(
  385. "本月达标奖励",
  386. style: Theme.of(context).textTheme.headline1.copyWith(fontSize: 16.0),
  387. ),
  388. const SizedBox(
  389. height: 16.0,
  390. ),
  391. Container(
  392. decoration: circular(),
  393. padding: const EdgeInsets.fromLTRB(12.0, 20.0, 12.0, 12.0),
  394. child: StaggeredGridView.countBuilder(
  395. padding: EdgeInsets.zero,
  396. shrinkWrap: true,
  397. physics: NeverScrollableScrollPhysics(),
  398. crossAxisCount: 3,
  399. itemCount: snapshot.data?.rewards?.length,
  400. itemBuilder: (BuildContext context, int index) {
  401. var item = snapshot.data?.rewards[index];
  402. if (item == null) return Container();
  403. return Center(
  404. child: Column(
  405. children: <Widget>[
  406. Image.asset("lib/assets/img/reward_icon_${item.days}day.png"),
  407. const SizedBox(
  408. height: 5.0,
  409. ),
  410. Text(
  411. "领取${item.score}积分",
  412. style: Theme.of(context).textTheme.subtitle2,
  413. ),
  414. const SizedBox(
  415. height: 12.0,
  416. ),
  417. PrimaryButton(
  418. width: 65.0,
  419. height: 30.0,
  420. content: item.status == "finish" ? "领取" : item.status == "received" ? "已领取" : "未完成",
  421. callback: item.status == "finish"
  422. ? () async {
  423. await request(context, () async {
  424. await inject<RestClient>().sportTargetReward(item.id);
  425. ToastUtil.show("领取成功");
  426. });
  427. }
  428. : null,
  429. )
  430. ],
  431. ),
  432. );
  433. },
  434. crossAxisSpacing: 12.0,
  435. mainAxisSpacing: 12.0,
  436. staggeredTileBuilder: (int index) => StaggeredTile.fit(1),
  437. ),
  438. ),
  439. const SizedBox(
  440. height: 50.0,
  441. ),
  442. ],
  443. ),
  444. ),
  445. )
  446. ],
  447. ),
  448. ),
  449. );
  450. }
  451. Future<List<_DataItem>> createFuture(DateTime time) async {
  452. const List<String> week = ["日", "一", "二", "三", "四", "五", "六"];
  453. List<_DataItem> items = [];
  454. for (var d in week) {
  455. items.add(_DataItem(d, 0));
  456. }
  457. DateTime today = DateTime.now();
  458. int todayday = today.day;
  459. DateTime now = DateTime(time.year, time.month, time.day);
  460. DateTime next = DateTime(time.year, time.month + 1, time.day);
  461. int inDays = next.difference(time).inDays;
  462. // print("11111111111111111 ${now} ${now.weekday} ${now} ${inDays}");
  463. if (now.weekday != 7) {
  464. for (var i = 0; i < now.weekday; i++) {
  465. items.add(_DataItem("", 0));
  466. }
  467. }
  468. for (var i = 0; i < inDays; i++) {
  469. bool lastDay = false;
  470. if (now.month == today.month && i + 1 > todayday) {
  471. lastDay = true;
  472. }
  473. items.add(
  474. _DataItem("${i + 1}", 0,
  475. enable: lastDay,
  476. today: now.month == today.month && todayday == i + 1,
  477. tag: "${now.year}-${now.month.toString().padLeft(2, '0')}-${(i + 1).toString().padLeft(2, '0')}"),
  478. );
  479. }
  480. int end = now.weekday - 1 + inDays;
  481. for (var i = end; i < 49; i++) {
  482. items.add(_DataItem("", 0));
  483. }
  484. var records = await inject<RestClient>().getSportTargetRecord(now.year, now.month);
  485. records?.results?.forEach((element) {
  486. for (var item in items) {
  487. if (item.tag == element.createdAt) {
  488. item.state = element.finish == true ? 2 : 1;
  489. }
  490. }
  491. });
  492. return items;
  493. }
  494. }
  495. class _DataItem {
  496. final String tag;
  497. final String title;
  498. final bool enable;
  499. final bool today;
  500. int state;
  501. _DataItem(this.title, this.state, {this.enable = true, this.tag, this.today});
  502. }