import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:eitc_erm_app/utils/Component.dart'; import 'package:eitc_erm_app/utils/Constants.dart'; import 'package:eitc_erm_app/utils/Utils.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:fluttertoast/fluttertoast.dart'; import 'package:http/http.dart' as http; import 'bean/normal_response.dart'; import 'login.dart'; class ChangePhoneNo extends StatefulWidget { @override ChangePhoneNoState createState() => ChangePhoneNoState(); } class ChangePhoneNoState extends State { final _formKey = GlobalKey(); String _phoneNumber = ''; String _newPassword = ''; bool _isLoading = false; bool isButtonEnable = true; int count = 60; late Timer timer; String buttonText = '发送验证码'; TextEditingController mController = TextEditingController(); TextEditingController newPhoneController = TextEditingController(); void _buttonClickListen() { setState(() { if (isButtonEnable) { sendCaptchaCode(); //当按钮可点击时 isButtonEnable = false; //按钮状态标记 _initTimer(); return null; //返回null按钮禁止点击 } else { //当按钮不可点击时 // debugPrint('false'); return null; //返回null按钮禁止点击 } }); } void _initTimer() { timer = Timer.periodic(const Duration(seconds: 1), (Timer timer) { count--; setState(() { if (count == 0) { timer.cancel(); //倒计时结束取消定时器 isButtonEnable = true; //按钮可点击 count = 60; //重置时间 buttonText = '发送验证码'; //重置按钮文本 } else { buttonText = '重新发送($count)'; //更新文本内容 } }); }); } @override void dispose() { timer?.cancel(); super.dispose(); } Future _changePhoneNo( {required String phoneNumber}) async { if (!Utils.isChinaPhoneLegal(phoneNumber)) { Component.toast("请输入正确手机号码!", 0); return null; } var params = { 'phoneNumber': phoneNumber, 'captchaCode': mController.text, }; print(phoneNumber + " " + mController.text); Map headers = { 'token': '${Global.token}', }; final response = await http .post(Uri.parse('${Global.BaseUrl}user/updatePhoneNumber'), headers: { HttpHeaders.contentTypeHeader: "application/json; charset=utf-8", 'token': '${Global.token}', }, body: jsonEncode(params)); setState(() { _isLoading = true; }); try { final jsonString = utf8.decode(response.bodyBytes); final jsonResponse = jsonDecode(jsonString); print(jsonString); NormalResponse mNormalResponse = new NormalResponse.fromJson(jsonResponse); if (mNormalResponse.code == Global.responseSuccessCode) { Component.toast("手机号修改成功!", 2); Navigator.of(context).pop(); Navigator.pushReplacement( context, MaterialPageRoute(builder: (context) => Login()), ); } else { Component.toast(mNormalResponse.msg.toString(), 0); } } catch (e) { Component.toast("异常: $e", 0); } finally { setState(() { _isLoading = false; }); } } @override Widget build(BuildContext context) { return Scaffold( appBar: new AppBar( title: new Text('修改手机号', style: TextStyle( color: Colors.white, )), centerTitle: true, elevation: 0.5, backgroundColor: Global.StatusBarColor, leading: new IconButton( tooltip: '返回上一页', icon: const Icon( Icons.arrow_back_ios, color: Colors.white, ), onPressed: () { Navigator.of(context).pop(); //_nextPage(-1); }, ), ), body: Form( key: _formKey, child: Padding( padding: const EdgeInsets.all(20.0), child: Column( children: [ SizedBox(height: 30.0), TextFormField( enabled: false, decoration: InputDecoration( labelText: '手机号', ), initialValue: Global.loginPhoneNo, // obscureText: true, onSaved: (value) => _newPassword = value!, ), TextFormField( controller: newPhoneController, decoration: InputDecoration(labelText: '新登录手机号'), onSaved: (value) => _phoneNumber = value!, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.baseline, textBaseline: TextBaseline.alphabetic, children: [ Expanded( child: Padding( padding: EdgeInsets.only(left: 0, right: 15, top: 15), child: TextFormField( maxLines: 1, onSaved: (value) {}, controller: mController, textAlign: TextAlign.left, inputFormatters: [ FilteringTextInputFormatter.digitsOnly, LengthLimitingTextInputFormatter(6) ], decoration: const InputDecoration( hintText: ('请输入验证码'), contentPadding: EdgeInsets.only(top: -5, bottom: 0), alignLabelWithHint: true, // border: // OutlineInputBorder(borderSide: BorderSide.none), ), ), ), ), Container( padding: const EdgeInsets.all(0), width: 120, child: ElevatedButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith((states) { if (states.contains(MaterialState.disabled)) { return Colors.brown; // Disabled color } return Colors.blue; // Regular color }), foregroundColor: MaterialStateProperty.all( isButtonEnable ? Colors.white : Colors.black.withOpacity(0.2)), ), // backgroundColor : isButtonEnable // ? Color(0xff44c5fe) // : Colors.grey.withOpacity(0.1), //按钮的颜色 // splashColor: isButtonEnable // ? Colors.white.withOpacity(0.1) // : Colors.transparent, // shape: StadiumBorder(side: BorderSide.none), onPressed: () { setState(() { _buttonClickListen(); }); }, child: Text( buttonText, style: TextStyle( fontSize: 12, ), ), ), ), ], ), Text( '更换手机号后,将同步修改就诊人为本人的手机号码。', style: const TextStyle(fontSize: 15, color: Colors.grey), ), Container( width: double.infinity, height: 45, margin: EdgeInsets.only(top: 40, left: 10, right: 10), child: ElevatedButton( style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith((states) { return Colors.blue; // Regular color }), ), child: _isLoading ? CircularProgressIndicator( valueColor: AlwaysStoppedAnimation(Colors.white), ) : Text( '确定', style: TextStyle(color: Colors.white, fontSize: 15), ), onPressed: _isLoading ? null : () { if (_formKey.currentState!.validate()) { _formKey.currentState?.save(); _changePhoneNo( phoneNumber: _phoneNumber, ); } }, ), ), ], ), ), ), ); } String tmpPhone = ""; String tmpCode = ""; Future sendCaptchaCode() async { if (!Utils.isChinaPhoneLegal(newPhoneController.text)) { Component.toast("请输入正确手机号码!", 0); return null; } tmpPhone = newPhoneController.text; var params = { 'phoneNumber': newPhoneController.text, }; var response = await http .post(Uri.parse(Global.BaseUrl + 'sendCaptchaCode'), body: params); if (response.statusCode == 200) { final jsonString = utf8.decode(response.bodyBytes); final jsonResponse = jsonDecode(jsonString); print(jsonResponse); NormalResponse mNormalResponse = new NormalResponse.fromJson(jsonResponse); if (mNormalResponse.code == Global.responseSuccessCode) { Component.toast("短信发送成功,请查收!", 2); tmpCode = mNormalResponse.data.toString(); } else { Component.toast(mNormalResponse.msg.toString(), 0); } return response.toString(); } else { Component.toast("出错了,请稍后再试!", 0); return null; } } }