19 changed files with 361 additions and 14 deletions
@ -0,0 +1,38 @@
|
||||
import 'package:dio/dio.dart'; |
||||
|
||||
class UserProvider { |
||||
final Dio _dio; |
||||
|
||||
// 通过构造函数注入 Dio 实例 |
||||
UserProvider({required Dio dio}) : _dio = dio; |
||||
|
||||
/// 调用 API 修改用户密码 |
||||
/// |
||||
/// @param newPassword 新密码 |
||||
Future<Response> changePassword(String newPassword) async { |
||||
try { |
||||
final response = await _dio.post( |
||||
'/api/change_password', // 替换为你的修改密码接口地址 |
||||
data: { |
||||
'new_password': newPassword, |
||||
}, |
||||
); |
||||
return response; |
||||
} on DioException catch (e) { |
||||
// 抛出 DioException 以便在控制器中处理 |
||||
throw e; |
||||
} |
||||
} |
||||
|
||||
/// 调用 API 获取用户信息 |
||||
/// |
||||
/// 这个方法可以用于获取用户的姓名、手机号等信息 |
||||
Future<Response> getUserInfo() async { |
||||
try { |
||||
final response = await _dio.get('/api/user_info'); // 替换为你的用户信息接口地址 |
||||
return response; |
||||
} on DioException catch (e) { |
||||
throw e; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,17 @@
|
||||
import 'package:dio/dio.dart'; |
||||
import 'package:get/get.dart'; |
||||
import 'package:problem_check_system/modules/my/controllers/change_password_controller.dart'; |
||||
import 'package:problem_check_system/data/providers/user_provider.dart'; // 假设你有这个 Provider |
||||
|
||||
class ChangePasswordBinding implements Bindings { |
||||
@override |
||||
void dependencies() { |
||||
// 如果 ChangePasswordController 依赖于 UserProvider,则需要先注入它 |
||||
final dio = Get.find<Dio>(); |
||||
Get.lazyPut<UserProvider>(() => UserProvider(dio: dio)); |
||||
Get.lazyPut<ChangePasswordController>( |
||||
// 如果需要,可以在这里注入依赖 |
||||
() => ChangePasswordController(userProvider: Get.find()), |
||||
); |
||||
} |
||||
} |
@ -0,0 +1,57 @@
|
||||
import 'package:get/get.dart'; |
||||
import 'package:problem_check_system/data/providers/user_provider.dart'; // 假设你有一个用户相关的 Provider |
||||
|
||||
class ChangePasswordController extends GetxController { |
||||
// 响应式变量,用于存储新密码和确认密码 |
||||
var newPassword = ''.obs; |
||||
var confirmPassword = ''.obs; |
||||
var isLoading = false.obs; |
||||
|
||||
// 假设你需要一个 UserProvider 来处理 API 请求 |
||||
final UserProvider _userProvider; |
||||
ChangePasswordController({required UserProvider userProvider}) |
||||
: _userProvider = userProvider; |
||||
|
||||
// 更新新密码 |
||||
void updateNewPassword(String value) { |
||||
newPassword.value = value; |
||||
} |
||||
|
||||
// 更新确认密码 |
||||
void updateConfirmPassword(String value) { |
||||
confirmPassword.value = value; |
||||
} |
||||
|
||||
// 修改密码逻辑 |
||||
Future<void> changePassword() async { |
||||
// 简单验证 |
||||
if (newPassword.value.isEmpty || confirmPassword.value.isEmpty) { |
||||
Get.snackbar('错误', '密码不能为空'); |
||||
return; |
||||
} |
||||
if (newPassword.value != confirmPassword.value) { |
||||
Get.snackbar('错误', '两次输入的密码不一致'); |
||||
return; |
||||
} |
||||
|
||||
isLoading.value = true; |
||||
try { |
||||
// TODO: 调用 API 来修改密码 |
||||
// final response = await _userProvider.changePassword(newPassword.value); |
||||
// if (response.statusCode == 200) { |
||||
// Get.back(); |
||||
// Get.snackbar('成功', '密码修改成功'); |
||||
// } else { |
||||
// Get.snackbar('失败', '密码修改失败,请重试'); |
||||
// } |
||||
// 模拟网络请求 |
||||
await Future.delayed(const Duration(seconds: 2)); |
||||
Get.back(); |
||||
Get.snackbar('成功', '密码修改成功', snackbarStatus: (status) {}); |
||||
} catch (e) { |
||||
Get.snackbar('错误', '修改密码失败: ${e.toString()}'); |
||||
} finally { |
||||
isLoading.value = false; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,166 @@
|
||||
import 'package:flutter/material.dart'; |
||||
import 'package:get/get.dart'; |
||||
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
||||
import 'package:problem_check_system/modules/my/controllers/change_password_controller.dart'; |
||||
|
||||
class ChangePasswordPage extends StatelessWidget { |
||||
const ChangePasswordPage({super.key}); |
||||
|
||||
@override |
||||
Widget build(BuildContext context) { |
||||
// 获取控制器实例 |
||||
final ChangePasswordController controller = Get.find<ChangePasswordController>(); |
||||
|
||||
return Scaffold( |
||||
appBar: _buildAppBar(), |
||||
body: Padding( |
||||
padding: EdgeInsets.symmetric(horizontal: 24.w), |
||||
child: Column( |
||||
children: [ |
||||
SizedBox(height: 16.h), |
||||
_buildInputField( |
||||
label: '新密码', |
||||
hintText: '请输入新密码', |
||||
onChanged: controller.updateNewPassword, |
||||
obscureText: true, |
||||
), |
||||
SizedBox(height: 24.h), |
||||
_buildInputField( |
||||
label: '确认新密码', |
||||
hintText: '请再次输入新密码', |
||||
onChanged: controller.updateConfirmPassword, |
||||
obscureText: true, |
||||
), |
||||
const Spacer(), // 占据剩余空间 |
||||
_buildButtons(controller), |
||||
SizedBox(height: 50.h), |
||||
], |
||||
), |
||||
), |
||||
); |
||||
} |
||||
|
||||
/// 自定义 AppBar |
||||
AppBar _buildAppBar() { |
||||
return AppBar( |
||||
backgroundColor: const Color(0xFFF1F7FF), |
||||
elevation: 0, |
||||
centerTitle: true, |
||||
title: const Text( |
||||
'修改密码', |
||||
style: TextStyle( |
||||
color: Colors.black, |
||||
fontWeight: FontWeight.bold, |
||||
), |
||||
), |
||||
leading: IconButton( |
||||
icon: const Icon(Icons.arrow_back_ios, color: Colors.black), |
||||
onPressed: () => Get.back(), |
||||
), |
||||
); |
||||
} |
||||
|
||||
/// 输入框组件 |
||||
Widget _buildInputField({ |
||||
required String label, |
||||
required String hintText, |
||||
required Function(String) onChanged, |
||||
bool obscureText = false, |
||||
}) { |
||||
return Container( |
||||
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 8.h), |
||||
decoration: BoxDecoration( |
||||
color: Colors.white, |
||||
borderRadius: BorderRadius.circular(12.r), |
||||
boxShadow: [ |
||||
BoxShadow( |
||||
color: Colors.grey.withOpacity(0.1), |
||||
spreadRadius: 2, |
||||
blurRadius: 5, |
||||
offset: const Offset(0, 3), |
||||
), |
||||
], |
||||
), |
||||
child: Column( |
||||
crossAxisAlignment: CrossAxisAlignment.start, |
||||
children: [ |
||||
Text( |
||||
label, |
||||
style: TextStyle( |
||||
fontSize: 16.sp, |
||||
fontWeight: FontWeight.w500, |
||||
color: Colors.black, |
||||
), |
||||
), |
||||
SizedBox(height: 8.h), |
||||
TextField( |
||||
onChanged: onChanged, |
||||
obscureText: obscureText, |
||||
decoration: InputDecoration( |
||||
hintText: hintText, |
||||
hintStyle: TextStyle( |
||||
color: Colors.grey, |
||||
fontSize: 14.sp, |
||||
), |
||||
border: InputBorder.none, // 移除下划线 |
||||
isDense: true, |
||||
contentPadding: EdgeInsets.zero, |
||||
), |
||||
), |
||||
], |
||||
), |
||||
); |
||||
} |
||||
|
||||
/// 底部按钮区域 |
||||
Widget _buildButtons(ChangePasswordController controller) { |
||||
return Row( |
||||
children: [ |
||||
// 取消按钮 |
||||
Expanded( |
||||
child: OutlinedButton( |
||||
onPressed: () => Get.back(), |
||||
style: OutlinedButton.styleFrom( |
||||
minimumSize: Size(160.w, 48.h), |
||||
side: const BorderSide(color: Color(0xFF5695FD)), |
||||
shape: RoundedRectangleBorder( |
||||
borderRadius: BorderRadius.circular(8.r), |
||||
), |
||||
), |
||||
child: Text( |
||||
'取消', |
||||
style: TextStyle( |
||||
fontSize: 16.sp, |
||||
color: const Color(0xFF5695FD), |
||||
), |
||||
), |
||||
), |
||||
), |
||||
SizedBox(width: 16.w), |
||||
// 确定按钮 |
||||
Expanded( |
||||
child: ElevatedButton( |
||||
onPressed: () { |
||||
// 调用控制器中的修改密码方法 |
||||
controller.changePassword(); |
||||
}, |
||||
style: ElevatedButton.styleFrom( |
||||
minimumSize: Size(160.w, 48.h), |
||||
backgroundColor: const Color(0xFF5695FD), |
||||
shape: RoundedRectangleBorder( |
||||
borderRadius: BorderRadius.circular(8.r), |
||||
), |
||||
), |
||||
child: Text( |
||||
'确定', |
||||
style: TextStyle( |
||||
fontSize: 16.sp, |
||||
color: Colors.white, |
||||
), |
||||
), |
||||
), |
||||
), |
||||
], |
||||
); |
||||
} |
||||
} |
Loading…
Reference in new issue