starwork_flutter/lib/views/login/login_controller.dart

213 lines
6.7 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:starwork_flutter/api/model/common/request/send_validation_code_request.dart';
import 'package:starwork_flutter/api/service/common_api_service.dart';
import 'package:starwork_flutter/base/base_controller.dart';
import 'package:starwork_flutter/common/constant/login_type.dart';
import 'package:starwork_flutter/common/constant/validation_code_channel.dart';
import 'package:starwork_flutter/common/constant/validation_code_type.dart';
import 'package:starwork_flutter/routes/app_pages.dart';
import 'package:starwork_flutter/routes/app_routes.dart';
class LoginController extends BaseController {
final commonApi = Get.find<CommonApiService>();
int phoneNumberSize = 11;
TextEditingController phoneController = TextEditingController();
TextEditingController passwordController = TextEditingController();
final isFormValid = false.obs;
final isPasswordVisible = true.obs;
final isPrivacyAgreementValid = 0.obs;
final loginType = LoginType.phoneCodeLogin.obs;
@override
void onInit() {
super.onInit();
// 监听输入变化
phoneController.addListener(_validateForm);
phoneController.text = '18269109817';
}
@override
void onClose() {
phoneController.removeListener(_validateForm);
phoneController.dispose();
super.onClose();
}
void _validateForm() {
isFormValid.value = phoneController.text.length == phoneNumberSize;
debugPrint('isFormValid: ${isFormValid.value}');
}
// 获取手机验证码
void requestPhoneCode() async {
if (isPrivacyAgreementValid.value != 1) {
_showCustomDialog(
title: '欢迎使用星勤'.tr,
content: '1',
onConfirm: () async {
isPrivacyAgreementValid.value = 1;
},
);
}
_handleRequestPhoneCode();
}
void _handleRequestPhoneCode() async {
var sendValidationCodeResult = await commonApi.sendValidationCode(
request: SendValidationCodeRequest(
countryCode: '+86',
account: phoneController.text.trim(),
channel: ValidationCodeChannel.sms.value,
codeType: ValidationCodeType.login,
),
);
if (sendValidationCodeResult.isSuccess) {
Get.toNamed(
AppRoutes.inputVerificationCode,
parameters: {
'phone': phoneController.text.trim(),
},
);
}
}
void _showCustomDialog({
required String title,
required String content,
required VoidCallback onConfirm,
}) {
Get.dialog(
Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(14.r), // 圆角
),
backgroundColor: Colors.white,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Padding(
padding: EdgeInsets.only(top: 22.h),
child: Text(
title,
style: TextStyle(
fontSize: 18.sp,
fontWeight: FontWeight.bold,
),
),
),
Padding(
padding: EdgeInsets.all(22.w),
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: '请你阅读并同意'.tr,
style: TextStyle(
fontSize: 12.sp,
color: Colors.grey,
),
),
TextSpan(
text: '《用户协议》'.tr,
style: TextStyle(
fontSize: 12.sp,
color: Colors.blue,
),
),
TextSpan(
text: ''.tr,
style: TextStyle(
fontSize: 12.sp,
color: Colors.grey,
),
),
TextSpan(
text: '《隐私政策》'.tr,
style: TextStyle(
fontSize: 12.sp,
color: Colors.blue,
),
),
],
),
),
),
SizedBox(height: 20.h),
Container(
decoration: const BoxDecoration(
border: Border(
top: BorderSide(
color: Colors.grey, // 右侧边框颜色
width: 0.5, // 右侧边框宽度
),
),
),
child: Row(
children: [
Expanded(
// 左侧文本占一半
child: GestureDetector(
onTap: () {
Get.back();
},
child: Container(
alignment: Alignment.center,
height: 42.h,
decoration: const BoxDecoration(
border: Border(
right: BorderSide(
color: Colors.grey, // 右侧边框颜色
width: 0.5, // 右侧边框宽度
),
),
),
child: Text(
'取消'.tr,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 14.sp, color: Colors.grey),
),
),
),
),
Expanded(
// 右侧文本占一半
child: GestureDetector(
onTap: () {
Get.back();
onConfirm();
},
child: Container(
alignment: Alignment.center,
height: 42.h,
child: Text(
'同意并继续'.tr,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14.sp,
color: Colors.blue,
fontWeight: FontWeight.w600,
),
),
),
),
),
],
),
)
],
),
),
);
}
}