precode_sendemail_controller.dart 1.7 KB

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