flutter_integrated_whb/lib/pages/mine/mine_set_pwd_page.dart

172 lines
5.0 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'dart:convert';
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';
import 'package:shared_preferences/shared_preferences.dart';
import '../../http/ApiService.dart';
import '../login_page.dart';
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;
}
final RegExp regex = RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,18}$');
if (regex.hasMatch(newPwd)) {
_showMessage('密码长度为6-18位必须包含大小字母小写字母数字和特殊符号。');
return;
}
// 示例验证:密码复杂度(实际可用正则加强)
if (newPwd.length < 8 || newPwd.length > 18) {
_showMessage('密码长度需在8-18位之间');
return;
}
_changePass(oldPwd,newPwd);
}
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(
'需8-18位字母大小写、数字、字符混合',
style: TextStyle(color: Colors.grey, fontSize: 13),
),
const SizedBox(height: 30,),
SizedBox(
width: double.infinity,
height: 48,
child: CustomButton(
onPressed: () {
_handleSubmit();
},
text: "提交", backgroundColor: Colors.blue),
),
],
),
),
),
);
}
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'); // 清除登录状态
}
}