reset_password.dart 13 KB

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