如何在 Onpressed FlatButton 中放置两个条件?
How to put two conditions in the Onpressed FlatButton?
我试图将两个条件与电子邮件和密码的值放在一起,以便仅当两个条件都得到验证时才激活我的按钮,但它不起作用,因为我认为我的语法写得不好。
你能帮我解决这个问题吗?
此外,我没有收到任何错误消息,除了按钮没有导航到主页,就像它失去了功能一样。
这是我的代码
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(),
),
);
};
},
在 flutter 中,如果你想让一个按钮变灰或不可点击,你可以将 null 给 onPressed,这就是你的情况,根据条件说如果密码 < 6。
构建小部件时发生的情况是,它到达 onPressed 行,应用您给它的条件,它发现密码确实 <6,因为还没有输入任何字符。
现在小部件已构建,完成了。
开始输入字母后,长度超过6,但是widget已经建好了,你没有触发UI更新重建按钮。
你可以做的是,在你的逻辑中移动 null,这不会使按钮变灰,但是当你点击它并且条件失败时,什么也不会发生,就像这样:
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
}
使用这个:
onPressed: ()=>{
if (_formKey.currentState!.validate()) {
if(_password.length >= 6 && emailRegex.hasMatch(_email)){
// put your code here!
}
}
}
这不是正确的方法。
希望您能通过这篇文章得到您的答案。
您还可以创建一个函数来检查密码长度等,并创建 bool true 或 false,该 bool 对于启用和禁用按钮也很有用。
如果您需要同时验证两个条件,请使用“&&”运算符。
_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(),
),
);
},
),
我试图将两个条件与电子邮件和密码的值放在一起,以便仅当两个条件都得到验证时才激活我的按钮,但它不起作用,因为我认为我的语法写得不好。
你能帮我解决这个问题吗?
此外,我没有收到任何错误消息,除了按钮没有导航到主页,就像它失去了功能一样。
这是我的代码
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(),
),
);
};
},
在 flutter 中,如果你想让一个按钮变灰或不可点击,你可以将 null 给 onPressed,这就是你的情况,根据条件说如果密码 < 6。
构建小部件时发生的情况是,它到达 onPressed 行,应用您给它的条件,它发现密码确实 <6,因为还没有输入任何字符。
现在小部件已构建,完成了。
开始输入字母后,长度超过6,但是widget已经建好了,你没有触发UI更新重建按钮。
你可以做的是,在你的逻辑中移动 null,这不会使按钮变灰,但是当你点击它并且条件失败时,什么也不会发生,就像这样:
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
}
使用这个:
onPressed: ()=>{
if (_formKey.currentState!.validate()) {
if(_password.length >= 6 && emailRegex.hasMatch(_email)){
// put your code here!
}
}
}
这不是正确的方法。
希望您能通过这篇文章得到您的答案。
您还可以创建一个函数来检查密码长度等,并创建 bool true 或 false,该 bool 对于启用和禁用按钮也很有用。
如果您需要同时验证两个条件,请使用“&&”运算符。
_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(),
),
);
},
),