BindGame.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import 'package:flutter/material.dart';
  2. import '../styles/colors.dart';
  3. import 'dart:ui';
  4. import '../styles/totast.dart';
  5. import 'package:flutter_redux/flutter_redux.dart';
  6. import '../redux/AppState.dart';
  7. import '../net/HttpManager.dart';
  8. import '../net/Result.dart';
  9. import '../model/BindGameInfo.dart';
  10. class BindGame extends StatefulWidget {
  11. @override
  12. BindGameState createState() => BindGameState();
  13. }
  14. class BindGameState extends State<BindGame> {
  15. String bindName = '';
  16. bool isBind = false;
  17. BindGameInfo bindInfo;
  18. void getBindInfo() async {
  19. Toast.show(context, '加载中', -1, 'loading');
  20. Result res = await HttpManager.get("bindGame/all", data: {
  21. "gameId": 1,
  22. "userId": StoreProvider.of<AppState>(context).state.userInfo.id
  23. });
  24. Toast.hide();
  25. if (res.success) {
  26. if (res.data.length > 0) {
  27. print(res.data[0]);
  28. setState(() {
  29. isBind = true;
  30. bindInfo = BindGameInfo.fromJson(res.data[0]);
  31. });
  32. } else {
  33. setState(() {
  34. isBind = false;
  35. });
  36. }
  37. }
  38. }
  39. @override
  40. void initState() {
  41. super.initState();
  42. Future.delayed(Duration(milliseconds: 50), () {
  43. getBindInfo();
  44. });
  45. }
  46. void bindEvent() async {
  47. if (bindName == '') {
  48. Toast.show(context, '请输入绑定角色名称', 1500, 'info');
  49. return;
  50. }
  51. Toast.show(context, '加载中', -1, 'loading');
  52. Result res = await HttpManager.post("bindGame/save", data: {
  53. "gameId": 1,
  54. "userId": StoreProvider.of<AppState>(context).state.userInfo.id,
  55. "nickName": bindName
  56. });
  57. Toast.hide();
  58. if (res.success) {
  59. Toast.show(context, '绑定成功', 1000, 'success');
  60. Future.delayed(Duration(milliseconds: 1000), () {
  61. getBindInfo();
  62. });
  63. } else {}
  64. }
  65. void cancelBind() async {
  66. Toast.show(context, '加载中', -1, 'loading');
  67. Result res =
  68. await HttpManager.post("bindGame/del", data: {'id': bindInfo.id});
  69. Toast.hide();
  70. if (res.success) {
  71. Toast.show(context, '解绑成功', 1000, 'success');
  72. Future.delayed(Duration(milliseconds: 1000), () {
  73. getBindInfo();
  74. });
  75. } else {}
  76. }
  77. @override
  78. Widget build(BuildContext context) {
  79. return new Scaffold(
  80. appBar: AppBar(
  81. backgroundColor: PRIMARY_COLOR,
  82. title: Text('绑定账号'),
  83. centerTitle: true,
  84. elevation: 0,
  85. ),
  86. body: Container(
  87. color: BG_SUB_COLOR,
  88. padding: EdgeInsets.all(15),
  89. child: Column(
  90. children: <Widget>[
  91. Container(
  92. height: 220,
  93. width: double.infinity,
  94. // padding: EdgeInsets.symmetric(vertical: 15, horizontal: 34),
  95. decoration: BoxDecoration(
  96. image: DecorationImage(
  97. image: AssetImage('images/img_chijizhanchang.png'),
  98. fit: BoxFit.cover)),
  99. child: Column(
  100. mainAxisAlignment: MainAxisAlignment.end,
  101. children: _bindWidegt(),
  102. ),
  103. )
  104. ],
  105. ),
  106. ));
  107. }
  108. List<Widget> _bindWidegt() {
  109. List<Widget> widgetList = [];
  110. if (isBind) {
  111. widgetList.add(Container(
  112. height: 78,
  113. padding: EdgeInsets.all(15),
  114. decoration: BoxDecoration(
  115. gradient: LinearGradient(
  116. colors: [Color(0xFF464B6A), Color(0xFF35395E)],
  117. begin: Alignment.topCenter,
  118. end: Alignment.bottomCenter)),
  119. child: Row(
  120. children: <Widget>[
  121. Image.network(bindInfo.gameInfo.icon, width: 48),
  122. Container(
  123. width: 10,
  124. ),
  125. Expanded(
  126. flex: 1,
  127. child: Column(
  128. mainAxisAlignment: MainAxisAlignment.center,
  129. crossAxisAlignment: CrossAxisAlignment.start,
  130. children: <Widget>[
  131. Text(bindInfo.gameInfo.gameName,
  132. style: TextStyle(color: Colors.white, fontSize: 14)),
  133. Container(
  134. height: 5,
  135. ),
  136. Row(
  137. children: <Widget>[
  138. Text(
  139. "已绑定",
  140. style: TextStyle(
  141. color: Color(0xFF727785),
  142. fontSize: 13,
  143. fontWeight: FontWeight.w500),
  144. ),
  145. Container(
  146. width: 10,
  147. ),
  148. Text(
  149. bindInfo.nickName,
  150. style: TextStyle(
  151. color: Colors.white,
  152. fontSize: 13,
  153. fontWeight: FontWeight.w500),
  154. )
  155. ],
  156. )
  157. ],
  158. )),
  159. Container(
  160. width: 50,
  161. height: 30,
  162. child: OutlineButton(
  163. padding: EdgeInsets.all(0),
  164. borderSide: BorderSide(color: Color(0xFFC2524D), width: 1),
  165. textColor: Color(0xFFC2524D),
  166. highlightedBorderColor: Color(0xFF9B4040),
  167. child: Text("解绑"),
  168. onPressed: () => cancelBind(),
  169. ))
  170. ],
  171. ),
  172. ));
  173. } else {
  174. widgetList = [
  175. Container(
  176. margin: EdgeInsets.only(bottom: 10, left: 45, right: 45),
  177. decoration: BoxDecoration(
  178. borderRadius: BorderRadius.all(Radius.circular(2)),
  179. color: Colors.white,
  180. ),
  181. height: 40,
  182. child: TextField(
  183. style: TextStyle(
  184. color: Color(0xFFAF4946), fontWeight: FontWeight.w500),
  185. textAlign: TextAlign.center,
  186. decoration: InputDecoration(
  187. hintText: '请输入刺激战场游戏昵称',
  188. border: InputBorder.none,
  189. hintStyle: TextStyle(fontSize: 12, color: Color(0xFF727785))),
  190. onChanged: (value) {
  191. setState(() {
  192. bindName = value;
  193. });
  194. },
  195. ),
  196. ),
  197. Container(
  198. margin: EdgeInsets.only(left: 45, right: 45, bottom: 15),
  199. width: double.infinity,
  200. height: 40,
  201. child: RaisedButton(
  202. textTheme: ButtonTextTheme.primary,
  203. child: Text('绑定角色'),
  204. onPressed: () {
  205. bindEvent();
  206. },
  207. ),
  208. )
  209. ];
  210. }
  211. return widgetList;
  212. }
  213. }