Kaynağa Gözat

创建房间

panhui 6 yıl önce
ebeveyn
işleme
822fa12b6b
3 değiştirilmiş dosya ile 506 ekleme ve 110 silme
  1. 495 102
      lib/pages/openRoom.dart
  2. 10 4
      lib/styles/netUtil.dart
  3. 1 4
      lib/styles/totast.dart

+ 495 - 102
lib/pages/openRoom.dart

@@ -1,15 +1,18 @@
 import 'package:flutter/material.dart';
 import 'package:flutter_picker/flutter_picker.dart';
+import 'package:flutter/cupertino.dart';
 import 'package:file_picker/file_picker.dart';
 import '../styles/colors.dart';
 import '../styles/netUtil.dart';
-import '../styles/totast.dart';
 import 'dart:io';
 import 'dart:async';
 import 'dart:convert';
 import 'dart:ui';
-import '../styles/api.dart';
 import 'package:dio/dio.dart';
+import '../styles/totast.dart';
+import '../pages/roomInfo.dart';
+import 'package:flutter_redux/flutter_redux.dart';
+import '../state.dart';
 
 class OpenRoom extends StatefulWidget {
   OpenRoom({Key key, this.roomFlag}) : super(key: key);
@@ -23,28 +26,75 @@ class OpenRoomState extends State<OpenRoom> {
   TextStyle valStyle =
       TextStyle(color: Colors.white, fontSize: 15, fontWeight: FontWeight.w500);
 
-  Map editRoomInfo = {
+  Map<String, dynamic> editRoomInfo = {
     "gameId": 1,
+    "houseLevel": 1,
+    "playerNumber": 0
   };
   List gameList = [];
+  List levelList = [];
 
   void getFilePath() async {
     String filePath = await FilePicker.getFilePath(type: FileType.ANY);
     File _file = File(filePath);
 
-    // final response =
-    //     await Dio().post(domain + 'assets/uploadFile', data: _formData);
-    // print(response);
+    Toast.show(context, '加载中', -1, 'loading');
     NetUtil.post(
         "assets/uploadFile",
         (data) {
-          print(data);
-          if (data['success']) {}
+          Toast.hide();
+          if (data['success']) {
+            setState(() {
+              editRoomInfo['video'] = data['data'];
+            });
+          }
         },
         params: {
           "file": UploadFileInfo(_file, filePath),
         },
         errorCallBack: (errorMsg) {
+          Toast.hide();
+          print("error:" + errorMsg);
+        });
+  }
+
+  void saveInfo() async {
+    if (!editRoomInfo.containsKey('gameId')) {
+      Toast.show(context, '请选择游戏', 1500, 'info');
+      return;
+    }
+    if (editRoomInfo['houseName'] == null || editRoomInfo['houseName'] == '') {
+      Toast.show(context, '请输入房间标题', 1500, 'info');
+      return;
+    }
+    if (!editRoomInfo.containsKey('houseLevel')) {
+      Toast.show(context, '请选择房间等级', 1500, 'info');
+      return;
+    }
+    print(editRoomInfo);
+    return;
+    Toast.show(context, '加载中', -1, 'loading');
+    NetUtil.post(
+        "houseInfo/save",
+        (data) {
+          Toast.hide();
+          if (data['success']) {
+            Future.delayed(Duration(milliseconds: 100), () {
+              Toast.show(context, '创建成功', 1500, 'success');
+            });
+            Future.delayed(Duration(milliseconds: 1500), () {
+              Navigator.pushAndRemoveUntil(
+                  context,
+                  new CupertinoPageRoute(
+                      builder: (context) =>
+                          new RoomInfo(roomId: data['data'].toString())),
+                  ModalRoute.withName('/'));
+            });
+          }
+        },
+        params: editRoomInfo,
+        errorCallBack: (errorMsg) {
+          Toast.hide();
           print("error:" + errorMsg);
         });
   }
@@ -55,7 +105,6 @@ class OpenRoomState extends State<OpenRoom> {
     super.initState();
     //获取游戏列表
     NetUtil.get("gameInfo/all", (data) {
-      print(data);
       if (data['success']) {
         setState(() {
           gameList = data['data'];
@@ -68,6 +117,29 @@ class OpenRoomState extends State<OpenRoom> {
     }, errorCallBack: (errorMsg) {
       print("error:" + errorMsg);
     });
+
+    NetUtil.get("houseLevel/all", (data) {
+      if (data['success']) {
+        levelList = data['data'];
+        setState(() {
+          if (levelList.length > 0) {
+            editRoomInfo['houseLevel'] = data['data'][0]['id'];
+            editRoomInfo['playerNumber'] = data['data'][0]['minNumber'];
+          }
+        });
+      }
+    }, errorCallBack: (errorMsg) {
+      print("error:" + errorMsg);
+    });
+  }
+
+  @override
+  void didChangeDependencies() {
+    // TODO: implement didChangeDependencies
+    super.didChangeDependencies();
+    editRoomInfo['userId'] =
+        StoreProvider.of<CountState>(context).state.userInfo['id'];
+    editRoomInfo['houseType'] = widget.roomFlag;
   }
 
   @override
@@ -79,112 +151,385 @@ class OpenRoomState extends State<OpenRoom> {
         chooseGameInfo = item;
       }
     }
+
+    Map chooseLevelInfo = {};
+    for (var item in levelList) {
+      if (item['id'].toString() == editRoomInfo['houseLevel'].toString()) {
+        chooseLevelInfo = item;
+      }
+    }
+
     // TODO: implement build
-    return new Scaffold(
-        appBar: AppBar(
-          backgroundColor: PRIMARY_COLOR,
-          title: Text('创建' + (widget.roomFlag == '0' ? '普通' : '官方') + '房间'),
-          centerTitle: true,
-          elevation: 0,
-          actions: <Widget>[
-            Container(
-              width: 60,
-              child: FlatButton(
-                highlightColor: PRIMARY_COLOR,
-                padding: EdgeInsets.only(right: 0),
-                child: Text('规则',
-                    style: TextStyle(color: Colors.white, fontSize: 13)),
-                onPressed: () {},
-              ),
-            )
-          ],
-        ),
-        body: Container(
-          color: BG_SUB_COLOR,
-          width: double.infinity,
-          height: double.infinity,
-          child: SingleChildScrollView(
-            child: Container(
-              color: BG_SUB_COLOR,
-              child: Column(
-                children: <Widget>[
-                  Container(
-                      padding: EdgeInsets.symmetric(horizontal: 15),
+    return WillPopScope(
+      child: Scaffold(
+          appBar: AppBar(
+            backgroundColor: PRIMARY_COLOR,
+            title: Text('创建' + (widget.roomFlag == '0' ? '普通' : '官方') + '房间'),
+            centerTitle: true,
+            elevation: 0,
+            actions: <Widget>[
+              Container(
+                width: 60,
+                child: FlatButton(
+                  highlightColor: PRIMARY_COLOR,
+                  padding: EdgeInsets.only(right: 0),
+                  child: Text('规则',
+                      style: TextStyle(color: Colors.white, fontSize: 13)),
+                  onPressed: () {},
+                ),
+              )
+            ],
+          ),
+          body: Container(
+            color: BG_COLOR,
+            width: double.infinity,
+            height: double.infinity,
+            child: SingleChildScrollView(
+              child: Container(
+                color: BG_COLOR,
+                child: Column(
+                  children: <Widget>[
+                    Container(
+                        padding: EdgeInsets.symmetric(horizontal: 15),
+                        color: BG_COLOR,
+                        height: 60,
+                        child: GestureDetector(
+                          child: Row(
+                            children: <Widget>[
+                              Text('选择游戏', style: titleStyle),
+                              Expanded(
+                                flex: 1,
+                                child: Text(
+                                  chooseGameInfo.containsKey('gameName')
+                                      ? chooseGameInfo['gameName']
+                                      : '',
+                                  textAlign: TextAlign.right,
+                                  style: valStyle,
+                                ),
+                              ),
+                              Image.asset('images/icon_inter.png', width: 24)
+                            ],
+                          ),
+                          onTap: () {
+                            showPicker(context);
+                          },
+                        )),
+                    Container(
+                      width: double.infinity,
+                      color: BG_SUB_COLOR,
+                      height: 210,
+                      child: Column(
+                        mainAxisAlignment: MainAxisAlignment.center,
+                        children: <Widget>[
+                          FlatButton(
+                            padding: EdgeInsets.all(0),
+                            color: Color(0xFF464B6A),
+                            highlightColor: Color(0xFF333557),
+                            child: Container(
+                              height: 38,
+                              width: 160,
+                              decoration: BoxDecoration(
+                                  gradient: LinearGradient(
+                                begin: Alignment.topRight,
+                                colors: [Colors.black12, Colors.black38],
+                              )),
+                              child: Row(
+                                mainAxisAlignment: MainAxisAlignment.center,
+                                children: <Widget>[
+                                  Padding(
+                                      padding: EdgeInsets.only(right: 8),
+                                      child: Image.asset(
+                                        'images/icon_shipin.png',
+                                        width: 20,
+                                      )),
+                                  Text('上传图片或视频', style: titleStyle)
+                                ],
+                              ),
+                            ),
+                            onPressed: () {
+                              getFilePath();
+                            },
+                          ),
+                          Padding(
+                            padding: EdgeInsets.only(top: 12),
+                            child: Text(
+                              editRoomInfo.containsKey('video')
+                                  ? '已选择'
+                                  : '不上传则自动使用官方默认视频',
+                              style: TextStyle(
+                                  color: Color(0xFF9BA0AE), fontSize: 13),
+                            ),
+                          )
+                        ],
+                      ),
+                    ),
+                    //房间标题
+                    Container(
+                      height: 60,
                       color: BG_COLOR,
+                      padding:
+                          EdgeInsets.symmetric(horizontal: 15, vertical: 8),
+                      child: TextField(
+                        textAlign: TextAlign.end,
+                        style: valStyle,
+                        maxLength: 10,
+                        decoration: InputDecoration(
+                            hintText: "请输入房间标题",
+                            hintStyle: TextStyle(
+                                color: Color(0xFF727785), fontSize: 13),
+                            prefixIcon: Padding(
+                              padding: EdgeInsets.symmetric(vertical: 12),
+                              child: Text('房间标题', style: titleStyle),
+                            ),
+                            border: InputBorder.none,
+                            counterStyle: TextStyle(fontSize: 0)),
+                        onChanged: (value) {
+                          editRoomInfo['houseName'] = value;
+                        },
+                      ),
+                    ),
+                    Container(
+                      height: 1,
+                      color: BG_SUB_COLOR,
+                    ),
+                    //房间简介
+                    Container(
                       height: 60,
-                      child: GestureDetector(
+                      color: BG_COLOR,
+                      padding:
+                          EdgeInsets.symmetric(horizontal: 15, vertical: 8),
+                      child: TextField(
+                        textAlign: TextAlign.end,
+                        style: valStyle,
+                        maxLength: 15,
+                        decoration: InputDecoration(
+                            hintText: "请输入房间简介",
+                            hintStyle: TextStyle(
+                                color: Color(0xFF727785), fontSize: 13),
+                            prefixIcon: Padding(
+                              padding: EdgeInsets.symmetric(vertical: 12),
+                              child: Text('房间简介', style: titleStyle),
+                            ),
+                            border: InputBorder.none,
+                            counterStyle: TextStyle(fontSize: 0)),
+                        onChanged: (value) {
+                          editRoomInfo['houseAbstract'] = value;
+                        },
+                      ),
+                    ),
+                    Container(
+                      height: 1,
+                      color: BG_SUB_COLOR,
+                    ),
+                    //房间等级
+                    Container(
+                        height: 60,
+                        color: BG_COLOR,
+                        padding: EdgeInsets.symmetric(horizontal: 15),
+                        child: GestureDetector(
+                          child: Row(
+                            children: <Widget>[
+                              Text(
+                                '房间等级',
+                                style: titleStyle,
+                              ),
+                              Expanded(
+                                flex: 1,
+                                child: Row(
+                                  mainAxisAlignment: MainAxisAlignment.end,
+                                  children: <Widget>[
+                                    Image.network(
+                                      chooseLevelInfo.containsKey('icon')
+                                          ? chooseLevelInfo['icon']
+                                          : '',
+                                      width: 22,
+                                    ),
+                                    Text(
+                                      chooseLevelInfo.containsKey('levelName')
+                                          ? chooseLevelInfo['levelName']
+                                          : '',
+                                      style:
+                                          TextStyle(color: Color(0xFFF9D881)),
+                                    ),
+                                    Padding(
+                                      padding:
+                                          EdgeInsets.only(left: 20, right: 2),
+                                      child: Image.asset(
+                                          'images/icon_jinbi_da_bai.png',
+                                          width: 20),
+                                    ),
+                                    Text(
+                                      'X' +
+                                          (chooseLevelInfo
+                                                  .containsKey('entryCoin')
+                                              ? chooseLevelInfo['entryCoin']
+                                                  .toString()
+                                              : ''),
+                                      style: TextStyle(
+                                          color: Colors.white,
+                                          fontSize: 15,
+                                          fontWeight: FontWeight.w500),
+                                    )
+                                  ],
+                                ),
+                              ),
+                              Padding(
+                                  padding: EdgeInsets.only(left: 4),
+                                  child: Image.asset('images/icon_inter.png',
+                                      width: 24))
+                            ],
+                          ),
+                          onTap: () {
+                            showLevelPicker(context);
+                          },
+                        )),
+                    Container(
+                      height: 1,
+                      color: BG_SUB_COLOR,
+                    ),
+                    //房间人数
+                    Container(
+                        padding: EdgeInsets.symmetric(horizontal: 15),
+                        color: BG_COLOR,
+                        height: 60,
+                        child: GestureDetector(
+                          child: Row(
+                            children: <Widget>[
+                              Text('房间人数', style: titleStyle),
+                              Expanded(
+                                flex: 1,
+                                child: Text(
+                                  editRoomInfo['playerNumber'].toString(),
+                                  textAlign: TextAlign.right,
+                                  style: valStyle,
+                                ),
+                              ),
+                              Image.asset('images/icon_inter.png', width: 24)
+                            ],
+                          ),
+                          onTap: () {
+                            print('111111');
+                            showNumPicker(chooseLevelInfo);
+                          },
+                        )),
+                    _bottomWidget(),
+                    //创建房间
+                    Container(
+                      padding: EdgeInsets.fromLTRB(15, 39, 15, 28),
+                      height: 109,
+                      color: BG_COLOR,
+                      child: FlatButton(
+                        color: PRIMARY_COLOR,
                         child: Row(
+                          mainAxisAlignment: MainAxisAlignment.center,
                           children: <Widget>[
-                            Text('选择游戏', style: titleStyle),
-                            Expanded(
-                              flex: 1,
+                            Padding(
+                              padding: EdgeInsets.only(right: 6),
+                              child: Image.asset('images/icon_jinbi_da_bai.png',
+                                  width: 20),
+                            ),
+                            Text(
+                              'X' +
+                                  (chooseLevelInfo.containsKey('entryCoin')
+                                      ? chooseLevelInfo['entryCoin'].toString()
+                                      : ''),
+                              style: TextStyle(
+                                  color: Colors.white,
+                                  fontSize: 16,
+                                  fontWeight: FontWeight.w500),
+                            ),
+                            Padding(
+                              padding: EdgeInsets.only(left: 20),
                               child: Text(
-                                chooseGameInfo.containsKey('gameName')
-                                    ? chooseGameInfo['gameName']
-                                    : '',
-                                textAlign: TextAlign.right,
-                                style: valStyle,
+                                '创建房间',
+                                style: TextStyle(
+                                    color: Colors.white,
+                                    fontSize: 16,
+                                    fontWeight: FontWeight.w500),
                               ),
-                            ),
-                            Image.asset('images/icon_inter.png', width: 24)
+                            )
                           ],
                         ),
-                        onTap: () {
-                          showPicker(context);
+                        onPressed: () {
+                          saveInfo();
                         },
-                      )),
-                  Container(
-                    width: double.infinity,
-                    color: BG_SUB_COLOR,
-                    height: 210,
-                    child: Column(
-                      mainAxisAlignment: MainAxisAlignment.center,
-                      children: <Widget>[
-                        FlatButton(
-                          padding: EdgeInsets.all(0),
-                          color: Color(0xFF464B6A),
-                          highlightColor: Color(0xFF333557),
-                          child: Container(
-                            height: 38,
-                            width: 160,
-                            decoration: BoxDecoration(
-                                gradient: LinearGradient(
-                              begin: Alignment.topRight,
-                              colors: [Colors.black12, Colors.black38],
-                            )),
-                            child: Row(
-                              mainAxisAlignment: MainAxisAlignment.center,
-                              children: <Widget>[
-                                Padding(
-                                    padding: EdgeInsets.only(right: 8),
-                                    child: Image.asset(
-                                      'images/icon_shipin.png',
-                                      width: 20,
-                                    )),
-                                Text('上传图片或视频', style: titleStyle)
-                              ],
-                            ),
-                          ),
-                          onPressed: () {
-                            getFilePath();
-                          },
-                        ),
-                        Padding(
-                          padding: EdgeInsets.only(top: 12),
-                          child: Text(
-                            '不上传则自动使用官方默认视频',
-                            style: TextStyle(
-                                color: Color(0xFF9BA0AE), fontSize: 13),
-                          ),
-                        )
-                      ],
-                    ),
-                  ),
-                ],
+                      ),
+                    )
+                  ],
+                ),
               ),
             ),
+          )),
+      onWillPop: () {
+        Toast.hide();
+        Navigator.pop(context);
+      },
+    );
+  }
+
+  Widget _bottomWidget() {
+    if (widget.roomFlag == '1')
+      return Column(
+        children: <Widget>[
+          Container(
+            height: 10,
+            color: BG_SUB_COLOR,
           ),
-        ));
+          //游戏房间号
+          Container(
+            height: 60,
+            color: BG_COLOR,
+            padding: EdgeInsets.symmetric(horizontal: 15, vertical: 8),
+            child: TextField(
+              textAlign: TextAlign.end,
+              style: valStyle,
+              maxLength: 15,
+              decoration: InputDecoration(
+                  hintText: "请输入游戏房间号",
+                  hintStyle: TextStyle(color: Color(0xFF727785), fontSize: 13),
+                  prefixIcon: Padding(
+                    padding: EdgeInsets.symmetric(vertical: 12),
+                    child: Text('游戏房间号', style: titleStyle),
+                  ),
+                  border: InputBorder.none,
+                  counterStyle: TextStyle(fontSize: 0)),
+              onChanged: (value) {
+                editRoomInfo['gameHouseId'] = value;
+              },
+            ),
+          ),
+          Container(
+            height: 1,
+            color: BG_SUB_COLOR,
+          ),
+          //游戏中房间密码
+          Container(
+            height: 60,
+            color: BG_COLOR,
+            padding: EdgeInsets.symmetric(horizontal: 15, vertical: 8),
+            child: TextField(
+              textAlign: TextAlign.end,
+              style: valStyle,
+              maxLength: 10,
+              decoration: InputDecoration(
+                  hintText: "请输入密码",
+                  hintStyle: TextStyle(color: Color(0xFF727785), fontSize: 13),
+                  prefixIcon: Padding(
+                    padding: EdgeInsets.symmetric(vertical: 12),
+                    child: Text('游戏中房间密码', style: titleStyle),
+                  ),
+                  border: InputBorder.none,
+                  counterStyle: TextStyle(fontSize: 0)),
+              onChanged: (value) {
+                editRoomInfo['gameHousePassword'] = value;
+              },
+            ),
+          ),
+        ],
+      );
+    else {
+      return Container();
+    }
   }
 
   showPicker(BuildContext context) {
@@ -208,4 +553,52 @@ class OpenRoomState extends State<OpenRoom> {
           });
         }).showModal(this.context);
   }
+
+  showNumPicker(Map info) {
+    List _list = [];
+    print(info['minNumber']);
+    print(info['maxNumber']);
+    for (var i = info['minNumber']; i <= info['maxNumber']; i = i + 5) {
+      _list.add(i);
+    }
+
+    String PickerData = json.encode(_list);
+    new Picker(
+        confirmText: '确定',
+        cancelText: "取消",
+        adapter: PickerDataAdapter<String>(
+            pickerdata: new JsonDecoder().convert(PickerData)),
+        changeToFirst: true,
+        textAlign: TextAlign.left,
+        columnPadding: const EdgeInsets.all(8.0),
+        onConfirm: (Picker picker, List value) {
+          print(value);
+          setState(() {
+            editRoomInfo['playerNumber'] = _list[value[0]];
+          });
+        }).showModal(this.context);
+  }
+
+  showLevelPicker(BuildContext context) {
+    List _list = [];
+    for (var item in levelList) {
+      _list.add(item['levelName']);
+    }
+    print(json.encode(_list));
+    String PickerData = json.encode(_list);
+    new Picker(
+        confirmText: '确定',
+        cancelText: "取消",
+        adapter: PickerDataAdapter<String>(
+            pickerdata: new JsonDecoder().convert(PickerData)),
+        changeToFirst: true,
+        textAlign: TextAlign.left,
+        columnPadding: const EdgeInsets.all(8.0),
+        onConfirm: (Picker picker, List value) {
+          setState(() {
+            editRoomInfo['houseLevel'] = levelList[value[0]]['id'];
+            editRoomInfo['playerNumber'] = levelList[value[0]]['minNumber'];
+          });
+        }).showModal(this.context);
+  }
 }

+ 10 - 4
lib/styles/netUtil.dart

@@ -1,6 +1,7 @@
 import 'package:dio/dio.dart';
 import 'dart:convert';
 import 'api.dart';
+import '../styles/totast.dart';
 import 'package:shared_preferences/shared_preferences.dart';
 
 class NetUtil {
@@ -9,7 +10,7 @@ class NetUtil {
 
   //get请求
   static void get(String url, Function callBack,
-      {Map params, Function errorCallBack}) async {
+      {Map<String, dynamic> params, Function errorCallBack}) async {
     if (!url.startsWith("http")) {
       url = domain + url;
     }
@@ -20,7 +21,8 @@ class NetUtil {
 
   //post请求
   static void post(String url, Function callBack,
-      {Map<String,dynamic> params, Function errorCallBack}) async {
+      {Map<String, dynamic> params, Function errorCallBack}) async {
+    print('**************');
     if (!url.startsWith("http")) {
       url = domain + url;
     }
@@ -31,7 +33,9 @@ class NetUtil {
   //具体的还是要看返回数据的基本结构
   //公共代码部分
   static void _request(String url, Function callBack,
-      {String method, Map<String,dynamic> params, Function errorCallBack}) async {
+      {String method,
+      Map<String, dynamic> params,
+      Function errorCallBack}) async {
     print("<net> url :<" + method + ">" + url);
 
     if (params != null && params.isNotEmpty) {
@@ -63,7 +67,6 @@ class NetUtil {
       } else {
         if (params != null && params.isNotEmpty) {
           FormData _formData = FormData.from(params);
-          print(url);
           response = await Dio().post(url,
               data: _formData,
               options: Options(headers: {"token": _electricContest}));
@@ -75,6 +78,9 @@ class NetUtil {
 
       statusCode = response.statusCode;
 
+      print(response);
+      //打印结果
+
       //处理错误部分
       if (statusCode < 0) {
         errorMsg = "网络请求错误,状态码:" + statusCode.toString();

+ 1 - 4
lib/styles/totast.dart

@@ -45,16 +45,13 @@ class Toast {
     toastView.overlayState = overlayState;
     preToast = toastView;
 
-
-    
     toastView._show();
   }
 
   static hide() {
-    print(preToast);
     if (preToast != null) {
       preToast.dismiss();
-      preToast = ToastView();
+      preToast = null;
     }
   }