BindGame.dart 6.6 KB

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