How to put two conditions in the Onpressed FlatButton?

Viewed 66

I tried to put two conditions with the values of an email and a password in order to activate my button only if the two conditions are validated but it is not working because I think my syntax is written in a bad way.

Can you help me to resolve this problem please ?

Plus I have no error messages except the fact that the button doesn't navigate to the HomePage like it lost its fonction.

This is my code

child: FlatButton(
                  padding: EdgeInsets.symmetric(vertical: 14, horizontal: 40),
                  color: DarkTurquoise,
                  onPressed: _password.length < 6 ? null :() {
                    !emailRegex.hasMatch(_email) ? null : () {
                      if (_formKey.currentState!.validate()) {
                        print(_email);
                        print(_password);
                      }
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => HomeScreen(),
                        ),
                      );
                    };
                    },
4 Answers

If you need to validate two conditions same time, use '&&' operator.

_password.length < 6 && !emailRegex.hasMatch(_email) ? <do something> : <do someting>
child: FlatButton(
                  padding: EdgeInsets.symmetric(vertical: 14, horizontal: 40),
                  color: DarkTurquoise,
                  onPressed: _password.length < 6 && !emailRegex.hasMatch(_email) ? null : () {
                  
                      if (_formKey.currentState!.validate()) {
                        print(_email);
                        print(_password);
                      }
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => HomeScreen(),
                        ),
                      );
          
                    },
),

In flutter, if you want a button to be greyed out or not clickable, you give null to onPressed, which is what is happening in your case based on the condition saying if password < 6.

What happens is when the widget is build, it reaches the line of onPressed, applies the condition you gave it, and it discovers that indeed the password is <6, because no characters have been entered yet.

The widget is built now, and what is done is done.

After you start entering letters, the length becomes longer than 6, but the widget has already been built and you didn't trigger your UI to update and rebuild the button.

What you can do, is to move the null inside your logic, this will not grey out the button, but when you tap it and the conditions fails, nothing happens, like this:

onPressed:   () {
if( _password.length >= 6) {
  if(emailRegex.hasMatch(_email)){
   if (_formKey.currentState!.validate()) {
    print(_email);
    print(_password);
    //I moved the curly brace which was here to the end of the function,
    //because I think you only want to navigate if the validation is true, not whenever it is pressed. 
     Navigator.push(
      context,
       MaterialPageRoute(
      builder: (context) => HomeScreen(),),
       );
      };
     }, //<= new curly brace
    }

use this:

onPressed: ()=>{
if (_formKey.currentState!.validate()) {
   if(_password.length >= 6 && emailRegex.hasMatch(_email)){
      // put your code here!
   }
}

}

This is not the right way.

Textfield validation in Flutter

Hope you can get your answer by referring to this article.

Also you can make one function for check password length and like that and create bool true or false and that bool is useful for enable and disable button as well.

Related