0

I have two forms which updates a non-observable User model on the form input's onChanged. Along with this, I have a state enum (observable) which determines whether or not the user is registering/logging in.

The login controller roughly has this implementation

class LoginController extends GetxController {
    Rx<LoginState> loginState = LoginState.notLoggedIn.obs;
    UserModel userModel = UserModel();

    bool get isLoggingIn => loginState.value == LoginState.loggingIn;
    bool get shouldDisableLoginFormSubmit =>
      isLoggingIn || !userModel.email.isEmail || userModel.password.isEmpty;

    ...

}

In the login form, I have a button with an isDisabled property as one of the children of a Column widget

Rough implementation of the form

class LoginForm extends StatelessWidget {
    LoginForm({Key? key}) : super(key: key);

    LoginController loginController = Get.find<LoginController>();

    @override
    Widget build(BuildContext context) {
        return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            ...
            Obx(() => InputTextFormField(
                onChanged: _onEmailFormInputChanged,
                inputType: TextInputType.emailAddress,
                isDisabled: loginController.isLoggingIn)),
            ...
            Obx(() => GenericRoundedGradientButton(
                isDisabled: loginController.shouldDisableLoginFormSubmit,
                onPressed: loginController.signInWithEmail)),
        ]);
    }
}

The LoginWithEmailButton is rebuilding on each form input change, which is what I want.

A similar approach to the registration form however, shows that the RegisterWithEmailButton is not rebuilding on the form input's onChanged.

Registration controller:

class RegisterController extends GetxController {
    Rx<RegisterState> registerState = RegisterState.notRegistered.obs;
    UserModel userModel = UserModel();
    bool get isRegistering => registerState.value == RegisterState.registering;
    bool get shouldDisableRegisterFormSubmit =>
        isRegistering ||
        userModel.fullName.isEmpty ||
        !userModel.email.isEmail ||
        userModel.password.isEmpty;

    ...

}

And the registration form:

class RegisterForm extends StatelessWidget {
    RegisterForm({
        Key? key,
    }) : super(key: key);

    RegisterController registerController = Get.find<RegisterController>();

    @override
    Widget build(BuildContext context) {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Obx(() => InputTextFormField(
            onChanged: _onEmailFormInputChanged,
            inputType: TextInputType.email,
            isDisabled: registerController.isRegistering)),
        ...
        Obx(() => GenericRoundedGradientButton(
            isDisabled: registerController.shouldDisableRegisterFormSubmit,
            onPressed: registerController.registerWithEmail))
        ]);
    }
}

However, the register with email button does not update its state. Both controllers are initialised in a previous page and kept as permanent. Has anyone faced this sort of issue before?

  • What is the implementation of `registerWithEmail` ? What function is executed when you press this button? – Kozubi Oct 14 '21 at 09:11
  • @Kozubi it calls a method that returns a `Future`. I don't think that has anything to do with the problem as the method is never called while filling up the form. Rather, the problem right now is that the state is not updating on the input form field's `onChanged` for one form but it is for the other and both forms and controllers are incredibly similar (different strings, extra form fields, etc.) – Loong Yeat Oct 14 '21 at 11:24
  • Can you show the `_onEmailFormInputChanged` method? – S. M. JAHANGIR Oct 18 '21 at 21:19

0 Answers0