precode_sendemail_controller.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:nomo/app/dialog/all_dialog.dart';
  4. import '../../../../../config/translations/strings_enum.dart';
  5. class PrecodeSendemailController extends GetxController {
  6. // 邮箱输入控制器
  7. final emailController = TextEditingController();
  8. // 输入框焦点
  9. final emailFocusNode = FocusNode();
  10. // 是否聚焦
  11. final isFocused = false.obs;
  12. // 是否正在发送
  13. final isSending = false.obs;
  14. // 邮箱地址
  15. final email = ''.obs;
  16. // 是否可以发送(邮箱格式验证)
  17. bool get canSend => _isValidEmail(email.value) && !isSending.value;
  18. @override
  19. void onInit() {
  20. super.onInit();
  21. // 监听输入变化
  22. emailController.addListener(() {
  23. email.value = emailController.text;
  24. });
  25. // 监听焦点变化
  26. emailFocusNode.addListener(() {
  27. isFocused.value = emailFocusNode.hasFocus;
  28. });
  29. }
  30. @override
  31. void onClose() {
  32. emailController.dispose();
  33. emailFocusNode.dispose();
  34. super.onClose();
  35. }
  36. // 验证邮箱格式
  37. bool _isValidEmail(String email) {
  38. if (email.isEmpty) return false;
  39. final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
  40. return emailRegex.hasMatch(email);
  41. }
  42. // 发送邮件
  43. Future<void> sendEmail() async {
  44. if (!canSend) return;
  45. isSending.value = true;
  46. try {
  47. // TODO: 实现实际的发送邮件功能
  48. await Future.delayed(const Duration(seconds: 2));
  49. AllDialog.showEmailSent();
  50. } catch (e) {
  51. AllDialog.showCustomError(
  52. title: Strings.error.tr,
  53. message: Strings.failedSendEmail.tr,
  54. buttonText: 'Retry',
  55. cancelText: 'Cancel',
  56. );
  57. } finally {
  58. isSending.value = false;
  59. }
  60. }
  61. }