rank_detail.dart 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. import 'package:sport/bean/achievement_info.dart';
  8. import 'package:sport/bean/post_user.dart';
  9. import 'package:sport/bean/rank_game_info.dart';
  10. import 'package:sport/bean/user.dart';
  11. import 'package:sport/pages/game/rank_introduce.dart';
  12. import 'package:sport/router/navigator_util.dart';
  13. import 'package:sport/services/api/inject_api.dart';
  14. import 'package:sport/services/api/resp.dart';
  15. import 'package:sport/services/userid.dart';
  16. import 'package:sport/widgets/image.dart';
  17. import 'package:sport/widgets/loading.dart';
  18. import 'package:sport/widgets/persistent_header.dart';
  19. import 'package:sport/widgets/user_widget.dart';
  20. import 'package:umeng_common_sdk/umeng_common_sdk.dart';
  21. class RankDetailPage extends StatefulWidget {
  22. final int id;
  23. final int type;
  24. RankDetailPage(this.id, this.type);
  25. @override
  26. State<StatefulWidget> createState() {
  27. return _RankDetailPageState();
  28. }
  29. }
  30. class _RankDetailPageState extends State<RankDetailPage> with TickerProviderStateMixin, InjectApi, UserId {
  31. bool _isLoading = true;
  32. Rank? rank;
  33. User? user;
  34. List<User> records = [];
  35. final ScrollController _controller = new ScrollController();
  36. final ItemScrollController itemScrollController = ItemScrollController();
  37. final ItemPositionsListener itemPositionsListener = ItemPositionsListener.create();
  38. final List<String> _tabs = [
  39. '世界榜',
  40. '好友榜',
  41. ];
  42. final List<String> rankImages = [
  43. "",
  44. "rank_icon_first.png",
  45. "rank_icon_second.png",
  46. "rank_icon_third.png",
  47. ];
  48. late TabController _tabController;
  49. @override
  50. void initState() {
  51. super.initState();
  52. _tabController = TabController(length: _tabs.length, initialIndex: 0, vsync: this);
  53. initData(0);
  54. UmengCommonSdk.onEvent("rank_detail", {});
  55. }
  56. @override
  57. void dispose() {
  58. super.dispose();
  59. _tabController.dispose();
  60. _controller.dispose();
  61. }
  62. // 缓存数据...
  63. initData(int index) async {
  64. setState(() {
  65. records = [];
  66. _isLoading = true;
  67. });
  68. String id = "${widget.id}";
  69. String scope = index == 1 ? "friend" : "world";
  70. SharedPreferences prefs = await SharedPreferences.getInstance();
  71. RespData<RankGameInfoData> data;
  72. if (widget.type == 1) {
  73. data = await api.getRankGameInfo(id, scope: scope);
  74. } else {
  75. data = await api.getRankSportInfo(id, scope: scope);
  76. }
  77. RankGameInfoData? infoData = data.data;
  78. if (infoData == null) return;
  79. setState(() {
  80. rank = data.data?.rank;
  81. user = data.data?.user;
  82. var list = data.data?.records ?? [];
  83. if (user == null) {
  84. list.add(new User(
  85. userName: prefs.getString("name"),
  86. userAvatar: prefs.getString("avatar"),
  87. userLevel: prefs.getInt("level_infolevel"),
  88. userLevelLogo: prefs.getString("level_infologo"),
  89. ));
  90. }
  91. records = list;
  92. _isLoading = false;
  93. });
  94. }
  95. Widget _buildRankItemWidget(int number, User data) {
  96. bool isSelf = data.userId == int.parse(selfId) || data.userId == null;
  97. return Column(
  98. children: <Widget>[
  99. InkWell(
  100. onTap: () {
  101. if (data.id != 0 && data.duration != null) NavigatorUtil.goSocialUserDetail(context, PostUser(id: "${data.userId}", name: data.userName, avatar: data.userAvatar));
  102. },
  103. child: Container(
  104. decoration: isSelf
  105. ? BoxDecoration(
  106. image: DecorationImage(image: AssetImage("lib/assets/img/rank_bg_my.png"), fit: BoxFit.cover),
  107. )
  108. : null,
  109. padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 6.0),
  110. child: Row(
  111. children: <Widget>[
  112. _buildTitle(number, data.id != null),
  113. Padding(
  114. padding: const EdgeInsets.fromLTRB(0, 6, 10.0, 6),
  115. child: CircleAvatar(backgroundColor: Colors.black26, backgroundImage: userAvatarProvider(data.userAvatar == null ? "" : data.userAvatar), radius: 22),
  116. ),
  117. Expanded(
  118. child: Row(
  119. mainAxisSize: MainAxisSize.min,
  120. children: [
  121. Text(
  122. data.userName ?? "",
  123. style: Theme.of(context).textTheme.subtitle1!,
  124. maxLines: 1,
  125. ),
  126. UserLevelWidget(
  127. level: Level(level: data.userLevel, logo: data.userLevelLogo),
  128. )
  129. ],
  130. ),
  131. ),
  132. _dataText(rank, data),
  133. ],
  134. ),
  135. ),
  136. ),
  137. Divider(
  138. height: 1,
  139. indent: 12,
  140. endIndent: 12,
  141. ),
  142. ],
  143. );
  144. }
  145. Widget _dataText(Rank? rank, User user) {
  146. var unit = _dataUnit(rank);
  147. if (rank != null) {
  148. if (rank.field == null || rank.field == "") {
  149. return Text(user.score == null || user.score == 0 ? "暂无成绩" : "${user.score?.toStringAsFixed(1)}$unit", style: TextStyle(color: Color.fromRGBO(255, 196, 0, 1)));
  150. } else {
  151. if (rank.field == 'consume') {
  152. return Text(user.consume == null ? "" : "${user.consume?.round()}$unit", style: TextStyle(color: Color.fromRGBO(255, 196, 0, 1)));
  153. } else if (rank.field == 'duration') {
  154. return Text(user.duration == null ? "" : "${(user.duration ?? 0) ~/ 60}$unit", style: TextStyle(color: Color.fromRGBO(255, 196, 0, 1)));
  155. }
  156. }
  157. }
  158. return Container();
  159. }
  160. String _dataUnit(Rank? rank) {
  161. if (rank != null) {
  162. if (rank.field == null || rank.field == "") {
  163. return "分";
  164. } else {
  165. if (rank.field == 'consume') {
  166. return "大卡";
  167. } else if (rank.field == 'duration') {
  168. return "分钟";
  169. }
  170. }
  171. }
  172. return "";
  173. }
  174. Widget _buildTitle(int number, bool isInRank) {
  175. // 直接先未上榜的...
  176. if (isInRank == false)
  177. return Container(
  178. width: 50,
  179. child: Center(
  180. child: Text(
  181. "未上榜",
  182. style: Theme.of(context).textTheme.bodyText2!.copyWith(fontSize: 11.0),
  183. ),
  184. ),
  185. );
  186. // 这里是 看是 前三 还是后七
  187. if (number <= rankImages.length - 1)
  188. return Container(
  189. width: 50.0,
  190. child: Center(
  191. child: Image.asset(
  192. "lib/assets/img/${rankImages[number]}",
  193. width: 25.0,
  194. )));
  195. else {
  196. return Container(
  197. width: 50,
  198. child: Center(
  199. child: Text(
  200. "$number",
  201. style: Theme.of(context).textTheme.bodyText2!,
  202. ),
  203. ),
  204. );
  205. }
  206. }
  207. @override
  208. Widget build(BuildContext context) {
  209. double height = widget.type != 1 ? MediaQuery.of(context).size.width * 611 / 1125 : MediaQuery.of(context).size.width * 500 / 750;
  210. return AnnotatedRegion<SystemUiOverlayStyle>(
  211. value: SystemUiOverlayStyle.light,
  212. child: Scaffold(
  213. backgroundColor: Colors.white,
  214. body: Stack(
  215. fit: StackFit.expand,
  216. children: <Widget>[
  217. Align(
  218. alignment: Alignment.topCenter,
  219. child: Container(
  220. width: MediaQuery.of(context).size.width,
  221. height: height,
  222. decoration: BoxDecoration(
  223. color: Colors.grey,
  224. image: DecorationImage(
  225. image: rank?.isGame == 1
  226. ? AssetImage("lib/assets/img/rank_bg.png")
  227. : rank == null
  228. ? widget.type == 1
  229. ? AssetImage("lib/assets/img/rank_bg.png")
  230. : widget.id == 1
  231. ? AssetImage("lib/assets/img/rank_bg_consume.png")
  232. : AssetImage("lib/assets/img/rank_bg_time.png")
  233. : rank?.id == 1
  234. ? AssetImage("lib/assets/img/rank_bg_consume.png")
  235. : AssetImage("lib/assets/img/rank_bg_time.png"),
  236. fit: BoxFit.cover)),
  237. child: rank != null
  238. ? (widget.type != 0
  239. ? Center(
  240. child: Column(
  241. mainAxisSize: MainAxisSize.min,
  242. children: <Widget>[
  243. ClipRRect(borderRadius: BorderRadius.circular(8), child: CachedNetworkImage(imageUrl: rank!.logo ?? "", width: 80, height: 80)),
  244. Padding(
  245. padding: EdgeInsets.only(top: 10.0),
  246. child: Text(
  247. "${rank!.name}",
  248. style: Theme.of(context).textTheme.headline4,
  249. ),
  250. ),
  251. GestureDetector(
  252. onTap: () {
  253. itemScrollController.scrollTo(index: user?.position != null ? (user?.position ?? 0) - 1 : records.length - 1, duration: Duration(milliseconds: 500));
  254. },
  255. child: Container(
  256. margin: EdgeInsets.fromLTRB(0.0, 12.0, 0, 0),
  257. child: Image.asset(
  258. "lib/assets/img/rank_btn_myrank.png",
  259. width: 103.0,
  260. height: 28.0,
  261. ))),
  262. ],
  263. ),
  264. )
  265. : SafeArea(
  266. child: Padding(
  267. padding: EdgeInsets.fromLTRB(24.0, 36.0, 0, 0),
  268. child: Column(
  269. crossAxisAlignment: CrossAxisAlignment.start,
  270. mainAxisSize: MainAxisSize.min,
  271. children: <Widget>[
  272. Padding(
  273. padding: EdgeInsets.symmetric(vertical: 6.0),
  274. child: Text(
  275. "${rank!.name}",
  276. style: TextStyle(color: Colors.white, fontSize: 20, fontWeight: FontWeight.w600),
  277. ),
  278. ),
  279. Padding(
  280. padding: EdgeInsets.symmetric(vertical: 2.0),
  281. child: Text(
  282. "${rank!.introduce}",
  283. style: Theme.of(context).textTheme.bodyText1!,
  284. ),
  285. ),
  286. GestureDetector(
  287. onTap: () {
  288. itemScrollController.scrollTo(index: user?.position != null ? (user?.position ?? 0) - 1 : records.length - 1, duration: Duration(milliseconds: 500));
  289. },
  290. child: Container(
  291. margin: EdgeInsets.fromLTRB(0.0, 12.0, 0, 0),
  292. child: Image.asset(
  293. "lib/assets/img/rank_btn_myrank.png",
  294. width: 103.0,
  295. height: 28.0,
  296. ))),
  297. ],
  298. ),
  299. ),
  300. ))
  301. : Container()),
  302. ),
  303. Positioned(
  304. left: 0.0,
  305. top: 0.0,
  306. child: SafeArea(
  307. child: IconButton(
  308. icon: Image.asset("lib/assets/img/topbar_return_white.png"),
  309. onPressed: () {
  310. Navigator.pop(context);
  311. },
  312. ),
  313. )),
  314. // Positioned(
  315. // top: 0,
  316. // right: 0,
  317. // child: SafeArea(
  318. // child: IconButton(
  319. // icon: Image.asset("lib/assets/img/topbar_problom.png"),
  320. // onPressed: () {
  321. // if (rank != null) {
  322. // NavigatorUtil.goPage(context, (context) => RankIntroduce(rank!));
  323. // }
  324. // },
  325. // ),
  326. // )),
  327. Positioned(
  328. top: height - 10,
  329. left: 0,
  330. right: 0,
  331. bottom: 0,
  332. child: CustomScrollView(
  333. controller: _controller,
  334. slivers: <Widget>[
  335. SliverPersistentHeader(
  336. delegate: PersistentHeader(
  337. min: 60,
  338. max: 60,
  339. child: Container(
  340. height: 80.0,
  341. alignment: Alignment.center,
  342. decoration: BoxDecoration(
  343. borderRadius: BorderRadius.only(topRight: Radius.circular(10.0), topLeft: Radius.circular(10.0)),
  344. color: Colors.white,
  345. ),
  346. padding: EdgeInsets.only(bottom: 10, top: 10.0),
  347. child: TabBar(
  348. isScrollable: true,
  349. indicatorPadding: EdgeInsets.symmetric(horizontal: 8),
  350. indicatorWeight: 3,
  351. labelPadding: EdgeInsets.symmetric(horizontal: 30),
  352. controller: _tabController,
  353. tabs: _tabs.map((e) => Tab(text: e)).toList(),
  354. onTap: (i) {
  355. initData(i);
  356. },
  357. ),
  358. )),
  359. pinned: true,
  360. ),
  361. if (_isLoading)
  362. SliverToBoxAdapter(
  363. child: RequestLoadingWidget(),
  364. ),
  365. if (records.isNotEmpty == true)
  366. SliverFillRemaining(
  367. child: ScrollablePositionedList.builder(
  368. itemCount: records.length,
  369. itemBuilder: (context, index) {
  370. return _buildRankItemWidget(index + 1, records[index]);
  371. },
  372. itemScrollController: itemScrollController,
  373. itemPositionsListener: itemPositionsListener,
  374. ),
  375. ),
  376. ],
  377. ))
  378. ],
  379. )));
  380. }
  381. }