123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- import 'dart:async';
- import 'dart:io';
- import 'package:eitc_erm_app/bean/normal_response.dart';
- import 'package:eitc_erm_app/utils/Component.dart';
- import 'package:eitc_erm_app/utils/Constants.dart';
- import 'package:eitc_erm_app/utils/logger.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:http/http.dart' as http;
- import 'package:http/http.dart';
- import 'login.dart';
- class ChangePasswordPage extends StatefulWidget {
- @override
- _ChangePasswordPageState createState() => _ChangePasswordPageState();
- }
- class _ChangePasswordPageState extends State<ChangePasswordPage> {
- final _formKey = GlobalKey<FormState>();
- String phoneNo = '';
- String _newPassword = '';
- String _confirmPassword = '';
- bool _isLoading = false;
- bool isButtonEnable = true;
- int count = 60;
- Timer? timer;
- String buttonText = '发送验证码';
- TextEditingController mController = TextEditingController();
- void _buttonClickListen() {
- setState(() {
- sendCaptchaCode();
- if (isButtonEnable) {
- //当按钮可点击时
- isButtonEnable = false; //按钮状态标记
- _initTimer();
- return null; //返回null按钮禁止点击
- } else {
- //当按钮不可点击时
- // logd('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<bool> _checkCaptchaCode() async {
- Map<String, dynamic> params = {
- "phoneNumber": Global.loginPhoneNo,
- "captchaCode": mController.text
- };
- Uri uri = Uri.parse("${Global.BaseUrl}api/checkCaptchaCode");
- logd("检查验证码,uri=$uri");
- Response response =
- await http.post(uri, body: encodeBody(params), headers: jsonHeaders());
- if (response.statusCode == 200) {
- final json = decodeBodyToJson(response.bodyBytes);
- logd("检查验证码结果$json");
- NormalResponse mNormalResponse = NormalResponse.fromJson(json);
- return mNormalResponse.code == 200;
- } else {
- logd("检查验证码异常${response.body}");
- }
- return false;
- }
- Future<void> _changePassword(
- {required String newPassword, required String phoneNo}) async {
- bool bo = await _checkCaptchaCode();
- if (!bo) {
- Component.toast("验证码错误!", 0);
- return;
- }
- var params = {
- 'phoneNumber': phoneNo,
- 'password': newPassword,
- 'confirmPassword': newPassword,
- 'captchaCode': mController.text,
- };
- final response =
- await http.post(Uri.parse('${Global.BaseUrl}user/setPassword'),
- headers: jsonHeaders(withToken: true),
- body: encodeBody(params));
- setState(() {
- _isLoading = true;
- });
- try {
- final json = decodeBodyToJson(response.bodyBytes);
- logd("修改密码结果=$json");
- NormalResponse mNormalResponse = new NormalResponse.fromJson(json);
- 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: AppBar(
- title: const Text('修改密码',
- style: TextStyle(
- color: Colors.white,
- )),
- centerTitle: true,
- elevation: 0.5,
- backgroundColor: Global.StatusBarColor,
- leading: IconButton(
- tooltip: '返回上一页',
- icon: const Icon(
- Icons.arrow_back_ios,
- color: Colors.white,
- ),
- onPressed: () {
- Navigator.of(context).pop();
- //_nextPage(-1);
- },
- ),
- ),
- body: SingleChildScrollView(
- child: Form(
- key: _formKey,
- child: Padding(
- padding: const EdgeInsets.all(20.0),
- child: Column(
- children: <Widget>[
- const SizedBox(height: 30.0),
- TextFormField(
- enabled: false,
- decoration: const InputDecoration(
- labelText: '手机号',
- ),
- initialValue: Global.loginPhoneNo,
- onSaved: (value) => phoneNo = value!,
- ),
- TextFormField(
- decoration: const InputDecoration(labelText: '新密码'),
- obscureText: true,
- onSaved: (value) => _newPassword = value!,
- ),
- TextFormField(
- decoration: const InputDecoration(labelText: '确认新密码'),
- obscureText: true,
- onSaved: (value) => _confirmPassword = value!,
- ),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- crossAxisAlignment: CrossAxisAlignment.baseline,
- textBaseline: TextBaseline.alphabetic,
- children: <Widget>[
- Expanded(
- child: Padding(
- padding:
- const 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:
- WidgetStateProperty.resolveWith<Color>((states) {
- return Colors.blue; // Regular color
- }),
- foregroundColor:
- WidgetStateProperty.all(Colors.white),
- ),
- // 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,
- ),
- ),
- ),
- ),
- ],
- ),
- Container(
- width: double.infinity,
- height: 45,
- margin: EdgeInsets.only(top: 40, left: 10, right: 10),
- child: ElevatedButton(
- style: ButtonStyle(
- backgroundColor:
- MaterialStateProperty.resolveWith<Color>((states) {
- return Colors.blue; // Regular color
- }),
- ),
- child: _isLoading
- ? CircularProgressIndicator(
- valueColor:
- AlwaysStoppedAnimation<Color>(Colors.white),
- )
- : Text(
- '修改密码',
- style: TextStyle(color: Colors.white, fontSize: 15),
- ),
- onPressed: _isLoading
- ? null
- : () {
- if (_formKey.currentState!.validate()) {
- _formKey.currentState?.save();
- if (_newPassword != _confirmPassword) {
- Component.toast("两次密码输入不一致", 0);
- return;
- }
- _changePassword(
- phoneNo: phoneNo,
- newPassword: _newPassword,
- );
- }
- },
- ),
- ),
- ],
- ),
- ),
- ),
- ),
- );
- }
- Future<String?> sendCaptchaCode() async {
- var params = {
- 'phoneNumber': Global.loginPhoneNo,
- };
- var response = await http.post(
- Uri.parse('${Global.BaseUrl}sendCaptchaCode'),
- body: encodeBody(params),
- headers: jsonHeaders());
- if (response.statusCode == 200) {
- final json = decodeBodyToJson(response.bodyBytes);
- logd("发送验证码结果=$json");
- NormalResponse mNormalResponse = new NormalResponse.fromJson(json);
- if (mNormalResponse.code == Global.responseSuccessCode) {
- Component.toast("短信发送成功,请查收!", 2);
- } else {
- Component.toast(mNormalResponse.msg.toString(), 0);
- }
- return response.toString();
- } else {
- Component.toast("出错了,请稍后再试!", 0);
- return null;
- }
- }
- }
|