rank_info.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //import 'package:flutter/.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:sport/bean/rank_info.dart';
  5. import 'package:sport/constant/ui.dart' show ui_padding, ui_margin_list;
  6. import 'package:sport/router/navigator_util.dart';
  7. import 'package:sport/services/api/inject_api.dart';
  8. import 'package:sport/widgets/label.dart';
  9. import 'package:sport/widgets/loading.dart';
  10. import 'package:sport/widgets/misc.dart';
  11. import 'game_info.dart';
  12. class RankInfo extends StatefulWidget {
  13. @override
  14. _RankInfoState createState() {
  15. // TODO: implement createState
  16. return _RankInfoState();
  17. }
  18. }
  19. class _RankInfoState extends State<RankInfo> with InjectApi, AutomaticKeepAliveClientMixin {
  20. List<RankInfoData> _rankData = [];
  21. @override
  22. void initState() {
  23. super.initState();
  24. // 做一手简单的缓存 全局变量好像不太好...
  25. if (_rankData == null || _rankData.isEmpty) {
  26. api.getRankInfoAll().then((data) {
  27. setState(() {
  28. _rankData = data.results ?? [];
  29. });
  30. });
  31. }
  32. }
  33. List<Widget> getRankHeaderItem() {
  34. int num = 0;
  35. List<Widget> _rankHeaders = [];
  36. for (var item in _rankData) {
  37. num++;
  38. if (item.isGame == 0) {
  39. _rankHeaders.add(RankHeaderItem(item.name, item.introduce, "${item.rateBegin} ~ ${item.rateEnd}", num, item.id));
  40. }
  41. }
  42. return _rankHeaders;
  43. }
  44. List<Widget> getRankGameInfo() {
  45. List<Widget> _rankGames = [];
  46. for (var item in _rankData) {
  47. if (item.isGame == 1) {
  48. _rankGames.add(GameItem(type: 2, name: item.name, imgUrl: item.game.cover, desc: item.introduce, time: "${item.rateBegin} ~ ${item.rateEnd}", id: item.gameId));
  49. }
  50. }
  51. return _rankGames;
  52. }
  53. @override
  54. Widget build(BuildContext context) {
  55. super.build(context);
  56. if (_rankData != null && _rankData.isNotEmpty) {
  57. return SingleChildScrollView(
  58. padding: EdgeInsets.zero,
  59. child: Column(
  60. crossAxisAlignment: CrossAxisAlignment.start,
  61. children: <Widget>[
  62. // header
  63. Column(
  64. children: getRankHeaderItem(),
  65. ),
  66. // label
  67. Padding(
  68. padding: const EdgeInsets.only(top: 5.0),
  69. child: buildLabelWidget(context, "运动评分榜"),
  70. ),
  71. // 游戏评分榜
  72. Column(
  73. children: getRankGameInfo(),
  74. ),
  75. SizedBox(
  76. height: 20,
  77. )
  78. ],
  79. ),
  80. );
  81. } else {
  82. return RequestLoadingWidget();
  83. }
  84. }
  85. @override
  86. bool get wantKeepAlive => true;
  87. }
  88. // header
  89. class RankHeaderItem extends StatelessWidget {
  90. String _title;
  91. String _intro;
  92. dynamic _time;
  93. int _index;
  94. int _id;
  95. RankHeaderItem(this._title, this._intro, this._time, this._index, this._id);
  96. @override
  97. Widget build(BuildContext context) {
  98. return InkWell(
  99. onTap: () =>
  100. NavigatorUtil.goRankDetails(context, _id, 0),
  101. child: Container(
  102. width: double.infinity,
  103. margin: EdgeInsets.fromLTRB(ui_padding, 6.0, ui_padding, 6.0),
  104. padding: EdgeInsets.all(ui_padding),
  105. decoration: new BoxDecoration(
  106. borderRadius: new BorderRadius.all(Radius.circular(10.0)), // 也可控件一边圆角大小
  107. image: DecorationImage(
  108. image: AssetImage("lib/assets/img/rank_bg$_index.png"), // NetWorkImage 返回的是Image
  109. fit: BoxFit.cover,
  110. //设置四周边框
  111. ),
  112. ),
  113. child: Padding(padding: EdgeInsets.symmetric(vertical: 5.0), child: RankHeaderIntro(title: "$_title", intro: "$_intro", time: "$_time")),
  114. ),
  115. );
  116. }
  117. }
  118. // header 里面的封装
  119. class RankHeaderIntro extends StatelessWidget {
  120. String title;
  121. String intro;
  122. String time;
  123. RankHeaderIntro({@required this.title, @required this.intro, @required this.time});
  124. @override
  125. Widget build(BuildContext context) {
  126. return Column(
  127. crossAxisAlignment: CrossAxisAlignment.start,
  128. mainAxisAlignment: MainAxisAlignment.center,
  129. children: <Widget>[
  130. Text(
  131. title,
  132. style: Theme.of(context).textTheme.headline3.copyWith(color: Colors.white),
  133. ),
  134. Padding(
  135. child: Text(intro,
  136. style: Theme.of(context).textTheme.bodyText1),
  137. padding: EdgeInsets.symmetric(vertical: 4.0),
  138. ),
  139. Text(time, style: Theme.of(context).textTheme.bodyText1)
  140. ],
  141. );
  142. }
  143. }
  144. // 游戏评分榜
  145. //class RankGameInfo extends StatelessWidget {
  146. // @override
  147. // Widget build(BuildContext context) {
  148. // // TODO: implement build
  149. // return Container(
  150. // padding: EdgeInsets.only(bottom: 30),
  151. // child: Column(
  152. // children: <Widget>[
  153. // ListView.builder(
  154. // shrinkWrap: true,
  155. // itemBuilder: (context, index) {
  156. // return GameItem(type: 2);
  157. // },
  158. // itemCount: 3)
  159. // ],
  160. // ),
  161. // );
  162. // }
  163. //}