CompetitionRooms.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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/widgets.dart';
  5. import 'package:flutter_redux/flutter_redux.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. import 'package:wanna_battle/model/UserPlayTimes.dart';
  8. import 'package:wanna_battle/styles/totast.dart';
  9. import '../model/CompetitionInfo.dart';
  10. import '../styles/colors.dart';
  11. import '../model/HouseInfo.dart';
  12. import '../net/HttpManager.dart';
  13. import '../net/Result.dart';
  14. import './RoomInfoNew.dart';
  15. import '../redux/AppState.dart';
  16. import '../plugins/ScreenStramPlugin.dart';
  17. import './CompetitionRank.dart';
  18. import '../Constants.dart';
  19. import './CompetitionRank.dart';
  20. import 'CompetitionNotice.dart';
  21. import 'UserGuid.dart';
  22. class CompetitionRooms extends StatefulWidget {
  23. CompetitionRooms(this.competitionInfo);
  24. final CompetitionInfo competitionInfo;
  25. @override
  26. State<StatefulWidget> createState() {
  27. return _CompetitionRoomsState();
  28. }
  29. }
  30. class _CompetitionRoomsState extends State<CompetitionRooms> {
  31. List<HouseInfo> houseList = [];
  32. int restTimes = 0;
  33. @override
  34. void initState() {
  35. super.initState();
  36. Future.delayed(Duration.zero, () async {
  37. _getRooms();
  38. _getPlayTimes();
  39. final prefs = await SharedPreferences.getInstance();
  40. final bool showGuid2 = prefs.getBool('showGuid2') ?? true;
  41. if (showGuid2) {
  42. await prefs.setBool('showGuid2', false);
  43. await showUserGuide2(context);
  44. }
  45. // showNotice(context);
  46. });
  47. }
  48. @override
  49. Widget build(BuildContext context) {
  50. final appBar = AppBar(
  51. title: Text(widget.competitionInfo.competitionName),
  52. centerTitle: true,
  53. elevation: 0,
  54. backgroundColor: Colors.transparent,
  55. );
  56. return Scaffold(
  57. backgroundColor: Color(0xff19213B),
  58. body: Column(
  59. crossAxisAlignment: CrossAxisAlignment.stretch,
  60. children: <Widget>[
  61. Container(
  62. decoration: BoxDecoration(
  63. image: DecorationImage(
  64. image: NetworkImage(widget.competitionInfo.pic),
  65. fit: BoxFit.cover,
  66. )),
  67. height: MediaQuery.of(context).size.width * 200 / 375,
  68. padding: EdgeInsets.only(left: 15, right: 15),
  69. child: Column(
  70. mainAxisAlignment: MainAxisAlignment.center,
  71. crossAxisAlignment: CrossAxisAlignment.center,
  72. children: <Widget>[
  73. appBar,
  74. Expanded(
  75. child: Column(
  76. children: <Widget>[
  77. Container(
  78. child: Text(
  79. widget.competitionInfo.intro ?? '',
  80. style: TextStyle(
  81. color: Color(0xADFFFFFF),
  82. fontSize: 13,
  83. ),
  84. ),
  85. ),
  86. Container(
  87. color: Colors.yellow,
  88. margin: EdgeInsets.only(top: 10),
  89. padding: EdgeInsets.fromLTRB(6, 1, 6, 2),
  90. child: Text(
  91. '剩余参赛次数 $restTimes次',
  92. style: TextStyle(
  93. color: Color(0xFF293354),
  94. fontSize: 12,
  95. ),
  96. ),
  97. ),
  98. Container(
  99. margin: EdgeInsets.only(top: 10),
  100. child: Row(
  101. children: <Widget>[
  102. Expanded(
  103. child: MaterialButton(
  104. color: Color(0xFFE56B45),
  105. elevation: 0,
  106. highlightElevation: 0,
  107. child: Row(
  108. mainAxisAlignment: MainAxisAlignment.center,
  109. crossAxisAlignment: CrossAxisAlignment.center,
  110. children: <Widget>[
  111. Image.asset(
  112. 'images/icon_jiangpin.png',
  113. width: 19,
  114. ),
  115. Container(
  116. margin: EdgeInsets.only(left: 15),
  117. child: Text(
  118. '奖品规则',
  119. style: TextStyle(color: Colors.white, fontSize: 14),
  120. ),
  121. )
  122. ],
  123. ),
  124. onPressed: () {
  125. showBonus();
  126. },
  127. ),
  128. ),
  129. Container(width: 15),
  130. Expanded(
  131. child: MaterialButton(
  132. color: PRIMARY_COLOR,
  133. elevation: 0,
  134. highlightElevation: 0,
  135. child: Row(
  136. mainAxisAlignment: MainAxisAlignment.center,
  137. crossAxisAlignment: CrossAxisAlignment.center,
  138. children: <Widget>[
  139. Image.asset(
  140. 'images/icon_paihangbang.png',
  141. width: 19,
  142. ),
  143. Container(
  144. margin: EdgeInsets.only(left: 15),
  145. child: Text(
  146. '排行榜',
  147. style: TextStyle(color: Colors.white, fontSize: 14),
  148. ),
  149. )
  150. ],
  151. ),
  152. onPressed: () {
  153. Navigator.push(context, CupertinoPageRoute(builder: (context) => CompetitionRank(competitionId: widget.competitionInfo.id)));
  154. },
  155. ),
  156. )
  157. ],
  158. ),
  159. )
  160. ],
  161. ),
  162. )
  163. ],
  164. ),
  165. ),
  166. Expanded(
  167. child: RefreshIndicator(
  168. onRefresh: () {
  169. _getPlayTimes();
  170. return _getRooms();
  171. },
  172. child: ListView(
  173. padding: EdgeInsets.only(bottom: 10),
  174. children: houseList.map((f) => _Room(f)).toList(),
  175. ),
  176. ))
  177. ],
  178. ),
  179. );
  180. }
  181. Future<void> _getRooms() async {
  182. final Result res = await HttpManager.get('houseInfo/all', data: {
  183. 'competitionId': widget.competitionInfo.id,
  184. 'advancedQuery': ' AND_,status_flag_,!=_,' + HouseStatus.SETTLEMENT.toString(),
  185. });
  186. final List<HouseInfo> list = [];
  187. if (res.success && res.data != null) {
  188. for (var item in res.data) {
  189. final HouseInfo _temHouse = HouseInfo.fromJson(item);
  190. list.add(_temHouse);
  191. }
  192. setState(() {
  193. houseList = list;
  194. });
  195. }
  196. }
  197. Future<void> _getPlayTimes() async {
  198. final Result res = await HttpManager.get('playerInfo/getTodayPlayTimes');
  199. if (res.success) {
  200. int rest = 0;
  201. UserPlayTimes userPlayTimes = UserPlayTimes.fromJson(res.data);
  202. if (widget.competitionInfo.type == 1) {
  203. rest = userPlayTimes.totalNormal - userPlayTimes.usedNormal;
  204. } else {
  205. rest = userPlayTimes.totalAdvanced - userPlayTimes.usedAdvanced;
  206. }
  207. if (rest < 0) {
  208. rest = 0;
  209. }
  210. setState(() {
  211. restTimes = rest;
  212. });
  213. }
  214. print(res);
  215. }
  216. void showBonus() {
  217. showGeneralDialog(
  218. context: context,
  219. barrierDismissible: true,
  220. pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
  221. return Center(
  222. child: Material(
  223. color: Colors.transparent,
  224. child: Column(
  225. mainAxisSize: MainAxisSize.min,
  226. mainAxisAlignment: MainAxisAlignment.center,
  227. children: <Widget>[
  228. Container(
  229. width: 270,
  230. padding: EdgeInsets.all(0),
  231. decoration: BoxDecoration(color: Color(0xE6293559), border: Border.all(width: 1, color: Color(0xFF1990F8))),
  232. child: Column(
  233. mainAxisSize: MainAxisSize.min,
  234. crossAxisAlignment: CrossAxisAlignment.stretch,
  235. children: <Widget>[
  236. Container(
  237. margin: EdgeInsets.only(top: 25),
  238. child: Text(
  239. '奖品规则',
  240. style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold),
  241. textAlign: TextAlign.center,
  242. ),
  243. ),
  244. Container(
  245. margin: EdgeInsets.only(top: 20, left: 20, right: 20),
  246. child: Text(
  247. '按照比赛周期内\n各个玩家累计积分排名发放奖金',
  248. style: TextStyle(color: Colors.white, fontSize: 14),
  249. textAlign: TextAlign.left,
  250. ),
  251. ),
  252. Container(
  253. margin: EdgeInsets.only(top: 10, bottom: 40),
  254. padding: EdgeInsets.only(left: 20, right: 20),
  255. child: Text(
  256. widget.competitionInfo.bonus ?? '',
  257. style: TextStyle(color: Colors.white, fontSize: 14),
  258. ),
  259. )
  260. ],
  261. ),
  262. ),
  263. Container(
  264. margin: EdgeInsets.only(top: 20),
  265. child: GestureDetector(
  266. child: Image.asset('images/icon_guanbi.png'),
  267. onTap: () {
  268. Navigator.of(context).pop();
  269. },
  270. ),
  271. )
  272. ],
  273. ),
  274. ),
  275. );
  276. },
  277. barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
  278. barrierColor: Colors.black87,
  279. transitionDuration: const Duration(milliseconds: 300),
  280. );
  281. }
  282. }
  283. class _Room extends StatelessWidget {
  284. _Room(this.houseInfo);
  285. final HouseInfo houseInfo;
  286. @override
  287. Widget build(BuildContext context) {
  288. var status;
  289. var borderColor;
  290. var backgroundColor;
  291. var imgSrc;
  292. switch (houseInfo.statusFlag) {
  293. case 0:
  294. case 1:
  295. status = '等待中';
  296. borderColor = Color(0xFF1C62A7);
  297. backgroundColor = Color(0x990D2F54);
  298. imgSrc = 'images/icon_dengdaizhong.png';
  299. break;
  300. case 2:
  301. status = '进行中';
  302. borderColor = Color(0xFF8C8146);
  303. backgroundColor = Color(0x990D2F54);
  304. imgSrc = 'images/icon_jinxingzhong.png';
  305. break;
  306. default:
  307. status = '已结束';
  308. borderColor = Color(0xFF414854);
  309. backgroundColor = Color(0xFF152536);
  310. imgSrc = 'images/icon_yijiesu.png';
  311. break;
  312. }
  313. return GestureDetector(
  314. onTap: () {
  315. joinRoom(context, houseInfo.id);
  316. },
  317. child: Container(
  318. height: 43,
  319. margin: EdgeInsets.fromLTRB(15, 10, 15, 0),
  320. decoration: BoxDecoration(
  321. color: backgroundColor,
  322. border: Border.all(color: borderColor, width: 2),
  323. ),
  324. child: Row(
  325. children: <Widget>[
  326. Expanded(
  327. child: Container(
  328. margin: EdgeInsets.only(left: 13),
  329. child: Text(
  330. houseInfo.houseName,
  331. style: TextStyle(fontSize: 14, color: Colors.white),
  332. ),
  333. ),
  334. ),
  335. Container(
  336. width: 105,
  337. child: RichText(
  338. text: TextSpan(
  339. style: TextStyle(color: Colors.white, fontSize: 11),
  340. children: <TextSpan>[
  341. TextSpan(text: '参赛人数:'),
  342. TextSpan(text: (houseInfo.playerNumber ?? 0).toString(), style: TextStyle(color: Color(0xFF38A968))),
  343. TextSpan(text: '/${houseInfo.maxNumber}'),
  344. ],
  345. ),
  346. ),
  347. ),
  348. Container(
  349. width: 60,
  350. height: 43,
  351. padding: EdgeInsets.only(left: 10),
  352. alignment: Alignment.center,
  353. decoration: BoxDecoration(
  354. image: DecorationImage(
  355. image: AssetImage(imgSrc),
  356. fit: BoxFit.cover,
  357. ),
  358. ),
  359. child: Text(
  360. status,
  361. style: TextStyle(
  362. color: Colors.white,
  363. fontSize: 14,
  364. ),
  365. ),
  366. )
  367. ],
  368. ),
  369. ),
  370. );
  371. }
  372. Future<void> joinRoom(context, houseId) async {
  373. final hasPermission = await ScreenStreamPlugin.checkPermission();
  374. if (!hasPermission) {
  375. showDialog(
  376. context: context,
  377. builder: (context) => AlertDialog(
  378. title: Text('需要悬浮窗权限'),
  379. contentTextStyle: TextStyle(color: Colors.black87),
  380. content: Text('请在点击确定后勾选"允许显示在其他应用的上层"'),
  381. actions: <Widget>[
  382. FlatButton(
  383. child: Text('确定'),
  384. onPressed: () {
  385. Navigator.of(context).pop();
  386. ScreenStreamPlugin.requestPermission();
  387. },
  388. ),
  389. ],
  390. ),
  391. );
  392. return;
  393. }
  394. // Navigator.push(context, CupertinoPageRoute(builder: (context) => RoomInfo(houseInfo)));
  395. Toast.show(context, '加载中', -1, 'loading');
  396. Result res = await HttpManager.post('houseInfo/join', data: {'houseId': houseId, 'userId': StoreProvider.of<AppState>(context).state.userInfo.id});
  397. Toast.hide();
  398. if (res.success) {
  399. Navigator.push(context, CupertinoPageRoute(builder: (context) => RoomInfo(houseInfo)));
  400. } else {
  401. Toast.show(context, res.error, 1500, 'info');
  402. }
  403. }
  404. }