openRoom.dart 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_picker/flutter_picker.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:file_picker/file_picker.dart';
  5. import '../styles/colors.dart';
  6. import 'dart:io';
  7. import 'dart:async';
  8. import 'dart:convert';
  9. import 'dart:ui';
  10. import '../styles/totast.dart';
  11. import '../pages/roomInfo.dart';
  12. import 'package:flutter_redux/flutter_redux.dart';
  13. import '../redux/AppState.dart';
  14. import '../net/HttpManager.dart';
  15. import '../net/Result.dart';
  16. import 'package:dio/dio.dart';
  17. class OpenRoom extends StatefulWidget {
  18. OpenRoom({Key key, this.roomFlag}) : super(key: key);
  19. final String roomFlag; // 用来储存传递过来的值
  20. @override
  21. OpenRoomState createState() => OpenRoomState();
  22. }
  23. class OpenRoomState extends State<OpenRoom> {
  24. TextStyle titleStyle = TextStyle(color: Colors.white, fontSize: 14);
  25. TextStyle valStyle =
  26. TextStyle(color: Colors.white, fontSize: 15, fontWeight: FontWeight.w500);
  27. Map<String, dynamic> editRoomInfo = {
  28. "gameId": 1,
  29. "houseLevel": 1,
  30. "maxNumber": 10
  31. };
  32. List gameList = [];
  33. List levelList = [];
  34. void getFilePath() async {
  35. String filePath = await FilePicker.getFilePath(type: FileType.ANY);
  36. File _file = File(filePath);
  37. Toast.show(context, '加载中', -1, 'loading');
  38. Result res = await HttpManager.post("assets/uploadFile", data: {
  39. "file": UploadFileInfo(_file, filePath),
  40. });
  41. Toast.hide();
  42. if (res.success) {
  43. setState(() {
  44. editRoomInfo['video'] = res.data[0];
  45. });
  46. } else {}
  47. }
  48. void saveInfo() async {
  49. if (!editRoomInfo.containsKey('gameId')) {
  50. Toast.show(context, '请选择游戏', 1500, 'info');
  51. return;
  52. }
  53. if (editRoomInfo['houseName'] == null || editRoomInfo['houseName'] == '') {
  54. Toast.show(context, '请输入房间标题', 1500, 'info');
  55. return;
  56. }
  57. if (!editRoomInfo.containsKey('houseLevel')) {
  58. Toast.show(context, '请选择房间等级', 1500, 'info');
  59. return;
  60. }
  61. if (editRoomInfo['houseType'] == '1' &&
  62. (editRoomInfo['gameHouseId'] == null ||
  63. editRoomInfo['gameHouseId'] == '')) {
  64. Toast.show(context, '请录入游戏房间号', 1500, 'info');
  65. return;
  66. }
  67. if (editRoomInfo['houseType'] == '1' &&
  68. (editRoomInfo['gameHousePassword'] == null ||
  69. editRoomInfo['gameHousePassword'] == '')) {
  70. Toast.show(context, '请录入游戏房间密码', 1500, 'info');
  71. return;
  72. }
  73. editRoomInfo['userId'] =
  74. StoreProvider.of<AppState>(context).state.userInfo.id;
  75. Toast.show(context, '加载中', -1, 'loading');
  76. Result res = await HttpManager.post("houseInfo/save", data: editRoomInfo);
  77. Toast.hide();
  78. if (res.success) {
  79. HttpManager.post("houseInfo/join", data: {
  80. "houseId": res.data,
  81. "userId": StoreProvider.of<AppState>(context).state.userInfo.id
  82. });
  83. Future.delayed(Duration(milliseconds: 100), () {
  84. Toast.show(context, '创建成功', 1500, 'success');
  85. });
  86. Future.delayed(Duration(milliseconds: 1500), () {
  87. Navigator.pushAndRemoveUntil(
  88. context,
  89. new CupertinoPageRoute(
  90. builder: (context) =>
  91. new RoomInfo(roomId: res.data.toString())),
  92. ModalRoute.withName('/'));
  93. });
  94. } else {}
  95. }
  96. @override
  97. void initState() {
  98. super.initState();
  99. print(widget.roomFlag);
  100. editRoomInfo['houseType'] = widget.roomFlag;
  101. Future.delayed(Duration.zero, () {
  102. getInfo();
  103. });
  104. }
  105. getInfo() async {
  106. Toast.show(context, '加载中', -1, 'loading');
  107. Result res = await HttpManager.get("gameInfo/all");
  108. if (res.success) {
  109. setState(() {
  110. gameList = res.data;
  111. if (res.data.length > 0) {
  112. editRoomInfo['gameId'] = res.data[0]['id'];
  113. }
  114. });
  115. } else {}
  116. Result res2 = await HttpManager.get("houseLevel/all");
  117. Toast.hide();
  118. if (res2.success) {
  119. setState(() {
  120. levelList = res2.data;
  121. if (levelList.length > 0) {
  122. editRoomInfo['houseLevel'] = levelList[0]['id'];
  123. }
  124. });
  125. } else {}
  126. }
  127. @override
  128. Widget build(BuildContext context) {
  129. Map chooseGameInfo = {};
  130. for (var item in gameList) {
  131. print(item);
  132. if (item['id'].toString() == editRoomInfo['gameId'].toString()) {
  133. chooseGameInfo = item;
  134. }
  135. }
  136. print(chooseGameInfo);
  137. Map chooseLevelInfo = {};
  138. for (var item in levelList) {
  139. if (item['id'].toString() == editRoomInfo['houseLevel'].toString()) {
  140. chooseLevelInfo = item;
  141. }
  142. }
  143. print(chooseGameInfo);
  144. return WillPopScope(
  145. child: Scaffold(
  146. appBar: AppBar(
  147. backgroundColor: PRIMARY_COLOR,
  148. title: Text('创建' + (widget.roomFlag == '0' ? '普通' : '官方') + '房间'),
  149. centerTitle: true,
  150. elevation: 0,
  151. actions: <Widget>[
  152. Container(
  153. width: 60,
  154. child: FlatButton(
  155. highlightColor: PRIMARY_COLOR,
  156. padding: EdgeInsets.only(right: 0),
  157. child: Text('规则',
  158. style: TextStyle(color: Colors.white, fontSize: 13)),
  159. onPressed: () {},
  160. ),
  161. )
  162. ],
  163. ),
  164. body: Container(
  165. color: BG_COLOR,
  166. width: double.infinity,
  167. height: double.infinity,
  168. child: SingleChildScrollView(
  169. child: Container(
  170. color: BG_COLOR,
  171. child: Column(
  172. children: <Widget>[
  173. Container(
  174. padding: EdgeInsets.symmetric(horizontal: 15),
  175. color: BG_COLOR,
  176. height: 60,
  177. child: GestureDetector(
  178. child: Row(
  179. children: <Widget>[
  180. Text('选择游戏', style: titleStyle),
  181. Expanded(
  182. flex: 1,
  183. child: Text(
  184. chooseGameInfo.containsKey('gameName')
  185. ? chooseGameInfo['gameName']
  186. : '',
  187. textAlign: TextAlign.right,
  188. style: valStyle,
  189. ),
  190. ),
  191. Image.asset('images/icon_inter.png', width: 24)
  192. ],
  193. ),
  194. onTap: () {
  195. showPicker(context);
  196. },
  197. )),
  198. Container(
  199. width: double.infinity,
  200. color: BG_SUB_COLOR,
  201. height: 210,
  202. child: Column(
  203. mainAxisAlignment: MainAxisAlignment.center,
  204. children: <Widget>[
  205. FlatButton(
  206. padding: EdgeInsets.all(0),
  207. color: Color(0xFF464B6A),
  208. highlightColor: Color(0xFF333557),
  209. child: Container(
  210. height: 38,
  211. width: 160,
  212. decoration: BoxDecoration(
  213. gradient: LinearGradient(
  214. begin: Alignment.topRight,
  215. colors: [Colors.black12, Colors.black38],
  216. )),
  217. child: Row(
  218. mainAxisAlignment: MainAxisAlignment.center,
  219. children: <Widget>[
  220. Padding(
  221. padding: EdgeInsets.only(right: 8),
  222. child: Image.asset(
  223. 'images/icon_shipin.png',
  224. width: 20,
  225. )),
  226. Text('上传图片或视频', style: titleStyle)
  227. ],
  228. ),
  229. ),
  230. onPressed: () {
  231. getFilePath();
  232. },
  233. ),
  234. Padding(
  235. padding: EdgeInsets.only(top: 12),
  236. child: Text(
  237. editRoomInfo.containsKey('video')
  238. ? '已选择'
  239. : '不上传则自动使用官方默认视频',
  240. style: TextStyle(
  241. color: Color(0xFF9BA0AE), fontSize: 13),
  242. ),
  243. )
  244. ],
  245. ),
  246. ),
  247. //房间标题
  248. Container(
  249. height: 60,
  250. color: BG_COLOR,
  251. padding:
  252. EdgeInsets.symmetric(horizontal: 15, vertical: 8),
  253. child: TextField(
  254. textAlign: TextAlign.end,
  255. style: valStyle,
  256. maxLength: 10,
  257. decoration: InputDecoration(
  258. hintText: "请输入房间标题",
  259. hintStyle: TextStyle(
  260. color: Color(0xFF727785), fontSize: 13),
  261. prefixIcon: Padding(
  262. padding: EdgeInsets.symmetric(vertical: 12),
  263. child: Text('房间标题', style: titleStyle),
  264. ),
  265. border: InputBorder.none,
  266. counterStyle: TextStyle(fontSize: 0)),
  267. onChanged: (value) {
  268. editRoomInfo['houseName'] = value;
  269. },
  270. ),
  271. ),
  272. Container(
  273. height: 1,
  274. color: BG_SUB_COLOR,
  275. ),
  276. //房间简介
  277. Container(
  278. height: 60,
  279. color: BG_COLOR,
  280. padding:
  281. EdgeInsets.symmetric(horizontal: 15, vertical: 8),
  282. child: TextField(
  283. textAlign: TextAlign.end,
  284. style: valStyle,
  285. maxLength: 15,
  286. decoration: InputDecoration(
  287. hintText: "请输入房间简介",
  288. hintStyle: TextStyle(
  289. color: Color(0xFF727785), fontSize: 13),
  290. prefixIcon: Padding(
  291. padding: EdgeInsets.symmetric(vertical: 12),
  292. child: Text('房间简介', style: titleStyle),
  293. ),
  294. border: InputBorder.none,
  295. counterStyle: TextStyle(fontSize: 0)),
  296. onChanged: (value) {
  297. editRoomInfo['houseAbstract'] = value;
  298. },
  299. ),
  300. ),
  301. Container(
  302. height: 1,
  303. color: BG_SUB_COLOR,
  304. ),
  305. //房间等级
  306. Container(
  307. height: 60,
  308. color: BG_COLOR,
  309. padding: EdgeInsets.symmetric(horizontal: 15),
  310. child: GestureDetector(
  311. child: Row(
  312. children: <Widget>[
  313. Text(
  314. '房间等级',
  315. style: titleStyle,
  316. ),
  317. Expanded(
  318. flex: 1,
  319. child: Row(
  320. mainAxisAlignment: MainAxisAlignment.end,
  321. children: <Widget>[
  322. Image.network(
  323. chooseLevelInfo.containsKey('icon')
  324. ? chooseLevelInfo['icon']
  325. : '',
  326. width: 22,
  327. ),
  328. Text(
  329. chooseLevelInfo.containsKey('levelName')
  330. ? chooseLevelInfo['levelName']
  331. : '',
  332. style:
  333. TextStyle(color: Color(0xFFF9D881)),
  334. ),
  335. Padding(
  336. padding:
  337. EdgeInsets.only(left: 20, right: 2),
  338. child: Image.asset(
  339. 'images/icon_jinbi_da_bai.png',
  340. width: 20),
  341. ),
  342. Text(
  343. 'X' +
  344. (chooseLevelInfo
  345. .containsKey('entryCoin')
  346. ? chooseLevelInfo['entryCoin']
  347. .toString()
  348. : ''),
  349. style: TextStyle(
  350. color: Colors.white,
  351. fontSize: 15,
  352. fontWeight: FontWeight.w500),
  353. )
  354. ],
  355. ),
  356. ),
  357. Padding(
  358. padding: EdgeInsets.only(left: 4),
  359. child: Image.asset('images/icon_inter.png',
  360. width: 24))
  361. ],
  362. ),
  363. onTap: () {
  364. showLevelPicker(context);
  365. },
  366. )),
  367. Container(
  368. height: 1,
  369. color: BG_SUB_COLOR,
  370. ),
  371. //房间人数
  372. Container(
  373. padding: EdgeInsets.symmetric(horizontal: 15),
  374. color: BG_COLOR,
  375. height: 60,
  376. child: GestureDetector(
  377. child: Row(
  378. children: <Widget>[
  379. Text('房间人数', style: titleStyle),
  380. Expanded(
  381. flex: 1,
  382. child: Text(
  383. editRoomInfo['maxNumber'].toString(),
  384. textAlign: TextAlign.right,
  385. style: valStyle,
  386. ),
  387. ),
  388. Image.asset('images/icon_inter.png', width: 24)
  389. ],
  390. ),
  391. onTap: () {
  392. print('111111');
  393. showNumPicker(chooseLevelInfo);
  394. },
  395. )),
  396. _bottomWidget(),
  397. //创建房间
  398. Container(
  399. padding: EdgeInsets.fromLTRB(15, 39, 15, 28),
  400. height: 109,
  401. color: BG_COLOR,
  402. child: FlatButton(
  403. color: PRIMARY_COLOR,
  404. child: Row(
  405. mainAxisAlignment: MainAxisAlignment.center,
  406. children: <Widget>[
  407. Padding(
  408. padding: EdgeInsets.only(right: 6),
  409. child: Image.asset('images/icon_jinbi_da_bai.png',
  410. width: 20),
  411. ),
  412. Text(
  413. 'X' +
  414. (chooseLevelInfo.containsKey('entryCoin')
  415. ? chooseLevelInfo['entryCoin'].toString()
  416. : ''),
  417. style: TextStyle(
  418. color: Colors.white,
  419. fontSize: 16,
  420. fontWeight: FontWeight.w500),
  421. ),
  422. Padding(
  423. padding: EdgeInsets.only(left: 20),
  424. child: Text(
  425. '创建房间',
  426. style: TextStyle(
  427. color: Colors.white,
  428. fontSize: 16,
  429. fontWeight: FontWeight.w500),
  430. ),
  431. )
  432. ],
  433. ),
  434. onPressed: () {
  435. saveInfo();
  436. },
  437. ),
  438. )
  439. ],
  440. ),
  441. ),
  442. ),
  443. )),
  444. onWillPop: () {
  445. Toast.hide();
  446. Navigator.pop(context);
  447. return Future.value(false);
  448. },
  449. );
  450. }
  451. Widget _bottomWidget() {
  452. if (widget.roomFlag == '1')
  453. return Column(
  454. children: <Widget>[
  455. Container(
  456. height: 10,
  457. color: BG_SUB_COLOR,
  458. ),
  459. //游戏房间号
  460. Container(
  461. height: 60,
  462. color: BG_COLOR,
  463. padding: EdgeInsets.symmetric(horizontal: 15, vertical: 8),
  464. child: TextField(
  465. textAlign: TextAlign.end,
  466. style: valStyle,
  467. maxLength: 15,
  468. decoration: InputDecoration(
  469. hintText: "请输入游戏房间号",
  470. hintStyle: TextStyle(color: Color(0xFF727785), fontSize: 13),
  471. prefixIcon: Padding(
  472. padding: EdgeInsets.symmetric(vertical: 12),
  473. child: Text('游戏房间号', style: titleStyle),
  474. ),
  475. border: InputBorder.none,
  476. counterStyle: TextStyle(fontSize: 0)),
  477. onChanged: (value) {
  478. editRoomInfo['gameHouseId'] = value;
  479. },
  480. ),
  481. ),
  482. Container(
  483. height: 1,
  484. color: BG_SUB_COLOR,
  485. ),
  486. //游戏中房间密码
  487. Container(
  488. height: 60,
  489. color: BG_COLOR,
  490. padding: EdgeInsets.symmetric(horizontal: 15, vertical: 8),
  491. child: TextField(
  492. textAlign: TextAlign.end,
  493. style: valStyle,
  494. maxLength: 10,
  495. decoration: InputDecoration(
  496. hintText: "请输入密码",
  497. hintStyle: TextStyle(color: Color(0xFF727785), fontSize: 13),
  498. prefixIcon: Padding(
  499. padding: EdgeInsets.symmetric(vertical: 12),
  500. child: Text('游戏中房间密码', style: titleStyle),
  501. ),
  502. border: InputBorder.none,
  503. counterStyle: TextStyle(fontSize: 0)),
  504. onChanged: (value) {
  505. editRoomInfo['gameHousePassword'] = value;
  506. },
  507. ),
  508. ),
  509. ],
  510. );
  511. else {
  512. return Container();
  513. }
  514. }
  515. showPicker(BuildContext context) {
  516. List _list = [];
  517. for (var item in gameList) {
  518. _list.add(item['gameName']);
  519. }
  520. print(json.encode(_list));
  521. String PickerData = json.encode(_list);
  522. new Picker(
  523. confirmText: '确定',
  524. cancelText: "取消",
  525. adapter: PickerDataAdapter<String>(
  526. pickerdata: new JsonDecoder().convert(PickerData)),
  527. changeToFirst: true,
  528. textAlign: TextAlign.left,
  529. columnPadding: const EdgeInsets.all(8.0),
  530. onConfirm: (Picker picker, List value) {
  531. setState(() {
  532. editRoomInfo['gameId'] = gameList[value[0]]['id'];
  533. });
  534. }).showModal(this.context);
  535. }
  536. showNumPicker(Map info) {
  537. List _list = [];
  538. for (var i = 10; i <= 100; i = i + 10) {
  539. _list.add(i);
  540. }
  541. String PickerData = json.encode(_list);
  542. new Picker(
  543. confirmText: '确定',
  544. cancelText: "取消",
  545. adapter: PickerDataAdapter<String>(
  546. pickerdata: new JsonDecoder().convert(PickerData)),
  547. changeToFirst: true,
  548. textAlign: TextAlign.left,
  549. columnPadding: const EdgeInsets.all(8.0),
  550. onConfirm: (Picker picker, List value) {
  551. print(value);
  552. setState(() {
  553. editRoomInfo['maxNumber'] = _list[value[0]];
  554. });
  555. }).showModal(this.context);
  556. }
  557. showLevelPicker(BuildContext context) {
  558. List _list = [];
  559. for (var item in levelList) {
  560. _list.add(item['levelName']);
  561. }
  562. print(json.encode(_list));
  563. String PickerData = json.encode(_list);
  564. new Picker(
  565. confirmText: '确定',
  566. cancelText: "取消",
  567. adapter: PickerDataAdapter<String>(
  568. pickerdata: new JsonDecoder().convert(PickerData)),
  569. changeToFirst: true,
  570. textAlign: TextAlign.left,
  571. columnPadding: const EdgeInsets.all(8.0),
  572. onConfirm: (Picker picker, List value) {
  573. setState(() {
  574. editRoomInfo['houseLevel'] = levelList[value[0]]['id'];
  575. });
  576. }).showModal(this.context);
  577. }
  578. }