【问题标题】:How to put two conditions in the Onpressed FlatButton?如何在 Onpressed FlatButton 中放置两个条件?
【发布时间】:2021-10-14 21:31:33
【问题描述】:

我尝试使用电子邮件和密码的值设置两个条件,以便仅在两个条件都得到验证但它不起作用的情况下激活我的按钮,因为我认为我的语法写得不好。

你能帮我解决这个问题吗?

此外,我没有任何错误消息,只是按钮无法导航到主页,就像它失去了功能一样。

这是我的代码

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(),
                        ),
                      );
                    };
                    },

【问题讨论】:

    标签: forms flutter validation dart syntax


    【解决方案1】:

    如果您需要同时验证两个条件,请使用“&&”运算符。

    _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(),
                            ),
                          );
              
                        },
    ),
    

    【讨论】:

    • 谢谢我不知道 && 存在!
    【解决方案2】:

    这不是正确的方法。

    Textfield validation in Flutter

    希望您可以参考这篇文章得到答案。

    您还可以创建一个功能来检查密码长度,并创建 bool true 或 false,并且该 bool 对于启用和禁用按钮也很有用。

    【讨论】:

    • 谢谢你现在它正在工作我刚刚使用了 && 运算符
    【解决方案3】:

    使用这个:

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

    【讨论】:

    • 谢谢你的工作我将你的代码与另一条评论结合起来!
    • 没问题 :),我都做了
    【解决方案4】:

    在颤振中,如果您希望按钮变灰或不可点击,则将 null 设置为 onPressed,这就是根据密码是否

    发生的情况是,当小部件被构建时,它到达 onPressed 行,应用你给它的条件,它发现密码确实是

    小部件现在已构建,并且已完成。

    开始输入字母后,长度变长超过 6,但小部件已经构建,您没有触发 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
        }
    

    【讨论】:

    • 没有灰色按钮没什么大不了的,现在它工作得很好,谢谢你我把你的代码和另一个评论结合起来了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2020-06-13
    • 1970-01-01
    • 2011-04-08
    • 2021-11-18
    • 2016-06-13
    • 2020-08-18
    相关资源
    最近更新 更多