change_phoneno.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'package:eitc_erm_app/utils/Component.dart';
  5. import 'package:eitc_erm_app/utils/Constants.dart';
  6. import 'package:eitc_erm_app/utils/Utils.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter/services.dart';
  9. import 'package:fluttertoast/fluttertoast.dart';
  10. import 'package:http/http.dart' as http;
  11. import 'bean/normal_response.dart';
  12. import 'login.dart';
  13. class ChangePhoneNo extends StatefulWidget {
  14. @override
  15. ChangePhoneNoState createState() => ChangePhoneNoState();
  16. }
  17. class ChangePhoneNoState extends State<ChangePhoneNo> {
  18. final _formKey = GlobalKey<FormState>();
  19. String _phoneNumber = '';
  20. String _newPassword = '';
  21. bool _isLoading = false;
  22. bool isButtonEnable = true;
  23. int count = 60;
  24. late Timer timer;
  25. String buttonText = '发送验证码';
  26. TextEditingController mController = TextEditingController();
  27. TextEditingController newPhoneController = TextEditingController();
  28. void _buttonClickListen() {
  29. setState(() {
  30. if (isButtonEnable) {
  31. sendCaptchaCode();
  32. //当按钮可点击时
  33. isButtonEnable = false; //按钮状态标记
  34. _initTimer();
  35. return null; //返回null按钮禁止点击
  36. } else {
  37. //当按钮不可点击时
  38. // debugPrint('false');
  39. return null; //返回null按钮禁止点击
  40. }
  41. });
  42. }
  43. void _initTimer() {
  44. timer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {
  45. count--;
  46. setState(() {
  47. if (count == 0) {
  48. timer.cancel(); //倒计时结束取消定时器
  49. isButtonEnable = true; //按钮可点击
  50. count = 60; //重置时间
  51. buttonText = '发送验证码'; //重置按钮文本
  52. } else {
  53. buttonText = '重新发送($count)'; //更新文本内容
  54. }
  55. });
  56. });
  57. }
  58. @override
  59. void dispose() {
  60. timer?.cancel();
  61. super.dispose();
  62. }
  63. Future<void> _changePhoneNo(
  64. {required String phoneNumber}) async {
  65. if (!Utils.isChinaPhoneLegal(phoneNumber)) {
  66. Component.toast("请输入正确手机号码!", 0);
  67. return null;
  68. }
  69. var params = {
  70. 'phoneNumber': phoneNumber,
  71. 'captchaCode': mController.text,
  72. };
  73. print(phoneNumber + " " + mController.text);
  74. Map<String, String> headers = {
  75. 'token': '${Global.token}',
  76. };
  77. final response = await http
  78. .post(Uri.parse('${Global.BaseUrl}user/updatePhoneNumber'), headers: {
  79. HttpHeaders.contentTypeHeader: "application/json; charset=utf-8",
  80. 'token': '${Global.token}',
  81. }, body: jsonEncode(params));
  82. setState(() {
  83. _isLoading = true;
  84. });
  85. try {
  86. final jsonString = utf8.decode(response.bodyBytes);
  87. final jsonResponse = jsonDecode(jsonString);
  88. print(jsonString);
  89. NormalResponse mNormalResponse =
  90. new NormalResponse.fromJson(jsonResponse);
  91. if (mNormalResponse.code == Global.responseSuccessCode) {
  92. Component.toast("手机号修改成功!", 2);
  93. Navigator.of(context).pop();
  94. Navigator.pushReplacement(
  95. context,
  96. MaterialPageRoute(builder: (context) => Login()),
  97. );
  98. } else {
  99. Component.toast(mNormalResponse.msg.toString(), 0);
  100. }
  101. } catch (e) {
  102. Component.toast("异常: $e", 0);
  103. } finally {
  104. setState(() {
  105. _isLoading = false;
  106. });
  107. }
  108. }
  109. @override
  110. Widget build(BuildContext context) {
  111. return Scaffold(
  112. appBar: new AppBar(
  113. title: new Text('修改手机号',
  114. style: TextStyle(
  115. color: Colors.white,
  116. )),
  117. centerTitle: true,
  118. elevation: 0.5,
  119. backgroundColor: Global.StatusBarColor,
  120. leading: new IconButton(
  121. tooltip: '返回上一页',
  122. icon: const Icon(
  123. Icons.arrow_back_ios,
  124. color: Colors.white,
  125. ),
  126. onPressed: () {
  127. Navigator.of(context).pop();
  128. //_nextPage(-1);
  129. },
  130. ),
  131. ),
  132. body: Form(
  133. key: _formKey,
  134. child: Padding(
  135. padding: const EdgeInsets.all(20.0),
  136. child: Column(
  137. children: <Widget>[
  138. SizedBox(height: 30.0),
  139. TextFormField(
  140. enabled: false,
  141. decoration: InputDecoration(
  142. labelText: '手机号',
  143. ),
  144. initialValue: Global.loginPhoneNo,
  145. // obscureText: true,
  146. onSaved: (value) => _newPassword = value!,
  147. ),
  148. TextFormField(
  149. controller: newPhoneController,
  150. decoration: InputDecoration(labelText: '新登录手机号'),
  151. onSaved: (value) => _phoneNumber = value!,
  152. ),
  153. Row(
  154. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  155. crossAxisAlignment: CrossAxisAlignment.baseline,
  156. textBaseline: TextBaseline.alphabetic,
  157. children: <Widget>[
  158. Expanded(
  159. child: Padding(
  160. padding: EdgeInsets.only(left: 0, right: 15, top: 15),
  161. child: TextFormField(
  162. maxLines: 1,
  163. onSaved: (value) {},
  164. controller: mController,
  165. textAlign: TextAlign.left,
  166. inputFormatters: [
  167. FilteringTextInputFormatter.digitsOnly,
  168. LengthLimitingTextInputFormatter(6)
  169. ],
  170. decoration: const InputDecoration(
  171. hintText: ('请输入验证码'),
  172. contentPadding: EdgeInsets.only(top: -5, bottom: 0),
  173. alignLabelWithHint: true,
  174. // border:
  175. // OutlineInputBorder(borderSide: BorderSide.none),
  176. ),
  177. ),
  178. ),
  179. ),
  180. Container(
  181. padding: const EdgeInsets.all(0),
  182. width: 120,
  183. child: ElevatedButton(
  184. style: ButtonStyle(
  185. backgroundColor:
  186. MaterialStateProperty.resolveWith<Color>((states) {
  187. if (states.contains(MaterialState.disabled)) {
  188. return Colors.brown; // Disabled color
  189. }
  190. return Colors.blue; // Regular color
  191. }),
  192. foregroundColor: MaterialStateProperty.all(
  193. isButtonEnable
  194. ? Colors.white
  195. : Colors.black.withOpacity(0.2)),
  196. ),
  197. // backgroundColor : isButtonEnable
  198. // ? Color(0xff44c5fe)
  199. // : Colors.grey.withOpacity(0.1), //按钮的颜色
  200. // splashColor: isButtonEnable
  201. // ? Colors.white.withOpacity(0.1)
  202. // : Colors.transparent,
  203. // shape: StadiumBorder(side: BorderSide.none),
  204. onPressed: () {
  205. setState(() {
  206. _buttonClickListen();
  207. });
  208. },
  209. child: Text(
  210. buttonText,
  211. style: TextStyle(
  212. fontSize: 12,
  213. ),
  214. ),
  215. ),
  216. ),
  217. ],
  218. ),
  219. Text(
  220. '更换手机号后,将同步修改就诊人为本人的手机号码。',
  221. style: const TextStyle(fontSize: 15, color: Colors.grey),
  222. ),
  223. Container(
  224. width: double.infinity,
  225. height: 45,
  226. margin: EdgeInsets.only(top: 40, left: 10, right: 10),
  227. child: ElevatedButton(
  228. style: ButtonStyle(
  229. backgroundColor:
  230. MaterialStateProperty.resolveWith<Color>((states) {
  231. return Colors.blue; // Regular color
  232. }),
  233. ),
  234. child: _isLoading
  235. ? CircularProgressIndicator(
  236. valueColor:
  237. AlwaysStoppedAnimation<Color>(Colors.white),
  238. )
  239. : Text(
  240. '确定',
  241. style: TextStyle(color: Colors.white, fontSize: 15),
  242. ),
  243. onPressed: _isLoading
  244. ? null
  245. : () {
  246. if (_formKey.currentState!.validate()) {
  247. _formKey.currentState?.save();
  248. _changePhoneNo(
  249. phoneNumber: _phoneNumber,
  250. );
  251. }
  252. },
  253. ),
  254. ),
  255. ],
  256. ),
  257. ),
  258. ),
  259. );
  260. }
  261. String tmpPhone = "";
  262. String tmpCode = "";
  263. Future<String?> sendCaptchaCode() async {
  264. if (!Utils.isChinaPhoneLegal(newPhoneController.text)) {
  265. Component.toast("请输入正确手机号码!", 0);
  266. return null;
  267. }
  268. tmpPhone = newPhoneController.text;
  269. var params = {
  270. 'phoneNumber': newPhoneController.text,
  271. };
  272. var response = await http
  273. .post(Uri.parse(Global.BaseUrl + 'sendCaptchaCode'), body: params);
  274. if (response.statusCode == 200) {
  275. final jsonString = utf8.decode(response.bodyBytes);
  276. final jsonResponse = jsonDecode(jsonString);
  277. print(jsonResponse);
  278. NormalResponse mNormalResponse =
  279. new NormalResponse.fromJson(jsonResponse);
  280. if (mNormalResponse.code == Global.responseSuccessCode) {
  281. Component.toast("短信发送成功,请查收!", 2);
  282. tmpCode = mNormalResponse.data.toString();
  283. } else {
  284. Component.toast(mNormalResponse.msg.toString(), 0);
  285. }
  286. return response.toString();
  287. } else {
  288. Component.toast("出错了,请稍后再试!", 0);
  289. return null;
  290. }
  291. }
  292. }