statistics.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. import 'package:flutter/material.dart';
  2. import 'package:provider/provider.dart';
  3. import 'package:sport/pages/run/chart.dart';
  4. import 'package:sport/pages/run/location.dart';
  5. import 'package:sport/pages/run/run_page.dart';
  6. import 'package:sport/provider/user_model.dart';
  7. import 'package:sport/utils/DateFormat.dart';
  8. import 'package:sport/utils/sport_utils.dart';
  9. import 'package:sport/widgets/decoration.dart';
  10. import 'package:sport/widgets/image.dart';
  11. class StatisticsWidget extends StatelessWidget {
  12. double totalDistance = 0;
  13. double totalAltitude = 0;
  14. int totalTime = 0;
  15. int totalStep = 0;
  16. int stepAvg = 0;
  17. int stepMax = 0;
  18. int totalConsume = 0;
  19. int kmTime = 0;
  20. double avg = 0.0;
  21. double avgSpeed = 0.0;
  22. int min = 0;
  23. int max = 0;
  24. DateTime? timeStart;
  25. DateTime? timeEnd;
  26. List<int> stepRateList = [];
  27. List<double> altitudeList = [];
  28. final Map<int, Location> _pointsKm = {};
  29. final Map<int, Duration> _pointsKmTime = {};
  30. @override
  31. Widget build(BuildContext context) {
  32. var maxKm = _pointsKmTime.isNotEmpty ? _pointsKmTime.keys.reduce((value, element) => value > element ? value : element) : 0;
  33. return Column(
  34. children: [
  35. Container(
  36. height: 68.0,
  37. child: Consumer<UserModel>(
  38. builder: (_, model, __) => Stack(
  39. children: [
  40. Positioned(
  41. right: 0,
  42. bottom: 0,
  43. left: 0,
  44. child: Container(
  45. padding: const EdgeInsets.fromLTRB(100.0, 12.0, 20.0, 6.0),
  46. decoration:
  47. BoxDecoration(color: Colors.white, borderRadius: BorderRadius.only(topLeft: Radius.circular(10.0), topRight: Radius.circular(10.0))),
  48. child: Row(
  49. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  50. crossAxisAlignment: CrossAxisAlignment.end,
  51. children: <Widget>[
  52. Text(model.user.name, style: Theme.of(context).textTheme.headline1!),
  53. Text(
  54. "${SportUtils.toDateTime(timeStart)} ${SportUtils.toEndTime(timeStart)} ~ ${SportUtils.toEndTime(timeEnd)}",
  55. style: Theme.of(context).textTheme.bodyText1!,
  56. )
  57. ],
  58. ),
  59. ),
  60. ),
  61. Positioned(
  62. left: 0,
  63. top: 0,
  64. child: Container(
  65. margin: const EdgeInsets.symmetric(horizontal: 16.0),
  66. padding: const EdgeInsets.all(4),
  67. decoration: BoxDecoration(color: Colors.white, shape: BoxShape.circle),
  68. child: CircleAvatar(
  69. backgroundColor: Colors.black26,
  70. backgroundImage: userAvatarProvider(model.user.avatar),
  71. radius: 30.0,
  72. ),
  73. ))
  74. ],
  75. ),
  76. ),
  77. ),
  78. Container(
  79. color: Colors.white,
  80. child: Column(
  81. children: [
  82. Padding(
  83. padding: const EdgeInsets.symmetric(horizontal: 4.0),
  84. child: Column(
  85. children: [
  86. Padding(
  87. padding: const EdgeInsets.all(20.0),
  88. child: Column(
  89. crossAxisAlignment: CrossAxisAlignment.start,
  90. children: [
  91. RichText(
  92. text: TextSpan(children: <InlineSpan>[
  93. TextSpan(
  94. text: "${formatNum(totalDistance / 1000, 2)}",
  95. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 45.0, fontFamily: "DIN"),
  96. ),
  97. WidgetSpan(alignment: PlaceholderAlignment.bottom, child: Text(" 公里", style: Theme.of(context).textTheme.subtitle1!))
  98. ]),
  99. ),
  100. const SizedBox(
  101. height: 20.0,
  102. ),
  103. Row(
  104. children: [
  105. // Container(
  106. // margin: EdgeInsets.only(right: 6.0),
  107. // height: 4,
  108. // width: 10,
  109. // color: COLOR_RUN_FAST,
  110. // ),
  111. Text(
  112. // "${SportUtils.toEndTime(timeStart)}开始",
  113. "快 ${SportUtils.pace(SportUtils.calPace((min), 1))}",
  114. style: Theme.of(context).textTheme.bodyText1!,
  115. ),
  116. Expanded(
  117. child: Container(
  118. margin: EdgeInsets.symmetric(horizontal: 6.0),
  119. height: 4,
  120. decoration: BoxDecoration(
  121. gradient: LinearGradient(
  122. begin: Alignment.centerLeft,
  123. end: Alignment.centerRight,
  124. colors: [
  125. COLOR_RUN_FAST,
  126. COLOR_RUN_MIDDLE,
  127. COLOR_RUN_SLOW,
  128. ],
  129. )))),
  130. Text(
  131. // "${SportUtils.toEndTime(timeEnd)}结束",
  132. "慢 ${SportUtils.pace(SportUtils.calPace((max), 1))}",
  133. style: Theme.of(context).textTheme.bodyText1!,
  134. ),
  135. ],
  136. ),
  137. ],
  138. ),
  139. ),
  140. Padding(
  141. padding: const EdgeInsets.only(left: 20.0),
  142. child: Row(
  143. crossAxisAlignment: CrossAxisAlignment.end,
  144. children: [
  145. Expanded(
  146. flex: 4,
  147. child: Column(
  148. crossAxisAlignment: CrossAxisAlignment.start,
  149. children: [
  150. Text(
  151. "${DateFormat.toTime(totalTime)}",
  152. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0, fontFamily: "DIN"),
  153. ),
  154. Text(
  155. "时长",
  156. style: Theme.of(context).textTheme.bodyText1!,
  157. )
  158. ],
  159. ),
  160. ),
  161. Expanded(
  162. flex: 4,
  163. child: Column(
  164. crossAxisAlignment: CrossAxisAlignment.start,
  165. children: [
  166. Row(
  167. children: [
  168. Text(
  169. "${SportUtils.pace11(SportUtils.calPace(totalTime, totalDistance / 1000))}",
  170. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0, fontFamily: "DIN"),
  171. ),
  172. Text(
  173. "′",
  174. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0),
  175. ),
  176. Text(
  177. "${SportUtils.pace12(SportUtils.calPace(totalTime, totalDistance / 1000))}",
  178. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0, fontFamily: "DIN"),
  179. ),
  180. Text(
  181. "″",
  182. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0),
  183. ),
  184. ],
  185. ),
  186. Text(
  187. "配速(每公里用时)",
  188. style: Theme.of(context).textTheme.bodyText1!,
  189. )
  190. ],
  191. ),
  192. ),
  193. Expanded(
  194. flex: 3,
  195. child: Column(
  196. crossAxisAlignment: CrossAxisAlignment.start,
  197. children: [
  198. Text(
  199. "$totalConsume",
  200. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0, fontFamily: "DIN"),
  201. ),
  202. Text(
  203. "消耗(大卡)",
  204. style: Theme.of(context).textTheme.bodyText1!,
  205. )
  206. ],
  207. ),
  208. ),
  209. ],
  210. ),
  211. ),
  212. const SizedBox(
  213. height: 20.0,
  214. ),
  215. Padding(
  216. padding: const EdgeInsets.only(left: 20.0),
  217. child: Row(
  218. children: [
  219. Expanded(
  220. flex: 4,
  221. child: Column(
  222. crossAxisAlignment: CrossAxisAlignment.start,
  223. children: [
  224. Text(
  225. "${totalStep}",
  226. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0, fontFamily: "DIN"),
  227. ),
  228. Text(
  229. "步数(步)",
  230. style: Theme.of(context).textTheme.bodyText1!,
  231. ),
  232. ],
  233. ),
  234. ),
  235. Expanded(
  236. flex: 4,
  237. child: Column(
  238. crossAxisAlignment: CrossAxisAlignment.start,
  239. children: [
  240. Text(
  241. totalStep == 0 ? "0" : "${totalStep ~/ (totalTime / 60)}",
  242. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0, fontFamily: "DIN"),
  243. ),
  244. Text(
  245. "平均步频(步/分钟)",
  246. style: Theme.of(context).textTheme.bodyText1!,
  247. ),
  248. ],
  249. ),
  250. ),
  251. Expanded(
  252. flex: 3,
  253. child: Column(
  254. crossAxisAlignment: CrossAxisAlignment.start,
  255. children: [
  256. Text(
  257. totalStep == 0 ? "0" : "${(totalDistance / totalStep * 100).toInt()}",
  258. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 22.0, fontFamily: "DIN"),
  259. ),
  260. Text(
  261. "平均步幅(厘米)",
  262. style: Theme.of(context).textTheme.bodyText1!,
  263. ),
  264. ],
  265. ),
  266. ),
  267. ],
  268. ),
  269. ),
  270. ],
  271. ),
  272. ),
  273. if (_pointsKmTime.isNotEmpty == true)
  274. BoxWidget(
  275. title: "配速",
  276. icon: "run_icon_tile1.png",
  277. child: Column(
  278. children: [
  279. Row(
  280. mainAxisSize: MainAxisSize.max,
  281. children: [
  282. Text(
  283. "公里",
  284. style: Theme.of(context).textTheme.bodyText1!,
  285. ),
  286. Expanded(
  287. child: Center(
  288. child: Text(
  289. "配速",
  290. style: Theme.of(context).textTheme.bodyText1!,
  291. ),
  292. ),
  293. ),
  294. ],
  295. ),
  296. const SizedBox(
  297. height: 12.0,
  298. ),
  299. Column(
  300. children: _pointsKmTime.isNotEmpty
  301. ? _pointsKmTime.keys
  302. .map(
  303. (e) => Padding(
  304. padding: const EdgeInsets.symmetric(vertical: 1.0),
  305. child: Row(
  306. mainAxisSize: MainAxisSize.max,
  307. children: [
  308. Text(
  309. "${e.toString().padLeft(2, "0")}",
  310. style: Theme.of(context).textTheme.subtitle1!.copyWith(fontFamily: "DIN"),
  311. ),
  312. Expanded(
  313. child: Container(
  314. height: 17.0,
  315. margin: const EdgeInsets.symmetric(horizontal: 10.0),
  316. child: LinearProgressIndicator(
  317. value: avg == 0 ? 0 : (_pointsKmTime[e]?.inSeconds ?? 0) / avg,
  318. // value: min == 0 ? 0 : (_pointsKmTime[e]?.inSeconds ?? 0) / min / 2,
  319. backgroundColor: const Color(0xffF1F1F1),
  320. valueColor: AlwaysStoppedAnimation<Color>(
  321. (_pointsKmTime[e]?.inSeconds ?? 0) == min ? const Color(0xffFF5B1D) : Theme.of(context).accentColor),
  322. ),
  323. ),
  324. ),
  325. ConstrainedBox(
  326. constraints: BoxConstraints(minWidth: 50),
  327. child: Text(
  328. "${SportUtils.pace(SportUtils.calPace((_pointsKmTime[e]?.inSeconds ?? 0), 1))}",
  329. style: Theme.of(context).textTheme.subtitle1!,
  330. ),
  331. ),
  332. ],
  333. ),
  334. ),
  335. )
  336. .toList()
  337. : [],
  338. ),
  339. if (_pointsKmTime.length > 0)
  340. const SizedBox(
  341. height: 12.0,
  342. ),
  343. if (_pointsKmTime.length > 0 && totalTime > 0)
  344. Row(
  345. mainAxisAlignment: MainAxisAlignment.spaceAround,
  346. children: [
  347. RichText(
  348. text: TextSpan(children: [
  349. TextSpan(text: "$maxKm公里累计用时 ", style: Theme.of(context).textTheme.bodyText1!),
  350. TextSpan(
  351. text: "${DateFormat.toTime(kmTime)}",
  352. style: Theme.of(context).textTheme.bodyText1!.copyWith(color: Theme.of(context).accentColor))
  353. ]),
  354. ),
  355. if (totalTime - kmTime > 0)
  356. RichText(
  357. text: TextSpan(children: [
  358. TextSpan(text: "最后不足1公里用时 ", style: Theme.of(context).textTheme.bodyText1!),
  359. TextSpan(
  360. text: "${DateFormat.toTime((totalTime) - kmTime)}",
  361. style: Theme.of(context).textTheme.bodyText1!.copyWith(color: Theme.of(context).accentColor))
  362. ]),
  363. ),
  364. ],
  365. )
  366. ],
  367. )),
  368. BoxWidget(
  369. title: "步频",
  370. icon: "run_icon_tile2.png",
  371. child: Column(
  372. children: [
  373. Row(
  374. children: [
  375. Column(
  376. crossAxisAlignment: CrossAxisAlignment.start,
  377. children: [
  378. Text(
  379. "$stepAvg",
  380. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 25.0, fontFamily: "DIN"),
  381. ),
  382. Text(
  383. "平均步频(步/分钟)",
  384. style: Theme.of(context).textTheme.bodyText1!,
  385. )
  386. ],
  387. ),
  388. const SizedBox(
  389. width: 37.0,
  390. ),
  391. Column(
  392. crossAxisAlignment: CrossAxisAlignment.start,
  393. children: [
  394. Text(
  395. "$stepMax",
  396. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 25.0, fontFamily: "DIN"),
  397. ),
  398. Text(
  399. "最高步频(步/分钟)",
  400. style: Theme.of(context).textTheme.bodyText1!,
  401. )
  402. ],
  403. ),
  404. ],
  405. ),
  406. RunChart(
  407. values: stepRateList.map((e) => e.toDouble()).toList(),
  408. gradient: false,
  409. ),
  410. ],
  411. )),
  412. BoxWidget(
  413. title: "运动海拔",
  414. icon: "run_icon_tile3.png",
  415. child: Column(
  416. children: [
  417. Row(
  418. children: [
  419. Column(
  420. crossAxisAlignment: CrossAxisAlignment.start,
  421. children: [
  422. Text(
  423. "${(totalAltitude).toStringAsFixed(1)}",
  424. style: Theme.of(context).textTheme.headline1!.copyWith(fontSize: 25.0, fontFamily: "DIN"),
  425. ),
  426. Text(
  427. "累计爬升(米)",
  428. style: Theme.of(context).textTheme.bodyText1!,
  429. )
  430. ],
  431. ),
  432. ],
  433. ),
  434. RunChart(
  435. values: altitudeList.map((e) => e.toDouble()).toList(),
  436. gradient: true,
  437. ),
  438. ],
  439. )),
  440. ],
  441. ),
  442. )
  443. ],
  444. );
  445. }
  446. }
  447. class BoxWidget extends StatelessWidget {
  448. final String title;
  449. final String icon;
  450. final Widget child;
  451. const BoxWidget({Key? key, required this.title, required this.child, required this.icon}) : super(key: key);
  452. @override
  453. Widget build(BuildContext context) {
  454. return Container(
  455. margin: const EdgeInsets.fromLTRB(12.0, 0, 12.0, 12.0),
  456. // transform: Matrix4.translationValues(0, -10, 0),
  457. decoration: card(),
  458. child: Column(
  459. children: [
  460. const SizedBox(
  461. height: 25,
  462. ),
  463. // Divider(
  464. // height: 40.0,
  465. // ),
  466. Padding(
  467. padding: const EdgeInsets.symmetric(horizontal: 24.0),
  468. child: Column(
  469. crossAxisAlignment: CrossAxisAlignment.start,
  470. children: [
  471. Row(
  472. children: [
  473. Image.asset("lib/assets/img/$icon"),
  474. const SizedBox(
  475. width: 7.0,
  476. ),
  477. Text(
  478. "$title",
  479. style: Theme.of(context).textTheme.headline3!,
  480. ),
  481. ],
  482. ),
  483. const SizedBox(
  484. height: 16.0,
  485. ),
  486. child,
  487. ],
  488. ),
  489. ),
  490. ],
  491. ),
  492. );
  493. }
  494. }