reset_password.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. import 'dart:async';
  2. import 'package:dartin/dartin.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:sport/pages/login/login_widget.dart';
  6. import 'package:sport/provider/login_info_model.dart';
  7. import 'package:sport/router/navigator_util.dart';
  8. import 'package:sport/utils/toast.dart';
  9. import 'package:sport/widgets/appbar.dart';
  10. import 'package:sport/widgets/button_primary.dart';
  11. import 'package:sport/widgets/code_input.dart';
  12. import 'package:sport/widgets/dialog/alert_dialog.dart';
  13. import 'package:sport/widgets/space.dart';
  14. class RestPasswordWidget extends StatefulWidget {
  15. final bool password;
  16. RestPasswordWidget(this.password);
  17. @override
  18. State<StatefulWidget> createState() {
  19. // TODO: implement createState
  20. return _RestPasswordWidgetState();
  21. }
  22. }
  23. class _RestPasswordWidgetState extends State<RestPasswordWidget> {
  24. int _progress = 0; // 流程 1 2 3
  25. DateTime _lastPressed;
  26. @override
  27. void initState() {
  28. // TODO: implement initState
  29. super.initState();
  30. }
  31. setProgress({int progress}) {
  32. print(progress);
  33. setState(() {
  34. progress != null ? _progress = progress : _progress += 1;
  35. });
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return WillPopScope(
  40. onWillPop: () async => _progress > 0
  41. ? showDialog(
  42. context: context,
  43. builder: (context) => CustomAlertDialog(
  44. title: '你真的要退出吗',
  45. ok: () {
  46. Navigator.of(context).pop(true);
  47. },
  48. ))
  49. : true,
  50. child: Scaffold(
  51. backgroundColor: Colors.white,
  52. body: CustomScrollView(slivers: <Widget>[
  53. buildSliverAppBar(context, "重置密码"),
  54. SliverToBoxAdapter(
  55. child: Container(
  56. color: Colors.white,
  57. width: double.infinity,
  58. alignment: Alignment.center,
  59. child: Center(
  60. child: Padding(
  61. padding: const EdgeInsets.only(top: 32.0),
  62. child: ConstrainedBox(
  63. constraints: BoxConstraints(maxWidth: 300),
  64. child: Column(
  65. children: <Widget>[
  66. ProgressWidget(widget.password, _progress, setProgress),
  67. ],
  68. )),
  69. ))),
  70. )
  71. ])),
  72. );
  73. }
  74. }
  75. class ProgressWidget extends StatefulWidget {
  76. final bool password;
  77. int _progress;
  78. Function callback;
  79. ProgressWidget(this.password, this._progress, this.callback);
  80. @override
  81. State<StatefulWidget> createState() {
  82. return _ProgressWidgetState();
  83. }
  84. }
  85. class _ProgressWidgetState extends State<ProgressWidget> {
  86. String _onePassword;
  87. String _twoPassword;
  88. String _phone;
  89. String _captcha;
  90. Timer _timer;
  91. int _seconds;
  92. bool _isShowAuthCode;
  93. LoginInfoModel _loginInfoModel = inject();
  94. String _oldPassword;
  95. String _newPassword;
  96. @override
  97. void initState() {
  98. super.initState();
  99. _seconds = widget._progress == 4 ? 60 : 3;
  100. _isShowAuthCode = false;
  101. startTimer();
  102. }
  103. void cancelTimer() {
  104. if (_timer != null) {
  105. _timer.cancel();
  106. _timer = null;
  107. }
  108. }
  109. void startTimer({int flag}) {
  110. //设置 1 秒回调一次
  111. cancelTimer();
  112. const period = const Duration(seconds: 1);
  113. _timer = Timer.periodic(period, (timer) {
  114. //更新界面
  115. setState(() {
  116. //秒数减一,因为一秒回调一次
  117. _seconds--;
  118. print(_seconds);
  119. if (_seconds <= 0) {
  120. //倒计时秒数为0,取消定时器
  121. cancelTimer();
  122. setState(() {
  123. _isShowAuthCode = true;
  124. });
  125. if (flag == 1) {
  126. NavigatorUtil.goLogin(context);
  127. }
  128. }
  129. });
  130. });
  131. }
  132. @override
  133. void dispose() {
  134. super.dispose();
  135. cancelTimer();
  136. }
  137. @override
  138. Widget build(BuildContext context) {
  139. Widget _progressWidet0 = Column(
  140. children: <Widget>[
  141. if (widget.password == true)
  142. PrimaryButton(
  143. content: '通过密码',
  144. callback: () => widget.callback(progress: 5),
  145. ),
  146. if (widget.password == true)
  147. Space(
  148. height: 30.0,
  149. ),
  150. PrimaryButton(
  151. content: '通过手机',
  152. callback: () => widget.callback(),
  153. ),
  154. ],
  155. );
  156. Widget _progressWidget1 = Column(
  157. children: <Widget>[
  158. Container(
  159. width: 300.0,
  160. alignment: Alignment.centerLeft,
  161. padding: EdgeInsets.only(bottom: 12.0, left: 10.0),
  162. child: Text(
  163. "请填写你要找回密码的手机号",
  164. style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: 14.0),
  165. ),
  166. ),
  167. Input(
  168. labelText: "请输入手机号",
  169. textInputType: TextInputType.phone,
  170. maxLength: 11,
  171. callBack: (value) {
  172. setState(() {
  173. _phone = value; // String类型看着要不要强转
  174. });
  175. },
  176. ),
  177. SizedBox(
  178. height: 12,
  179. ),
  180. PrimaryButton(
  181. content: "下一步",
  182. callback: () async {
  183. bool flag = await _loginInfoModel.getCaptcha(_phone);
  184. // bool flag =true;
  185. if (flag) {
  186. widget.callback();
  187. setState(() {
  188. _seconds = 60;
  189. _isShowAuthCode = false;
  190. });
  191. startTimer();
  192. }
  193. },
  194. ),
  195. ],
  196. );
  197. Widget _progressWidget2 = Column(
  198. children: <Widget>[
  199. Padding(
  200. padding: EdgeInsets.only(bottom: 32.0),
  201. child: Text(
  202. "正在向您帐号关联手机号码发送验证码",
  203. style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1), fontWeight: FontWeight.w400),
  204. ),
  205. ),
  206. Padding(
  207. padding: EdgeInsets.only(bottom: 32.0),
  208. child: Text(
  209. "验证码已发送至$_phone",
  210. style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1), fontWeight: FontWeight.w400),
  211. ),
  212. ),
  213. Padding(
  214. padding: EdgeInsets.symmetric(vertical: 16.0, horizontal: 70.0),
  215. // onSumbit 是输入完的回调 判断一手
  216. child: LcfarmCodeInput(
  217. codeLength: 4,
  218. onSubmit: (value) async {
  219. bool flag = await _loginInfoModel.verifyCaptch(_phone, value);
  220. setState(() {
  221. _captcha = value;
  222. });
  223. if (flag) {
  224. widget.callback();
  225. }
  226. },
  227. )),
  228. PrimaryButton(
  229. content: _isShowAuthCode == false ? "$_seconds秒后重新获取验证码" : "重新获取验证码",
  230. callback: _isShowAuthCode == false
  231. ? null
  232. : () async {
  233. if (_isShowAuthCode) {
  234. print("重新获取验证码");
  235. bool flag = await _loginInfoModel.getCaptcha(_phone);
  236. if (flag) {
  237. setState(() {
  238. _seconds = 60;
  239. _isShowAuthCode = false;
  240. });
  241. startTimer();
  242. }
  243. }
  244. },
  245. ),
  246. ],
  247. );
  248. Widget _progressWidget3 = Column(
  249. children: <Widget>[
  250. Container(
  251. height: 44.0,
  252. width: 300.0,
  253. margin: EdgeInsets.only(bottom: 17.0),
  254. child: Container(
  255. padding: EdgeInsets.only(left: 20.0),
  256. child: Row(
  257. mainAxisAlignment: MainAxisAlignment.start,
  258. children: <Widget>[
  259. Expanded(
  260. child: TextField(
  261. // focusNode: _phone,
  262. decoration: InputDecoration(
  263. border: InputBorder.none,
  264. labelText: "请输入新密码",
  265. floatingLabelBehavior: FloatingLabelBehavior.never,
  266. ),
  267. onChanged: (value) {
  268. setState(() {
  269. _onePassword = value;
  270. });
  271. },
  272. ),
  273. )
  274. ],
  275. ),
  276. decoration: BoxDecoration(border: Border.all(color: Color.fromRGBO(206, 206, 206, 1)), borderRadius: BorderRadius.all(Radius.circular(44.0))),
  277. )),
  278. Container(
  279. height: 44.0,
  280. width: 300.0,
  281. margin: EdgeInsets.only(bottom: 17.0),
  282. child: Container(
  283. padding: EdgeInsets.only(left: 20.0),
  284. child: Row(
  285. mainAxisAlignment: MainAxisAlignment.start,
  286. children: <Widget>[
  287. Expanded(
  288. child: TextField(
  289. // focusNode: _password,
  290. decoration: InputDecoration(
  291. border: InputBorder.none,
  292. labelText: "再次确认密码",
  293. floatingLabelBehavior: FloatingLabelBehavior.never,
  294. ),
  295. onChanged: (value) {
  296. setState(() {
  297. _twoPassword = value;
  298. });
  299. },
  300. ),
  301. )
  302. ],
  303. ),
  304. decoration: BoxDecoration(border: Border.all(color: Color.fromRGBO(206, 206, 206, 1)), borderRadius: BorderRadius.all(Radius.circular(44.0))),
  305. )),
  306. PrimaryButton(
  307. content: '完成',
  308. callback: () async {
  309. if (_twoPassword != _onePassword) {
  310. ToastUtil.show("两次输入的密码不一致");
  311. } else {
  312. bool flag = await _loginInfoModel.resetPasswordByCaptcha(_phone, _captcha, _onePassword);
  313. setState(() {
  314. _seconds = 3;
  315. startTimer(flag: 1);
  316. });
  317. if (flag) widget.callback();
  318. }
  319. },
  320. ),
  321. ],
  322. );
  323. Widget _progressWidget4 = Column(
  324. children: <Widget>[
  325. Padding(
  326. padding: EdgeInsets.only(bottom: 20.0),
  327. child: Image.asset(
  328. "lib/assets/img/pop_image_ok@.png",
  329. width: 80.0,
  330. height: 80.0,
  331. ),
  332. ),
  333. Padding(
  334. padding: EdgeInsets.only(bottom: 6.0),
  335. child: Text(
  336. "密码更换成功",
  337. style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1), fontSize: 25.0),
  338. ),
  339. ),
  340. Padding(
  341. padding: EdgeInsets.only(bottom: 36.0),
  342. child: Text(
  343. "$_seconds秒后自动跳转至登陆界面",
  344. style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: 12.0),
  345. ),
  346. ),
  347. PrimaryButton(
  348. content: '立即跳转',
  349. callback: () {
  350. NavigatorUtil.goLogin(context);
  351. },
  352. ),
  353. ],
  354. );
  355. Widget _progressWidget5 = Column(
  356. children: <Widget>[
  357. Input(
  358. labelText: "请输入旧密码",
  359. callBack: (value) {
  360. setState(() {
  361. _oldPassword = value; // String类型看着要不要强转
  362. });
  363. },
  364. obscureText: true,
  365. ),
  366. Input(
  367. labelText: "请输入新密码",
  368. callBack: (value) {
  369. setState(() {
  370. _newPassword = value; // String类型看着要不要强转
  371. });
  372. },
  373. obscureText: true,
  374. ),
  375. Input(
  376. labelText: "确认新密码",
  377. callBack: (value) {
  378. setState(() {
  379. _twoPassword = value; // String类型看着要不要强转
  380. });
  381. },
  382. obscureText: true,
  383. ),
  384. Space(
  385. height: 30.0,
  386. ),
  387. PrimaryButton(
  388. content: '确定',
  389. callback: () async {
  390. if (_twoPassword != _newPassword) {
  391. ToastUtil.show("两次输入的密码不一致");
  392. return;
  393. }
  394. bool flag = await _loginInfoModel.resetPasswordByOld(_oldPassword, _newPassword);
  395. if (flag) {
  396. setState(() {
  397. _seconds = 3;
  398. });
  399. startTimer(flag: 1);
  400. widget.callback(progress: 4);
  401. }
  402. },
  403. ),
  404. ],
  405. );
  406. List<Widget> _progressWidget = [_progressWidet0, _progressWidget1, _progressWidget2, _progressWidget3, _progressWidget4, _progressWidget5];
  407. // TODO: implement build
  408. return _progressWidget[widget._progress];
  409. }
  410. }