flutter_integrated_whb/lib/pages/mine/mine_set_pwd_page.dart

172 lines
5.0 KiB
Dart
Raw Normal View History

2025-07-16 08:38:10 +08:00
import 'dart:convert';
2025-07-11 11:03:21 +08:00
import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/custom_button.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:flutter/material.dart';
import 'package:qhd_prevention/tools/h_colors.dart';
2025-07-16 08:38:10 +08:00
import 'package:shared_preferences/shared_preferences.dart';
import '../../http/ApiService.dart';
import '../login_page.dart';
2025-07-11 11:03:21 +08:00
class MineSetPwdPage extends StatefulWidget {
const MineSetPwdPage({super.key});
@override
State<MineSetPwdPage> createState() => _MineSetPwdPageState();
}
class _MineSetPwdPageState extends State<MineSetPwdPage> {
final _oldPwdController = TextEditingController();
final _newPwdController = TextEditingController();
final _confirmPwdController = TextEditingController();
@override
void dispose() {
_oldPwdController.dispose();
_newPwdController.dispose();
_confirmPwdController.dispose();
super.dispose();
}
void _handleSubmit() {
final oldPwd = _oldPwdController.text.trim();
final newPwd = _newPwdController.text.trim();
final confirmPwd = _confirmPwdController.text.trim();
if (oldPwd.isEmpty || newPwd.isEmpty || confirmPwd.isEmpty) {
_showMessage('请填写完整所有字段');
return;
}
if (newPwd != confirmPwd) {
_showMessage('新密码与确认密码不一致');
return;
}
2025-07-16 08:38:10 +08:00
final RegExp regex = RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,18}$');
if (regex.hasMatch(newPwd)) {
_showMessage('密码长度为6-18位必须包含大小字母小写字母数字和特殊符号。');
return;
}
2025-07-11 11:03:21 +08:00
// 示例验证:密码复杂度(实际可用正则加强)
2025-07-16 08:38:10 +08:00
if (newPwd.length < 8 || newPwd.length > 18) {
_showMessage('密码长度需在8-18位之间');
2025-07-11 11:03:21 +08:00
return;
}
2025-07-16 08:38:10 +08:00
_changePass(oldPwd,newPwd);
2025-07-11 11:03:21 +08:00
}
void _showMessage(String msg) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
}
Widget _buildPwdField(String hintText, TextEditingController controller) {
return Column(
children: [
TextField(
controller: controller,
obscureText: true,
decoration: InputDecoration(
hintText: hintText,
border: InputBorder.none,
contentPadding: const EdgeInsets.symmetric(vertical: 10),
),
),
const Divider(height: 1, color: Colors.grey),
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: MyAppbar(title: '修改密码'),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'修改密码',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
const SizedBox(height: 30),
_buildPwdField('旧密码', _oldPwdController),
const SizedBox(height: 20),
_buildPwdField('新密码', _newPwdController),
const SizedBox(height: 20),
_buildPwdField('确认新密码', _confirmPwdController),
const SizedBox(height: 15),
const Text(
2025-07-16 08:38:10 +08:00
'需8-18位字母大小写、数字、字符混合',
2025-07-11 11:03:21 +08:00
style: TextStyle(color: Colors.grey, fontSize: 13),
),
const SizedBox(height: 30,),
SizedBox(
width: double.infinity,
height: 48,
2025-07-16 08:38:10 +08:00
child: CustomButton(
onPressed: () {
_handleSubmit();
},
text: "提交", backgroundColor: Colors.blue),
2025-07-11 11:03:21 +08:00
),
],
),
),
),
);
}
2025-07-16 08:38:10 +08:00
Future<void> _changePass(String oldPwd, String newPwd) async {
try {
// “我的工作” 数量
final raw = await ApiService.changePassWord(oldPwd, newPwd);
// final hidCount = raw['hidCount'] as Map<String, dynamic>;
// print(hidCount);
if (raw['result'] == 'success') {
_showMessage('密码修改成功'); // 这里换成实际调用接口逻辑
Navigator.pop(context,true);
// 清除用户登录状态
await _clearUserSession();
// 跳转到登录页并清除所有历史路由
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => LoginPage()),
(Route<dynamic> route) => false, // 移除所有历史路由
);
}else{
_showMessage('密码修改失败');
}
} catch (e) {
// 出错时可以 Toast 或者在页面上显示错误状态
print('加载首页数据失败:$e');
_showMessage('密码修改失败');
}
}
Future<void> _clearUserSession() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove('isLoggedIn'); // 清除登录状态
}
2025-07-11 11:03:21 +08:00
}
2025-07-16 08:38:10 +08:00