sport_index.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import 'dart:math';
  2. import 'dart:ui';
  3. import 'package:sport/application.dart';
  4. import 'package:sport/bean/sport_detail.dart';
  5. import 'package:sport/bean/sport_target.dart';
  6. import 'package:sport/bean/user.dart';
  7. //import 'package:sport/bean/user.dart';
  8. import 'game.dart' as game;
  9. class SportIndex {
  10. int? duration;
  11. int? durationTarget;
  12. int? consume;
  13. int? beyond;
  14. String? inspire;
  15. game.GameInfoData? lastGame;
  16. List<game.GameInfoData>? games;
  17. RankInfo? rank;
  18. List<Achievement>? achievement;
  19. Today? today;
  20. SportTarget? target;
  21. List<RecordsToday>? records;
  22. SportIndex({this.duration, this.durationTarget, this.consume, this.beyond, this.inspire, this.lastGame, this.achievement});
  23. SportIndex.fromJson(Map<String, dynamic> json) {
  24. duration = json['duration'];
  25. durationTarget = json['duration_target'];
  26. consume = json['consume'];
  27. beyond = json['beyond'];
  28. inspire = json['inspire'];
  29. lastGame = json['last_game'] != null ? new game.GameInfoData.fromJson(json['last_game']) : null;
  30. rank = json['rank'] != null ? new RankInfo.fromJson(json['rank']) : null;
  31. if (json['games'] != null) {
  32. games = [];
  33. json['games'].forEach((v) {
  34. games!.add(new game.GameInfoData.fromJson(v));
  35. });
  36. }
  37. if (json['new_achievements'] != null) {
  38. achievement = [];
  39. json['new_achievements'].forEach((v) {
  40. achievement!.add(new Achievement.fromJson(v));
  41. });
  42. }
  43. today = json['today'] != null ? new Today.fromJson(json['today']) : null;
  44. target = json['target'] != null ? new SportTarget.fromJson(json['target']) : null;
  45. if (json['records'] != null) {
  46. records = [];
  47. json['records'].forEach((v) {
  48. records!.add(new RecordsToday.fromJson(v));
  49. });
  50. }
  51. }
  52. Map<String, dynamic> toJson() {
  53. final Map<String, dynamic> data = new Map<String, dynamic>();
  54. data['duration'] = this.duration;
  55. data['duration_target'] = this.durationTarget;
  56. data['consume'] = this.consume;
  57. data['beyond'] = this.beyond;
  58. data['inspire'] = this.inspire;
  59. if (this.lastGame != null) {
  60. data['last_game'] = this.lastGame!.toJson();
  61. }
  62. data["new_achievements"] = this.achievement;
  63. return data;
  64. }
  65. }
  66. class RankInfo {
  67. Rank? rank;
  68. User? user;
  69. List<User>? records;
  70. RankInfo({this.rank, this.user, this.records});
  71. RankInfo.fromJson(Map<String, dynamic> json) {
  72. rank = json['rank'] != null
  73. ? json['rank'] is bool
  74. ? null
  75. : new Rank.fromJson(json['rank'])
  76. : null;
  77. user = json['user'] != null ? new User.fromJson(json['user']) : null;
  78. if (json['records'] != null) {
  79. records = [];
  80. json['records'].forEach((v) {
  81. records!.add(new User.fromJson(v));
  82. });
  83. }
  84. }
  85. Map<String, dynamic> toJson() {
  86. final Map<String, dynamic> data = new Map<String, dynamic>();
  87. if (this.rank != null) {
  88. data['rank'] = this.rank!.toJson();
  89. }
  90. if (this.user != null) {
  91. data['user'] = this.user!.toJson();
  92. }
  93. if (this.records != null) {
  94. data['records'] = this.records!.map((v) => v.toJson()).toList();
  95. }
  96. return data;
  97. }
  98. }
  99. class Rank {
  100. int? id;
  101. String? name;
  102. int? isGame;
  103. int? isSport;
  104. int? gameId;
  105. String? introduce;
  106. int? userCountMax;
  107. String? rateBegin;
  108. String? rateEnd;
  109. Rank({this.id, this.name, this.isGame, this.isSport, this.gameId, this.introduce, this.userCountMax, this.rateBegin, this.rateEnd});
  110. Rank.fromJson(Map<String, dynamic> json) {
  111. id = json['id'];
  112. name = json['name'];
  113. isGame = json['is_game'];
  114. isSport = json['is_sport'];
  115. gameId = json['game_id'];
  116. introduce = json['introduce'];
  117. userCountMax = json['user_count_max'];
  118. rateBegin = json['rate_begin'];
  119. rateEnd = json['rate_end'];
  120. }
  121. Map<String, dynamic> toJson() {
  122. final Map<String, dynamic> data = new Map<String, dynamic>();
  123. data['id'] = this.id;
  124. data['name'] = this.name;
  125. data['is_game'] = this.isGame;
  126. data['is_sport'] = this.isSport;
  127. data['game_id'] = this.gameId;
  128. data['introduce'] = this.introduce;
  129. data['user_count_max'] = this.userCountMax;
  130. data['rate_begin'] = this.rateBegin;
  131. data['rate_end'] = this.rateEnd;
  132. return data;
  133. }
  134. }
  135. class Today {
  136. int? step, consume, duration;
  137. Today({this.step, this.consume, this.duration});
  138. Today.fromJson(Map<String, dynamic> json) {
  139. step = json['step'];
  140. consume = json['consume'];
  141. duration = json['duration_minute'];
  142. }
  143. double get strength => strengthToValue(consume, duration);
  144. String strengthLabel() {
  145. return strengthToLabel(consume, duration);
  146. }
  147. int? value(String? type) => type == "consume" ? consume : duration;
  148. }
  149. const strengthArr = ["低强度", "中等", "高强度", "剧烈", "高负荷"];
  150. const strengthDetails = ["该级别适用于恢复和基础心血功能训练,可以提高心脏的泵血能力和肌肉使用氧气的能力", "可以适当增强心肺功能,获得更强耐力", "可以增强力量和肌肉耐力,提高厌氧能力和乳酸阈值", "有利于增强机体内肌肉的含量,加快代谢,使肌肉骨骼更加强壮", "可以增加个人机体的极限,对人体素质有很大提高;此时强度过大,心肺对全身的肌肉、细胞供氧不足,可能会出现无力、虚脱等症状"];
  151. const strengthColors = [const Color(0xffFFBFA8), const Color(0xffFF9D77), const Color(0xffFF5B1D), const Color(0xffD93D01), const Color(0xff9C2B00)];
  152. double strengthToValue(int? consume, int? duration) {
  153. if (duration == null || consume == null) return 0.0;
  154. if (duration == 0 || consume == 0) return 0.0;
  155. double result = consume / duration * 60.0;
  156. return result;
  157. }
  158. String strengthToLabel(int? consume, int? duration) {
  159. double v = metNum(consume, (duration ?? 0) ~/ 60);
  160. return strengthMetToLabel(v);
  161. }
  162. String metToLabel(double met) {
  163. return strengthMetToLabel(met);
  164. }
  165. String metToDetail(double met) {
  166. var label = metToLabel(met);
  167. return strengthDetails[strengthArr.indexOf(label)];
  168. }
  169. Color metToColor(double met) {
  170. var label = metToLabel(met);
  171. return strengthColors[strengthArr.indexOf(label)];
  172. }
  173. String strengthMetToLabel(double met) {
  174. double v = met;
  175. if (v == 0) return strengthArr[0];
  176. if (gender == 1) {
  177. if (v < 4.0) {
  178. return strengthArr[0];
  179. } else if (v < 6.0) {
  180. return strengthArr[1];
  181. } else if (v < 8.0) {
  182. return strengthArr[2];
  183. } else if (v < 10.0) {
  184. return strengthArr[3];
  185. } else {
  186. return strengthArr[4];
  187. }
  188. } else {
  189. if (v < 2.8) {
  190. return strengthArr[0];
  191. } else if (v < 4.4) {
  192. return strengthArr[1];
  193. } else if (v < 6.0) {
  194. return strengthArr[2];
  195. } else if (v < 7.6) {
  196. return strengthArr[3];
  197. } else {
  198. return strengthArr[4];
  199. }
  200. }
  201. }
  202. String met(int? consume, int? duration) {
  203. return metNum(consume, duration).toStringAsFixed(1);
  204. }
  205. double metNum(int? consume, int? duration) {
  206. // print("met $consume $duration");
  207. if (duration == null || consume == null) return 0;
  208. if (duration == 0 || consume == 0) return 0;
  209. // 卡路里消耗(大卡)=MET值× 0.0167 ×时间h(min)×体重(kg)
  210. return min(30.0, (consume / (0.0167 * duration * weight)));
  211. }