emailField property

TextFieldState emailField
latefinal

TextFieldState for inputting the email.

Implementation

late final TextFieldState emailField = TextFieldState(
  onFocus: (s) {
    if (s.text.isNotEmpty) {
      try {
        email = UserEmail(s.text);

        if (myUser.value!.emails.confirmed.contains(email) ||
            myUser.value?.emails.unconfirmed == email) {
          s.error.value = 'err_you_already_add_this_email'.l10n;
        }
      } catch (e) {
        s.error.value = 'err_incorrect_email'.l10n;
      }
    }
  },
  onSubmitted: (s) async {
    if (s.text.isEmpty ||
        (s.error.value != null && s.resubmitOnError.isFalse)) {
      return;
    }

    page.value = AddEmailPage.confirm;

    email = UserEmail(s.text.toLowerCase());

    try {
      await _myUserService.addUserEmail(
        email!,
        locale: L10n.chosen.value?.toString(),
      );

      _setResendEmailTimer();
    } catch (e) {
      page.value = AddEmailPage.add;
      s.unchecked = email?.val;

      if (e is AddUserEmailException) {
        s.error.value = e.toMessage();
        s.resubmitOnError.value = e.code == AddUserEmailErrorCode.busy;
      } else {
        s.error.value = 'err_data_transfer'.l10n;
        s.resubmitOnError.value = true;
      }

      s.unsubmit();
    }
  },
);