loginSecond.dart 8.2 KB

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