import 'dart:async'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:flutter/cupertino.dart'; import '../styles/colors.dart'; import 'dart:ui'; import '../styles/totast.dart'; import '../widget/ITextInput.dart'; import 'package:flutter_redux/flutter_redux.dart'; import '../net/HttpManager.dart'; import '../net/Result.dart'; import 'Home.dart'; import '../redux/AppState.dart'; import '../redux/UserRedux.dart'; import '../model/UserInfo.dart'; class LoginSecond extends StatefulWidget { LoginSecond({Key key, this.phone}) : super(key: key); final String phone; // 用来储存传递过来的值 @override LoginSecondState createState() => LoginSecondState(); } class LoginSecondState extends State { String _sessionID; bool isSend = false; //是否发送 int sendTime = 0; String inputCode; String useToken; Timer timer; @override void initState() { super.initState(); Future.delayed(Duration.zero, () { sendMsg(); }); } @override Widget build(BuildContext context) { return WillPopScope( child: Container( color: BG_SUB_COLOR, child: Scaffold( appBar: AppBar( backgroundColor: BG_SUB_COLOR, centerTitle: true, elevation: 0, ), body: Container( color: BG_SUB_COLOR, child: CustomScrollView( slivers: [ SliverToBoxAdapter( child: Container( color: BG_SUB_COLOR, padding: EdgeInsets.symmetric(vertical: 30, horizontal: 15), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( '验证码已发送至', style: TextStyle(color: Colors.white, fontSize: 28, fontWeight: FontWeight.bold), ), Container( margin: EdgeInsets.only(top: 45), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(widget.phone, style: TextStyle(color: Color(0xFFF15436), fontSize: 20, fontWeight: FontWeight.bold)), FlatButton( highlightColor: BG_SUB_COLOR, textColor: PRIMARY_COLOR, child: Text(isSend ? '($sendTime)秒' : '重新发送'), disabledTextColor: Color(0xFF8D8E9C), onPressed: isSend ? null : () { if (timer != null) { timer.cancel(); } sendMsg(); }, ) ], ), ), Container( margin: EdgeInsets.only(top: 10), child: ITextField( maxLength: 6, keyboardType: ITextInputType.number, prefixIcon: Container( padding: EdgeInsets.all(10), // color: PRIMARY_COLOR, child: Image.asset( 'images/list_icon_yanzhengma.png', width: 20, ), ), hintText: '输入短信验证码', hintStyle: TextStyle( fontSize: 16, color: Color(0xFF727785), ), textStyle: TextStyle(color: Colors.white), fieldCallBack: (content) { setState(() { inputCode = content; }); }, counterStyle: TextStyle(color: BG_SUB_COLOR, fontSize: 0), ), ), Container( margin: EdgeInsets.only(top: 63, bottom: 20), width: double.infinity, height: 48, child: FlatButton( textColor: Colors.white, color: PRIMARY_COLOR, highlightColor: Color(0xFF763434), child: Text('注册/登录'), onPressed: () async { if (inputCode.length != 6) { Toast.show(context, '请输入验证码', 1500, 'info'); } else { Toast.show(context, '加载中', -1, 'loading'); final Result res = await HttpManager.post('auth/loginSms', data: {'phone': widget.phone, 'code': inputCode, 'requireToken': true}); Toast.hide(); if (res.success) { if (timer != null) { timer.cancel(); } final prefs = await SharedPreferences.getInstance(); await prefs.setString('token', res.token); HttpManager.token = res.token; StoreProvider.of(context).dispatch(UpdateUserAction(UserInfo.fromJson(res.data))); Toast.show(context, '登录成功', 1500, 'success'); Navigator.pushReplacement( context, CupertinoPageRoute(builder: (context) => HomePage()), ); } else { Toast.show(context, res.error, 1500, 'info'); } } }, ), ) ], ), ), ) ], ), )), ), onWillPop: () { Toast.hide(); Navigator.pop(context); return Future.value(false); }); } Future sendMsg() async { Toast.show(context, '加载中', -1, 'loading'); final Result res = await HttpManager.post('userInfo/sendSms', data: {'mobile': widget.phone}); Toast.hide(); if (res.success) { Toast.show(context, '发送成功', 1500, 'success'); _sessionID = res.data; setState(() { isSend = true; }); sendTime = 60; timer = Timer.periodic(Duration(seconds: 1), (timer) { if (sendTime == 0) { setState(() { isSend = false; }); } else { setState(() { sendTime = sendTime - 1; }); } }); } else { Toast.show(context, res.error, 1500, 'info'); } } }