precode_sendemail_controller.dart 1.8 KB

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