tanlongsheng 2 years ago
parent
commit
fb38c376d6
  1. 69
      lib/modules/login/controllers/login_controller.dart
  2. 115
      lib/modules/login/views/login_view.dart
  3. 2
      lib/modules/profile/controllers/profile_controller.dart

69
lib/modules/login/controllers/login_controller.dart

@ -19,6 +19,54 @@ enum LoginViewState {
class LoginController extends GetxController {
static LoginController get to => Get.find();
final GetStorage _storage = GetStorage();
// 添加国家编码相关状态
final _selectedCountryCode = '+86'.obs;
final _selectedCountryFlag = '🇨🇳'.obs;
// 国家编码 Getter
String get selectedCountryCode => _selectedCountryCode.value;
String get selectedCountryFlag => _selectedCountryFlag.value;
// 预设国家编码列表
final List<Map<String, String>> countryCodes = [
{'name': '邮箱', 'code': '', 'flag': '✉️'}, // 保持首位
{'name': '中国', 'code': '+86', 'flag': '🇨🇳'}, // 保持第二位
// 按国家代码数字升序排列(去除+号后排序)
{'name': '俄罗斯', 'code': '+7', 'flag': '🇷🇺'},
{'name': '英国', 'code': '+44', 'flag': '🇬🇧'},
{'name': '德国', 'code': '+49', 'flag': '🇩🇪'},
{'name': '意大利', 'code': '+39', 'flag': '🇮🇹'},
{'name': '法国', 'code': '+33', 'flag': '🇫🇷'},
{'name': '西班牙', 'code': '+34', 'flag': '🇪🇸'},
{'name': '南非', 'code': '+27', 'flag': '🇿🇦'},
{'name': '沙特阿拉伯', 'code': '+966', 'flag': '🇸🇦'},
{'name': '土耳其', 'code': '+90', 'flag': '🇹🇷'},
{'name': '日本', 'code': '+81', 'flag': '🇯🇵'},
{'name': '韩国', 'code': '+82', 'flag': '🇰🇷'},
{'name': '新加坡', 'code': '+65', 'flag': '🇸🇬'},
{'name': '马来西亚', 'code': '+60', 'flag': '🇲🇾'},
{'name': '泰国', 'code': '+66', 'flag': '🇹🇭'},
{'name': '越南', 'code': '+84', 'flag': '🇻🇳'},
{'name': '印度', 'code': '+91', 'flag': '🇮🇳'},
{'name': '巴基斯坦', 'code': '+92', 'flag': '🇵🇰'},
{'name': '菲律宾', 'code': '+63', 'flag': '🇵🇭'},
{'name': '印度尼西亚', 'code': '+62', 'flag': '🇮🇩'},
{'name': '阿联酋', 'code': '+971', 'flag': '🇦🇪'},
{'name': '美国', 'code': '+1', 'flag': '🇺🇸'},
{'name': '加拿大', 'code': '+1', 'flag': '🇨🇦'},
{'name': '墨西哥', 'code': '+52', 'flag': '🇲🇽'},
{'name': '巴西', 'code': '+55', 'flag': '🇧🇷'},
{'name': '阿根廷', 'code': '+54', 'flag': '🇦🇷'},
{'name': '澳大利亚', 'code': '+61', 'flag': '🇦🇺'},
{'name': '新西兰', 'code': '+64', 'flag': '🇳🇿'},
// 中国地区(保持最后)
{'name': '中国香港', 'code': '+852', 'flag': '🇭🇰'},
{'name': '中国澳门', 'code': '+853', 'flag': '🇲🇴'},
{'name': '中国台湾', 'code': '+886', 'flag': '🇹🇼'},
];
// 状态管理
final _viewState = LoginViewState.welcome.obs;
final _isLoading = false.obs;
@ -52,6 +100,19 @@ class LoginController extends GetxController {
super.onClose();
}
// 更新选中的国家编码
void updateCountryCode(String code, String flag) {
_selectedCountryCode.value = code;
_selectedCountryFlag.value = flag;
// 根据选择更新输入框类型
if (code == '@') {
contactController.clear(); // 清空当前输入
loginType = 0; // 设置为邮箱登录类型
} else {
loginType = 1; // 设置为手机号登录类型
}
}
// 获取验证码
void getVerificationCode() {
if (contactController.text.isEmpty) {
@ -146,7 +207,7 @@ class LoginController extends GetxController {
void _formatDisplayContact() {
String contact = _contactValue.value;
if (contact.contains('@')) {
// 邮箱
// 邮箱逻辑保持不变
final parts = contact.split('@');
if (parts[0].length > 3) {
_displayContact.value = parts[0].substring(0, 3) + '****@' + parts[1];
@ -155,14 +216,14 @@ class LoginController extends GetxController {
}
loginType = 0;
} else if (RegExp(r'^\d+$').hasMatch(contact)) {
// 手机号
// 手机号格式化时包含国家编码
if (contact.length > 7) {
_displayContact.value = '+86 ' +
_displayContact.value = '$selectedCountryCode ' +
contact.substring(0, 3) +
'****' +
contact.substring(contact.length - 4);
} else {
_displayContact.value = contact;
_displayContact.value = '$selectedCountryCode $contact';
}
loginType = 1;
} else {

115
lib/modules/login/views/login_view.dart

@ -170,7 +170,7 @@ class LoginView extends GetView<LoginController> {
);
}
// 输入框
// 输入框
Widget _buildInputField(bool isDarkMode) {
return Container(
decoration: BoxDecoration(
@ -183,17 +183,53 @@ class LoginView extends GetView<LoginController> {
padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 4.h),
child: Row(
children: [
Icon(
Icons.person_outline,
color: isDarkMode ? Colors.grey[400] : Colors.black54,
size: 20.sp,
// 添加国家选择按钮
InkWell(
onTap: () => _showCountryPicker(isDarkMode),
child: Container(
padding: EdgeInsets.symmetric(horizontal: 4.w),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Obx(() => Text(
'${controller.selectedCountryFlag}${controller.selectedCountryCode}',
style: TextStyle(
fontSize: 14.sp,
color: isDarkMode ? Colors.white : Colors.black,
),
)),
Icon(
Icons.arrow_drop_down,
color: isDarkMode ? Colors.grey[400] : Colors.black54,
size: 20.sp,
),
],
),
),
),
SizedBox(width: 12.w),
// 分隔线
Container(
height: 20.h,
width: 1,
color: isDarkMode ? Colors.grey[800] : Colors.grey[300],
margin: EdgeInsets.symmetric(horizontal: 8.w),
),
// Icon(
// Icons.person_outline,
// color: isDarkMode ? Colors.grey[400] : Colors.black54,
// size: 20.sp,
// ),
//SizedBox(width: 12.w),
Expanded(
child: TextField(
controller: controller.contactController,
// keyboardType: controller.selectedCountryFlag == '✉️'
// ? TextInputType.emailAddress
// : TextInputType.phone,
decoration: InputDecoration(
hintText: 'accountNumber'.tr, // 手机号码或邮箱
hintText: controller.selectedCountryFlag == '✉️'
? 'email'.tr // 需要在翻译文件中添加
: 'phoneNumber'.tr, // 需要在翻译文件中添加
hintStyle: TextStyle(
fontSize: 14.sp,
color: isDarkMode ? Colors.grey[600] : Colors.black38,
@ -214,6 +250,71 @@ class LoginView extends GetView<LoginController> {
);
}
// 添加国家选择底部弹窗
void _showCountryPicker(bool isDarkMode) {
Get.bottomSheet(
Container(
height: 400.h,
padding: EdgeInsets.all(16.w),
decoration: BoxDecoration(
color: isDarkMode ? Colors.grey[900] : Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(20.r),
topRight: Radius.circular(20.r),
),
),
child: Column(
children: [
Text(
'selectCountry'.tr, // 选择国家/地区
style: TextStyle(
fontSize: 18.sp,
fontWeight: FontWeight.bold,
color: isDarkMode ? Colors.white : Colors.black,
),
),
SizedBox(height: 16.h),
Expanded(
child: ListView.builder(
itemCount: controller.countryCodes.length,
itemBuilder: (context, index) {
final country = controller.countryCodes[index];
return ListTile(
onTap: () {
controller.updateCountryCode(
country['code']!,
country['flag']!,
);
Get.back();
},
leading: Text(
country['flag']!,
style: TextStyle(fontSize: 24.sp),
),
title: Text(
country['name']!,
style: TextStyle(
color: isDarkMode ? Colors.white : Colors.black,
),
),
trailing: Text(
country['code']!,
style: TextStyle(
color: isDarkMode ? Colors.grey[400] : Colors.grey[600],
),
),
);
},
),
),
],
),
),
backgroundColor: Colors.transparent,
isScrollControlled: true,
);
}
// 验证码输入框
Widget _buildVerificationInputField(bool isDarkMode) {
return Container(

2
lib/modules/profile/controllers/profile_controller.dart

@ -154,7 +154,7 @@ class ProfileController extends GetxController {
));
// 调用API前打印token
Logger().d('当前用户token: ${userInfo.value.token}');
Logger().d('当前用户token: ${_storage.read<String>("logintoken")}');
Logger().d('请求数据: ${{"user": data}}');
final response = await dio.post(
'/api/home/user_setting',

Loading…
Cancel
Save