LoginSecond.dart 9.0 KB

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