| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:nomo/app/dialog/all_dialog.dart';
- class PrecodeSendemailController extends GetxController {
- // 邮箱输入控制器
- final emailController = TextEditingController();
- // 输入框焦点
- final emailFocusNode = FocusNode();
- // 是否聚焦
- final isFocused = false.obs;
- // 是否正在发送
- final isSending = false.obs;
- // 邮箱地址
- final email = ''.obs;
- // 是否可以发送(邮箱格式验证)
- bool get canSend => _isValidEmail(email.value) && !isSending.value;
- @override
- void onInit() {
- super.onInit();
- // 监听输入变化
- emailController.addListener(() {
- email.value = emailController.text;
- });
- // 监听焦点变化
- emailFocusNode.addListener(() {
- isFocused.value = emailFocusNode.hasFocus;
- });
- }
- @override
- void onClose() {
- emailController.dispose();
- emailFocusNode.dispose();
- super.onClose();
- }
- // 验证邮箱格式
- bool _isValidEmail(String email) {
- if (email.isEmpty) return false;
- final emailRegex = RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
- return emailRegex.hasMatch(email);
- }
- // 发送邮件
- Future<void> sendEmail() async {
- if (!canSend) return;
- isSending.value = true;
- try {
- // TODO: 实现实际的发送邮件功能
- await Future.delayed(const Duration(seconds: 2));
- AllDialog.showEmailSent();
- } catch (e) {
- AllDialog.showCustomError(
- title: 'Error',
- message: 'Failed to send email',
- buttonText: 'Retry',
- cancelText: 'Cancel',
- );
- } finally {
- isSending.value = false;
- }
- }
- }
|