LoginSecond.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 'Home.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.bold),
  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.bold)),
  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 (inputCode.length != 6) {
  123. Toast.show(context, '请输入验证码', 1500, 'info');
  124. } else {
  125. Toast.show(context, '加载中', -1, 'loading');
  126. final Result res =
  127. await HttpManager.post('auth/loginSms', data: {'phone': widget.phone, 'code': inputCode, 'requireToken': true});
  128. Toast.hide();
  129. if (res.success) {
  130. if (timer != null) {
  131. timer.cancel();
  132. }
  133. final prefs = await SharedPreferences.getInstance();
  134. await prefs.setString('token', res.token);
  135. HttpManager.token = res.token;
  136. StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(UserInfo.fromJson(res.data)));
  137. Toast.show(context, '登录成功', 1500, 'success');
  138. Navigator.pushReplacement(
  139. context,
  140. CupertinoPageRoute(builder: (context) => HomePage()),
  141. );
  142. } else {
  143. Toast.show(context, res.error, 1500, 'info');
  144. }
  145. }
  146. },
  147. ),
  148. )
  149. ],
  150. ),
  151. ),
  152. )
  153. ],
  154. ),
  155. )),
  156. ),
  157. onWillPop: () {
  158. Toast.hide();
  159. Navigator.pop(context);
  160. return Future.value(false);
  161. });
  162. }
  163. Future<void> sendMsg() async {
  164. Toast.show(context, '加载中', -1, 'loading');
  165. final Result res = await HttpManager.post('userInfo/sendSms', data: {'mobile': widget.phone});
  166. Toast.hide();
  167. if (res.success) {
  168. Toast.show(context, '发送成功', 1500, 'success');
  169. _sessionID = res.data;
  170. setState(() {
  171. isSend = true;
  172. });
  173. sendTime = 60;
  174. timer = Timer.periodic(Duration(seconds: 1), (timer) {
  175. if (sendTime == 0) {
  176. setState(() {
  177. isSend = false;
  178. });
  179. } else {
  180. setState(() {
  181. sendTime = sendTime - 1;
  182. });
  183. }
  184. });
  185. } else {
  186. Toast.show(context, res.error, 1500, 'info');
  187. }
  188. }
  189. }