import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:image_picker/image_picker.dart'; class UserInfoController extends GetxController { // 用户信息表单字段 final gender = 'male'.obs; // 默认选择男性 final nickname = ''.obs; final birthday = ''.obs; final education = ''.obs; final invitationCode = ''.obs; final avatarUrl = ''.obs; // 加载状态 final isSubmitting = false.obs; // GetStorage实例,用于存储用户信息 final storage = GetStorage(); // 选择性别 void selectGender(String selectedGender) { gender.value = selectedGender; // 打印选择的性别 print('选择的性别: $selectedGender'); // 显示选择结果 SmartDialog.showToast('已选择${selectedGender == 'male' ? '男' : '女'}性'); } // 选择日期 void selectBirthday(String selectedDate) { birthday.value = selectedDate; // 打印选择的日期 print('选择的出生日期: $selectedDate'); // 显示选择结果 SmartDialog.showToast('出生日期已选择'); } // 选择学历 void selectEducation(String selectedEducation) { education.value = selectedEducation; // 打印选择的学历 print('选择的学历: $selectedEducation'); // 显示选择结果 SmartDialog.showToast('学历已选择'); } // 选择头像 - 业务逻辑处理 Future handleCameraCapture() async { try { // 请求相机权限并拍照 final ImagePicker picker = ImagePicker(); final XFile? photo = await picker.pickImage(source: ImageSource.camera); if (photo != null) { await processSelectedImage(File(photo.path)); } } catch (e) { print('拍照失败: $e'); // 更友好的错误提示 if (e.toString().contains('permission') || e.toString().contains('权限')) { SmartDialog.showToast('相机权限被拒绝,请在设置中允许访问相机'); } else if (e.toString().contains('camera') || e.toString().contains('相机')) { SmartDialog.showToast('设备没有可用的相机'); } else { SmartDialog.showToast('拍照失败,请重试'); } } } Future handleGallerySelection() async { try { // 从相册选择图片 final ImagePicker picker = ImagePicker(); final XFile? image = await picker.pickImage(source: ImageSource.gallery); if (image != null) { await processSelectedImage(File(image.path)); } } catch (e) { print('选择图片失败: $e'); // 更友好的错误提示 if (e.toString().contains('permission') || e.toString().contains('权限')) { SmartDialog.showToast('相册权限被拒绝,请在设置中允许访问相册'); } else { SmartDialog.showToast('选择图片失败,请重试'); } } } // 处理选中的图片 Future processSelectedImage(File imageFile) async { try { // 显示加载提示 SmartDialog.showLoading(msg: '正在处理头像...'); // 更新本地头像URL(实际项目中应该先上传到服务器) // 这里直接使用本地文件路径作为临时URL avatarUrl.value = imageFile.path; // 保存到本地存储 final currentUserInfo = storage.read('userInfo') ?? {}; if (currentUserInfo is Map) { currentUserInfo['avatarUrl'] = imageFile.path; await storage.write('userInfo', currentUserInfo); } SmartDialog.dismiss(); SmartDialog.showToast('头像设置成功'); } catch (e) { SmartDialog.dismiss(); print('处理图片失败: $e'); SmartDialog.showToast('处理图片失败'); } } // 选择头像入口方法(保持兼容性) void selectAvatar() { // 这个方法现在只是一个入口,实际的UI交互在页面中实现 // 页面会调用上面的具体处理方法 } // 验证表单 bool _validateForm() { // 验证昵称 if (nickname.value.isEmpty) { SmartDialog.showToast('请输入昵称'); return false; } // 验证生日 if (birthday.value.isEmpty) { SmartDialog.showToast('请选择出生日期'); return false; } // 验证学历 if (education.value.isEmpty) { SmartDialog.showToast('请选择学历'); return false; } return true; } // 构建提交参数 Map _buildSubmitParams() { return { 'gender': gender.value == 'male' ? 1 : 2, // 1:男, 2:女 'nickname': nickname.value, 'birthday': birthday.value, 'education': education.value, if (invitationCode.value.isNotEmpty) 'invitationCode': invitationCode.value, if (avatarUrl.value.isNotEmpty) 'avatarUrl': avatarUrl.value, }; } // 提交用户信息 Future submitUserInfo() async { // 验证表单 if (!_validateForm()) { return; } isSubmitting.value = true; try { // 构建请求参数 final params = _buildSubmitParams(); // 调用UserApi中的完善用户信息接口 // 注意:这里需要在UserApi中添加updateUserInfo方法 // 由于目前没有看到完整的API定义,这里先模拟成功 // 实际项目中应该调用真实的API // 模拟网络延迟 await Future.delayed(const Duration(seconds: 1)); // 打印提交的信息 print('提交用户信息参数: $params'); // 模拟成功响应 // final response = await _userApi.updateUserInfo(params); // 处理响应 // if (response.data.isSuccess) { // 更新本地存储的用户信息 final currentUserInfo = storage.read('userInfo') ?? {}; if (currentUserInfo is Map) { currentUserInfo.addAll({ 'gender': gender.value, 'nickname': nickname.value, 'birthday': birthday.value, 'education': education.value, }); await storage.write('userInfo', currentUserInfo); } // 显示成功提示 SmartDialog.showToast('信息提交成功!'); // 延迟后跳转 Future.delayed(const Duration(milliseconds: 1500), () { // 跳转到主页面(暂时注释,实际使用时需要导入并替换正确的路由) // Get.offAllNamed('/main'); }); // } else { // Get.snackbar('错误', response.data.message); // } } catch (e) { print('提交用户信息失败: $e'); // 显示错误提示 SmartDialog.showToast('提交失败,请重试'); } finally { isSubmitting.value = false; } } }