misc.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import 'dart:math';
  2. import 'package:cached_network_image/cached_network_image.dart';
  3. import 'package:dartin/dartin.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_easyrefresh/easy_refresh.dart';
  6. import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
  7. import 'package:provider/provider.dart';
  8. import 'package:sport/bean/post_user.dart';
  9. import 'package:sport/bean/user.dart';
  10. import 'package:sport/constant/ui.dart';
  11. import 'package:sport/pages/my/achievement_detail_page.dart';
  12. import 'package:sport/provider/user_model.dart';
  13. import 'package:sport/router/navigator_util.dart';
  14. import 'package:sport/services/api/inject_api.dart';
  15. import 'package:sport/services/api/login_api.dart';
  16. import 'package:sport/widgets/dialog/popupmenu.dart' as menu;
  17. import 'package:sport/widgets/image.dart';
  18. import 'package:sport/widgets/refresh_header.dart' as header;
  19. import 'package:sport/widgets/refresh_footer.dart';
  20. import 'package:sport/widgets/space.dart';
  21. Widget buildSocialUserWidget(BuildContext context, PostUser user, int createTime, int avatarWidth) {
  22. return user == null
  23. ? Container()
  24. : GestureDetector(
  25. onTap: () => NavigatorUtil.goSocialUserDetail(context, user),
  26. child: Row(
  27. children: <Widget>[
  28. Space(
  29. width: 12,
  30. ),
  31. CircleAvatar(backgroundImage: userAvatarProvider(user?.avatar), radius: avatarWidth / 2),
  32. Space(
  33. width: 8,
  34. ),
  35. Text(
  36. user?.name,
  37. style: Theme.of(context).textTheme.subtitle1.copyWith(fontWeight: FontWeight.w600),
  38. ),
  39. ],
  40. ));
  41. }
  42. Widget buildLabelWidget(BuildContext context, String title) {
  43. return Container(
  44. padding: EdgeInsets.fromLTRB(ui_padding, 10.0, ui_padding, 10.0),
  45. child: Text(
  46. title,
  47. style: Theme.of(context).textTheme.headline1,
  48. ));
  49. }
  50. Widget gameTag(BuildContext context, String name) {
  51. return Container(
  52. decoration: BoxDecoration(
  53. borderRadius: BorderRadius.all(Radius.circular(50)),
  54. border: Border.all(
  55. color: Theme.of(context).accentColor,
  56. width: 1,
  57. ),
  58. ),
  59. padding: EdgeInsets.fromLTRB(8, 0, 8, 1),
  60. child: Text(
  61. name,
  62. style: Theme.of(context).textTheme.bodyText1.copyWith(color: Theme.of(context).accentColor),
  63. strutStyle: fixedLine,
  64. ),
  65. );
  66. }
  67. Widget achievementGroupWidget(List<Achievement> achievements) {
  68. return achievements == null || achievements.isEmpty
  69. ? Center(child: Container(child: Padding(padding: const EdgeInsets.all(24.0), child: Text("还未获得成就"))))
  70. : StaggeredGridView.countBuilder(
  71. padding: EdgeInsets.zero,
  72. shrinkWrap: true,
  73. physics: NeverScrollableScrollPhysics(),
  74. crossAxisCount: 4,
  75. itemCount: min(4, achievements.length),
  76. itemBuilder: (BuildContext context, int index) => achievementWidget(context, achievements[index]),
  77. crossAxisSpacing: 12.0,
  78. staggeredTileBuilder: (int index) => StaggeredTile.fit(1),
  79. );
  80. }
  81. Widget achievementWidget(BuildContext context, Achievement item, {double w = 70, bool replace = false, bool isRadius = false, bool jump = true,showCount=false}) => InkWell(
  82. onTap: () async {
  83. // 不能看别人的成就...
  84. if(item.userId != null && Provider.of<UserModel>(context, listen: false).user.id != item.userId)
  85. return;
  86. // if (jump == true) NavigatorUtil.goAchievementDetails(context, id: item.id, userId: item.userId ?? 0, replace: replace);
  87. if(jump == true) {
  88. List<Achievement> relateAchievements = (await inject<LoginApi>().getAchieveDetailInfo(item.id)).data.relateAchievements;
  89. showSharePopup(context,relateAchievements,item.id);
  90. }
  91. },
  92. child: Stack(
  93. children: <Widget>[
  94. Column(
  95. children: <Widget>[
  96. isRadius
  97. ? CircleAvatar(backgroundImage: CachedNetworkImageProvider(item.logo ?? ""), radius: w / 2)
  98. : item?.createdAt == ""
  99. ? ColorFiltered(
  100. colorFilter: ColorFilter.mode(Color(0xffF1F1F1), BlendMode.color),
  101. child: CachedNetworkImage(width: w, height: w, imageUrl: item.logo ?? ""),
  102. )
  103. : Container(
  104. width: w,
  105. height: w,
  106. child: CachedNetworkImage(imageUrl: item.logo ?? ""),
  107. ),
  108. Space(
  109. height: 5,
  110. ),
  111. Text(
  112. item.seriesName != null ? item.seriesName : item.name,
  113. style: Theme.of(context).textTheme.subtitle1.copyWith(fontSize: w < 80 ? 12 : 14),
  114. )
  115. ],
  116. ),
  117. showCount ? Positioned(
  118. top: 0,
  119. right: 10.0,
  120. child: Container(
  121. alignment: Alignment.center,
  122. width: 32.0,height: 21.0,
  123. decoration: BoxDecoration(
  124. image: DecorationImage(
  125. image: AssetImage("lib/assets/img/bg_achievement_number.png"),
  126. fit: BoxFit.cover,
  127. )
  128. ),
  129. child: Text("${item.seriesCount}枚",style:TextStyle(fontSize: 12.0,color:Colors.white),strutStyle: StrutStyle(forceStrutHeight: true, height: 0.8),),
  130. )
  131. ):Container()
  132. ],
  133. ));
  134. Widget sportBeEquivalentTo(BuildContext context, int consume, {bool highlight = false}) => Padding(
  135. padding: EdgeInsets.all(10.0),
  136. child: Text(
  137. "消耗了 ${(consume / 50).round()} 块小饼干",
  138. style: Theme.of(context).textTheme.subtitle1,
  139. ),
  140. );
  141. // Row(
  142. // mainAxisAlignment: MainAxisAlignment.spaceAround,
  143. // children: <Widget>[
  144. // Column(
  145. // children: <Widget>[
  146. // Image.asset("lib/assets/img/gamedetail_image_walk.png"),
  147. // Space(
  148. // height: 8,
  149. // ),
  150. // RichText(
  151. // text: TextSpan(children: <InlineSpan>[
  152. // TextSpan(text: '步行', style: Theme.of(context).textTheme.bodyText1),
  153. // TextSpan(
  154. // text: '${(consume / 3 * 90).floor()}',
  155. // style: highlight
  156. // ? Theme.of(context).textTheme.bodyText1.copyWith(color: Theme.of(context).accentColor)
  157. // : Theme.of(context).textTheme.bodyText1),
  158. // TextSpan(text: '步', style: Theme.of(context).textTheme.bodyText1),
  159. // ]),
  160. // )
  161. // ],
  162. // ),
  163. // Column(
  164. // children: <Widget>[
  165. // Image.asset("lib/assets/img/gamedetail_image_run.png"),
  166. // Space(
  167. // height: 8,
  168. // ),
  169. // RichText(
  170. // text: TextSpan(children: <InlineSpan>[
  171. // TextSpan(text: '跑步', style: Theme.of(context).textTheme.bodyText1),
  172. // TextSpan(
  173. // text: '${(consume / 60 / 1.036).toStringAsFixed(1)}',
  174. // style: highlight
  175. // ? Theme.of(context).textTheme.bodyText1.copyWith(color: Theme.of(context).accentColor)
  176. // : Theme.of(context).textTheme.bodyText1),
  177. // TextSpan(text: '公里', style: Theme.of(context).textTheme.bodyText1),
  178. // ]),
  179. // )
  180. // ],
  181. // ),
  182. // Column(
  183. // children: <Widget>[
  184. // Image.asset("lib/assets/img/gamedetail_image_riding.png"),
  185. // Space(
  186. // height: 8,
  187. // ),
  188. // RichText(
  189. // text: TextSpan(children: <InlineSpan>[
  190. // TextSpan(text: '单车', style: Theme.of(context).textTheme.bodyText1),
  191. // TextSpan(
  192. // text: '${(consume / 60 / 0.6142).toStringAsFixed(1)}',
  193. // style: highlight
  194. // ? Theme.of(context).textTheme.bodyText1.copyWith(color: Theme.of(context).accentColor)
  195. // : Theme.of(context).textTheme.bodyText1),
  196. // TextSpan(text: '公里', style: Theme.of(context).textTheme.bodyText1),
  197. // ]),
  198. // )
  199. // ],
  200. // ),
  201. // ],
  202. // );
  203. const REFRESH_INFO_COLOR = Color(0xff999999);
  204. header.ClassicalHeader buildClassicalHeader(
  205. {double extent = 80.0,
  206. double triggerDistance = 90.0,
  207. Color infoColor = REFRESH_INFO_COLOR,
  208. Color textColor = REFRESH_INFO_COLOR,
  209. Color bgColor = Colors.transparent}) {
  210. return header.ClassicalHeader(
  211. extent: extent,
  212. triggerDistance: triggerDistance,
  213. showInfo: false,
  214. refreshText: '下拉刷新',
  215. refreshFailedText: '刷新失败',
  216. refreshedText: '刷新完成',
  217. refreshingText: '正在刷新...',
  218. refreshReadyText: '释放刷新',
  219. infoColor: infoColor,
  220. bgColor: bgColor,
  221. textColor: infoColor,
  222. );
  223. }
  224. Footer buildClassicalFooter() {
  225. return CustomClassicalFooter(
  226. showInfo: false,
  227. loadedText: '加载完成',
  228. loadReadyText: '释放加载',
  229. loadFailedText: '加载失败',
  230. loadText: '拉动加载',
  231. loadingText: '正在加载...',
  232. noMoreText: "没有更多了~",
  233. infoColor: Color(0xff999999));
  234. }
  235. PopupMenuEntry menuDivider() => menu.PopupMenuItem(
  236. height: 1,
  237. child: Divider(
  238. height: 1,
  239. ),
  240. );
  241. PopupMenuEntry menuItem(String value, String icon, String text) => menu.PopupMenuItem(
  242. value: value,
  243. child: Row(
  244. mainAxisSize: MainAxisSize.min,
  245. children: <Widget>[
  246. Image.asset(
  247. "lib/assets/img/$icon",
  248. width: 24,
  249. ),
  250. SizedBox(
  251. width: 4,
  252. ),
  253. Text(
  254. text,
  255. )
  256. ],
  257. ),
  258. );
  259. PopupMenuEntry menuItemCenter(dynamic value, String text) => menu.PopupMenuItem(
  260. value: value,
  261. child: Center(
  262. child: Text(
  263. text,
  264. )),
  265. );
  266. PopupMenuEntry menuItemSelected(dynamic value, String text, bool select) => menu.PopupMenuItem(
  267. value: value,
  268. child: Row(
  269. mainAxisSize: MainAxisSize.min,
  270. children: <Widget>[
  271. Container(
  272. alignment: AlignmentDirectional.centerStart,
  273. constraints: BoxConstraints(minWidth: 60),
  274. padding: const EdgeInsets.symmetric(horizontal: 8.0),
  275. child: select
  276. ? Text(
  277. text,
  278. style: TextStyle(color: Color(0xffFFC400)),
  279. )
  280. : Text(
  281. text,
  282. ),
  283. ),
  284. if (select)
  285. Image.asset(
  286. "lib/assets/img/pop_icon_selected.png",
  287. width: 24,
  288. ),
  289. ],
  290. ));
  291. List<PopupMenuEntry> divideMenus(Iterable<PopupMenuEntry> tiles) {
  292. assert(tiles != null);
  293. final Iterator<PopupMenuEntry> iterator = tiles.iterator;
  294. final PopupMenuEntry divider = menuDivider();
  295. final List<PopupMenuEntry> list = [];
  296. while (iterator.moveNext()) {
  297. list.add(iterator.current);
  298. list.add(divider);
  299. }
  300. if (list.length > 1) list.removeAt(list.length - 1);
  301. return list;
  302. }
  303. const StrutStyle fixedLine = StrutStyle(forceStrutHeight: true);