skip to Main Content

Whenever I use FocusManager or FocusScope.of(context) my code breaks and the controller.loading values are ignored.

When the keyboard is not open the code works perfectly fine

and

even if I place the FocusManager After the await it works perfectly fine

But

The Focus Manager or FocusScopr.of(context) break when placed like below

void resetPassword() async {
      FocusManager.instance.primaryFocus?.unfocus(); // Placed Here the code behaves very diffrently

      try {
 FocusManager.instance.primaryFocus?.unfocus(); // Also breaks here or at any place behind the await function
        controller.changeErrorStatus(false);

        if (emailController.text.isEmpty) {
          controller.changeErrorStatus(true);

          controller.changeErrorMessage(
              "An Error Occurred, Email Field cannot be left blank.");
          controller.startLoading(false);

          return;
        }
        controller.startLoading(true);
        await Future.delayed(Duration(seconds: 5));

        // await FirebaseAuth.instance
        //     .sendPasswordResetEmail(email: emailController.text.trim())
        //     .timeout(const Duration(seconds: 5));

        controller.startLoading(false);
      } catch (e) {
        controller.changeErrorStatus(true);
        controller.startLoading(false);

        controller.changeErrorMessage("An Error Occurred, $e");
        
      }
    }

It Works Like this

 
      try {
        controller.changeErrorStatus(false);

        if (emailController.text.isEmpty) {
          controller.changeErrorStatus(true);

          controller.changeErrorMessage(
              "An Error Occurred, Email Field cannot be left blank.");
          controller.startLoading(false);

          return;
        }
        controller.startLoading(true);
        await Future.delayed(Duration(seconds: 5));
FocusManager.instance.primaryFocus?.unfocus();  // If Focus Scope is here everything works Fine

        // await FirebaseAuth.instance
        //     .sendPasswordResetEmail(email: emailController.text.trim())
        //     .timeout(const Duration(seconds: 5));

        controller.startLoading(false);

Also tried calling it from the onTap function and it still breaks

onTap:(){ 
 FocusManager.instance.primaryFocus?.unfocus();
resetPassword();
}

The controller.isLoading values are right but the button disregards then for I don’t what reason

Obx(
              () => Container(
                alignment: Alignment.center,
                width: size.width * 0.8,
                height: 50,
                decoration: BoxDecoration(
                    color: mainController.isLoading.value
                        ? AppColors().secHalfGrey
                        : AppColors().primaryBlue,
                    borderRadius: BorderRadius.circular(10)),
                child: mainController.isLoading.value
                    ? Lottie.asset("assets/jsons/atom-loader.json")
                    : Text(
                        buttonText,
                        style: context.textTheme.displayMedium,
                      ),
              ),
            ),

2

Answers


  1. Chosen as BEST ANSWER

    I was messing around and found the answer

    I used the onTapOutside of the TextFormField and used the

    FocusManager.instance.primaryFocus?.unfocus();
    

    The Function is

    onTapOutside: (event) {
    FocusManager.instance.primaryFocus?.unfocus();
    },
    

    I hope it helps anyone who is facing the same problem.


  2. Use this :

    FocusScope.of(context).unfocus();
    

    Instead of :

     FocusManager.instance.primaryFocus?.unfocus();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search