LoginSecond_old.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:shared_preferences/shared_preferences.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import '../styles/colors.dart';
  6. import 'dart:ui';
  7. import '../styles/totast.dart';
  8. import '../widget/ITextInput.dart';
  9. import 'package:flutter_redux/flutter_redux.dart';
  10. import '../net/HttpManager.dart';
  11. import '../net/Result.dart';
  12. import 'HomePage.dart';
  13. import '../redux/AppState.dart';
  14. import '../redux/UserRedux.dart';
  15. import '../model/UserInfo.dart';
  16. import '../widget/LinearButton.dart';
  17. class LoginSecond extends StatefulWidget {
  18. LoginSecond({Key key, this.phone,this.userId=0}) : super(key: key);
  19. final String phone; // 用来储存传递过来的值
  20. final int userId;
  21. @override
  22. LoginSecondState createState() => LoginSecondState();
  23. }
  24. class LoginSecondState extends State<LoginSecond> {
  25. String _sessionID;
  26. bool isSend = false; //是否发送
  27. int sendTime = 0;
  28. String inputCode;
  29. String useToken;
  30. Timer timer;
  31. @override
  32. void initState() {
  33. super.initState();
  34. Future.delayed(Duration.zero, () {
  35. // sendMsg();
  36. });
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return WillPopScope(
  41. child: Container(
  42. color: BG_SUB_COLOR,
  43. child: Scaffold(
  44. appBar: AppBar(
  45. // backgroundColor: BG_SUB_COLOR,
  46. centerTitle: true,
  47. elevation: 0,
  48. ),
  49. body: Container(
  50. color: BG_SUB_COLOR,
  51. child: CustomScrollView(
  52. slivers: <Widget>[
  53. SliverToBoxAdapter(
  54. child: Container(
  55. color: BG_SUB_COLOR,
  56. padding:
  57. EdgeInsets.symmetric(vertical: 30, horizontal: 15),
  58. child: Column(
  59. crossAxisAlignment: CrossAxisAlignment.start,
  60. children: <Widget>[
  61. Text(
  62. '验证码已发送至',
  63. style: TextStyle(
  64. color: Colors.white,
  65. fontSize: 28,
  66. fontWeight: FontWeight.w600),
  67. ),
  68. Container(
  69. margin: EdgeInsets.only(top: 45),
  70. child: Row(
  71. mainAxisAlignment:
  72. MainAxisAlignment.spaceBetween,
  73. children: <Widget>[
  74. Text(widget.phone,
  75. style: TextStyle(
  76. color: Theme.of(context).primaryColor,
  77. fontSize: 20,
  78. fontWeight: FontWeight.w500)),
  79. FlatButton(
  80. highlightColor: BG_SUB_COLOR,
  81. textColor: PRIMARY_COLOR,
  82. child:
  83. Text(isSend ? '($sendTime)秒' : '重新发送'),
  84. disabledTextColor: Color(0xFF8D8E9C),
  85. onPressed: isSend
  86. ? null
  87. : () {
  88. if (timer != null) {
  89. timer.cancel();
  90. }
  91. sendMsg();
  92. },
  93. )
  94. ],
  95. ),
  96. ),
  97. Container(
  98. margin: EdgeInsets.only(top: 10),
  99. child: ITextField(
  100. maxLength: 6,
  101. keyboardType: ITextInputType.number,
  102. prefixIcon: Container(
  103. padding: EdgeInsets.all(10),
  104. // color: PRIMARY_COLOR,
  105. child: Image.asset(
  106. 'images/list_icon_yanzhengma.png',
  107. width: 20,
  108. ),
  109. ),
  110. hintText: '输入短信验证码',
  111. hintStyle: TextStyle(
  112. fontSize: 16,
  113. color: Color(0xFF727785),
  114. ),
  115. textStyle: TextStyle(color: Colors.white),
  116. fieldCallBack: (content) {
  117. setState(() {
  118. inputCode = content;
  119. });
  120. },
  121. counterStyle:
  122. TextStyle(color: BG_SUB_COLOR, fontSize: 0),
  123. ),
  124. ),
  125. Container(height: 20,),
  126. LinearButton(
  127. btntext:'注册/登录',
  128. onTapHomeMenu: () async {
  129. if (_sessionID == null) {
  130. Toast.show(
  131. context, '请发送验证码', 1500, 'info');
  132. } else if (inputCode.length != 6) {
  133. Toast.show(
  134. context, '请输入验证码', 1500, 'info');
  135. } else {
  136. Toast.show(context, '加载中', -1, 'loading');
  137. final Result res = await HttpManager.post(
  138. 'auth/loginSms',
  139. data: {
  140. 'phone': widget.phone,
  141. 'code': inputCode,
  142. 'sessionId': _sessionID,
  143. 'requireToken': true
  144. });
  145. Toast.hide();
  146. if (res.success) {
  147. if (timer != null) {
  148. timer.cancel();
  149. }
  150. final prefs = await SharedPreferences
  151. .getInstance();
  152. await prefs.setString(
  153. 'token', res.token);
  154. HttpManager.token = res.token;
  155. StoreProvider.of<AppState>(context)
  156. .dispatch(UpdateUserAction(
  157. UserInfo.fromJson(res.data)));
  158. Toast.show(
  159. context, '登录成功', 1500, 'success');
  160. Navigator.pushReplacement(
  161. context,
  162. CupertinoPageRoute(
  163. builder: (context) => HomePage()),
  164. );
  165. } else {
  166. Toast.show(
  167. context, res.error, 1500, 'info');
  168. }
  169. }
  170. },
  171. ),
  172. ],
  173. ),
  174. ),
  175. )
  176. ],
  177. ),
  178. )),
  179. ),
  180. onWillPop: () {
  181. Toast.hide();
  182. Navigator.pop(context);
  183. return Future.value(false);
  184. });
  185. }
  186. Future<void> sendMsg() async {
  187. Toast.show(context, '加载中', -1, 'loading');
  188. final Result res =
  189. await HttpManager.get('rong/sendCode', data: {'phone': widget.phone});
  190. Toast.hide();
  191. if (res.success) {
  192. Toast.show(context, '发送成功', 1500, 'success');
  193. _sessionID = res.data;
  194. setState(() {
  195. isSend = true;
  196. });
  197. sendTime = 60;
  198. timer = Timer.periodic(Duration(seconds: 1), (timer) {
  199. if (sendTime == 0) {
  200. setState(() {
  201. isSend = false;
  202. });
  203. } else {
  204. setState(() {
  205. sendTime = sendTime - 1;
  206. });
  207. }
  208. });
  209. } else {
  210. Toast.show(context, res.error, 1500, 'info');
  211. }
  212. }
  213. }