import 'package:flutter/material.dart'; import '../styles/colors.dart'; import 'dart:ui'; import '../styles/totast.dart'; import 'package:flutter_redux/flutter_redux.dart'; import '../redux/AppState.dart'; import '../net/HttpManager.dart'; import '../net/Result.dart'; import '../model/BindGameInfo.dart'; class BindGame extends StatefulWidget { @override BindGameState createState() => BindGameState(); } class BindGameState extends State { String bindName = ''; bool isBind = false; BindGameInfo bindInfo; void getBindInfo() async { Toast.show(context, '加载中', -1, 'loading'); Result res = await HttpManager.get("bindGame/all", data: { "gameId": 1, "userId": StoreProvider.of(context).state.userInfo.id }); Toast.hide(); if (res.success) { if (res.data.length > 0) { print(res.data[0]); setState(() { isBind = true; bindInfo = BindGameInfo.fromJson(res.data[0]); }); } else { setState(() { isBind = false; }); } } } @override void initState() { super.initState(); Future.delayed(Duration(milliseconds: 50), () { getBindInfo(); }); } void bindEvent() async { if (bindName == '') { Toast.show(context, '请输入绑定角色名称', 1500, 'info'); return; } Toast.show(context, '加载中', -1, 'loading'); Result res = await HttpManager.post("bindGame/save", data: { "gameId": 1, "userId": StoreProvider.of(context).state.userInfo.id, "nickName": bindName }); Toast.hide(); if (res.success) { Toast.show(context, '绑定成功', 1000, 'success'); Future.delayed(Duration(milliseconds: 1000), () { getBindInfo(); }); } else {} } void cancelBind() async { Toast.show(context, '加载中', -1, 'loading'); Result res = await HttpManager.post("bindGame/del", data: {'id': bindInfo.id}); Toast.hide(); if (res.success) { Toast.show(context, '解绑成功', 1000, 'success'); Future.delayed(Duration(milliseconds: 1000), () { getBindInfo(); }); } else {} } @override Widget build(BuildContext context) { return new Scaffold( appBar: AppBar( backgroundColor: PRIMARY_COLOR, title: Text('绑定账号'), centerTitle: true, elevation: 0, ), body: Container( color: BG_SUB_COLOR, padding: EdgeInsets.all(15), child: Column( children: [ Container( height: 220, width: double.infinity, // padding: EdgeInsets.symmetric(vertical: 15, horizontal: 34), decoration: BoxDecoration( image: DecorationImage( image: AssetImage('images/img_chijizhanchang.png'), fit: BoxFit.cover)), child: Column( mainAxisAlignment: MainAxisAlignment.end, children: _bindWidegt(), ), ) ], ), )); } List _bindWidegt() { List widgetList = []; if (isBind) { widgetList.add(Container( height: 78, padding: EdgeInsets.all(15), decoration: BoxDecoration( gradient: LinearGradient( colors: [Color(0xFF464B6A), Color(0xFF35395E)], begin: Alignment.topCenter, end: Alignment.bottomCenter)), child: Row( children: [ Image.network(bindInfo.gameInfo.icon, width: 48), Container( width: 10, ), Expanded( flex: 1, child: Column( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(bindInfo.gameInfo.gameName, style: TextStyle(color: Colors.white, fontSize: 14)), Container( height: 5, ), Row( children: [ Text( "已绑定", style: TextStyle( color: Color(0xFF727785), fontSize: 13, fontWeight: FontWeight.w500), ), Container( width: 10, ), Text( bindInfo.nickName, style: TextStyle( color: Colors.white, fontSize: 13, fontWeight: FontWeight.w500), ) ], ) ], )), Container( width: 50, height: 30, child: OutlineButton( padding: EdgeInsets.all(0), borderSide: BorderSide(color: Color(0xFFC2524D), width: 1), textColor: Color(0xFFC2524D), highlightedBorderColor: Color(0xFF9B4040), child: Text("解绑"), onPressed: () => cancelBind(), )) ], ), )); } else { widgetList = [ Container( margin: EdgeInsets.only(bottom: 10, left: 45, right: 45), decoration: BoxDecoration( borderRadius: BorderRadius.all(Radius.circular(2)), color: Colors.white, ), height: 40, child: TextField( style: TextStyle( color: Color(0xFFAF4946), fontWeight: FontWeight.w500), textAlign: TextAlign.center, decoration: InputDecoration( hintText: '请输入刺激战场游戏昵称', border: InputBorder.none, hintStyle: TextStyle(fontSize: 12, color: Color(0xFF727785))), onChanged: (value) { setState(() { bindName = value; }); }, ), ), Container( margin: EdgeInsets.only(left: 45, right: 45, bottom: 15), width: double.infinity, height: 40, child: RaisedButton( textTheme: ButtonTextTheme.primary, child: Text('绑定角色'), onPressed: () { bindEvent(); }, ), ) ]; } return widgetList; } }