1

Here's the Button code

ButtonWidget(
                  text: 'Submit',
                  onClicked: () {
                    Navigator.pushAndRemoveUntil(
                        context, MaterialPageRoute(builder: (_) => SPAL()),
                          (Route<dynamic> route) => false,
                    );
                    print('Email: ${emailController.text}');
                    print('Password: ${password}');
                  },
                ),

How i can make the button check if the email and password is empty or not, if it is then button wont open next page but if it was filled then the button will open the next Screen

1 Answer 1

2

you need to add a validator in the email and password field, then when the user clicks on the button you can check for validation. the email and password field needs to be put inside a form with a key

final formKey = GlobalKey<FormState>();
final anotherKey= GlobalKey<FormState>();
Form(
   key: formKey,
        child: Column(
           children: <Widget>[
              emailField(),
              passwordField(),
              ButtonWidget(),
           ]
       ),
),

  Widget emailField(String value) {
    return TextFormField(
    //using controller
    controller: emailController,
      decoration: InputDecoration(
          labelText: 'Email',
          hintText: 'Enter Email',
      validator: (value) => value == null || value.isEmpty ? "enter your email" : null,
    //don't need onSaved when using controller
      //onSaved: (String value) {
      //  emailController.text = value.toLowerCase().trim();
      //},
    );
  }

ButtonWidget(
                  text: 'Submit',
                  onClicked: () {
                     if(formKey.currentState.validate() && anotherKey.currenState.validate()) {
                      formKey.currentState.save();
                      anotherKey.currentState.save();
                      Navigator.pushAndRemoveUntil(
                          context, MaterialPageRoute(builder: (_) => SPAL()),
                            (Route<dynamic> route) => false,
                      );
                    }
                    print('Email: ${emailController.text}');
                    print('Password: ${password}');
                  },
                ),
8
  • email= value.toLowerCase().trim(); where this come from?
    – Macchiato
    May 27, 2021 at 3:41
  • you have to create it somewhere in your code String email = ""; if you want to use controller then you can pass the controller to the emailField instead controller: emailController, May 27, 2021 at 3:45
  • how to Validate 2 different keys?
    – Macchiato
    May 27, 2021 at 4:04
  • both fields are validated since they are inside the same form. if you created 2 different forms then you need to create another key and pass it to that other form. May 27, 2021 at 4:10
  • How can I pass my other key to the email form?
    – Macchiato
    May 27, 2021 at 4:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.