Flutter 如何在flatter中匹配密码和确认密码?

Flutter 如何在flatter中匹配密码和确认密码?,flutter,dart,Flutter,Dart,我刚学会了颤振。这里我想问一下如何匹配密码和确认密码。这里我将给出我的代码。我也使用TextField。我这里不用验证器来验证 TextField( key: passkey, style: new TextStyle(color: Colors.white), controller: password, decoration: Inpu

我刚学会了颤振。这里我想问一下如何匹配密码和确认密码。这里我将给出我的代码。我也使用TextField。我这里不用验证器来验证

                TextField(
                key: passkey,
                style: new TextStyle(color: Colors.white),
                controller: password,
                decoration: InputDecoration(
                  labelText: 'Password',
                  labelStyle: TextStyle(color: Colors.white),
                  hintStyle: TextStyle(color: Colors.white),
                  icon: const Padding(
                      padding: const EdgeInsets.only(top: 15.0),
                      child: const Icon(
                        Icons.lock_outline,
                        color: Colors.white,
                      )
                      ),
                  errorText: validate ? 'Password Can\'t Be Empty' : null,
                ),
                obscureText: _obscureText,

              ),
              TextField(
                style: new TextStyle(color: Colors.white),
                controller: confirmpassword,
                decoration: InputDecoration(
                  labelText: 'Retype Password',
                  labelStyle: TextStyle(color: Colors.white),
                  hintStyle: TextStyle(color: Colors.white),
                  icon: const Padding(
                      padding: const EdgeInsets.only(top: 15.0),
                      child: const Icon(
                        Icons.lock_outline,
                        color: Colors.white,
                      )),
                  // errorText:
                  // validate ? 'Password Can\'t Be Empty' : null,
                ),
                obscureText: _obscureText,
              ),

使用由内置验证器组成的TextFormField小部件

  // Form
  final GlobalKey<FormState> _form = GlobalKey<FormState>();
  final TextEditingController _pass = TextEditingController();
  final TextEditingController _confirmPass = TextEditingController();

  Form(
        key: _form,
        child: ListView(
              children: <Widget>[
                     TextFormField(
                           controller: _pass,
                           validator: (val){
                              if(val.isEmpty)
                                   return 'Empty';
                              return null;
                              }
                     ),
                      TextFormField(
                           controller: _confirmPass,
                           validator: (val){
                              if(val.isEmpty)
                                   return 'Empty';
                              if(val != _pass.text)
                                   return 'Not Match'
                              return null;
                              }
                     ),
                       ]
              )
    )


    // To validate call
    _form.currentState.validate()
//表单
最终的GlobalKey _form=GlobalKey();
最终TextEditingController _pass=TextEditingController();
最终TextEditingController _confirmPass=TextEditingController();
形式(
键:_形式,
子:ListView(
儿童:[
TextFormField(
管制员:"通过,,
验证器:(val){
如果(val.isEmpty)
返回“空”;
返回null;
}
),
TextFormField(
控制员:_confirmPass,
验证器:(val){
如果(val.isEmpty)
返回“空”;
如果(val!=\u pass.text)
返回“不匹配”
返回null;
}
),
]
)
)
//验证呼叫
_form.currentState.validate()

只需先声明一个变量,然后将其值赋给我们正在使用的值即可

然后将其向下比较,如下所示:

GlobalKey<FormState> _formKey = GlobalKey<FormState>();
      var confirmPass;
    TextFormField(
                                validator: (String value) {
                                  confirmPass = value;
                                  if (value.isEmpty) {
                                    return "Please Enter New Password";
                                  } else if (value.length < 8) {
                                    return "Password must be atleast 8 characters long";
                                  } else {
                                    return null;
                                  }
                                },
                                decoration: InputDecoration(
                                  hintText: "Enter New Password",
                                  hintStyle: TextStyle(color: Colors.grey),
                                  border: new OutlineInputBorder(
                                    borderRadius: const BorderRadius.all(
                                      const Radius.circular(40.0),
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            SizedBox(height: 20),
                            Container(
                              child: TextFormField(
                                validator: (String value) {
                                  if (value.isEmpty) {
                                    return "Please Re-Enter New Password";
                                  } else if (value.length < 8) {
                                    return "Password must be atleast 8 characters long";
                                  } else if (value != confirmPass) {
                                    return "Password must be same as above";
                                  } else {
                                    return null;
                                  }
                                },
                                decoration: InputDecoration(
                                  hintText: "Re-Enter New Password",
                                  hintStyle: TextStyle(color: Colors.grey),
                                  border: new OutlineInputBorder(
                                    borderRadius: const BorderRadius.all(
                                      const Radius.circular(40.0),
                                    ),
                                  ),
                                ),
                              ),
                            ),

GlobalKey\u formKey=GlobalKey();
var确认通行证;
TextFormField(
验证器:(字符串值){
confirmPass=值;
if(value.isEmpty){
返回“请输入新密码”;
}else if(value.length<8){
return“密码长度必须至少为8个字符”;
}否则{
返回null;
}
},
装饰:输入装饰(
hintText:“输入新密码”,
hintStyle:TextStyle(颜色:Colors.grey),
边框:新大纲输入边框(
borderRadius:const borderRadius.all(
圆形常数半径(40.0),
),
),
),
),
),
尺寸箱(高度:20),
容器(
子项:TextFormField(
验证器:(字符串值){
if(value.isEmpty){
返回“请重新输入新密码”;
}else if(value.length<8){
return“密码长度必须至少为8个字符”;
}else if(值!=confirmPass){
return“密码必须与上面相同”;
}否则{
返回null;
}
},
装饰:输入装饰(
hintText:“重新输入新密码”,
hintStyle:TextStyle(颜色:Colors.grey),
边框:新大纲输入边框(
borderRadius:const borderRadius.all(
圆形常数半径(40.0),
),
),
),
),
),

我使用RxDart和Streams进行了验证。虽然这是一个多一点的工作,我保证最终的结果提高了性能和用户体验。


为什么不使用验证器?你想要另一种方法吗?为什么不试试正则表达式呢?如果(val!=\u pass.text)你可以试试val.compareTo(\u pass.text)==0,那么这一行总是返回true,只需先声明一个变量,然后将它的值赋给我们正在做的值,然后按照上面所示进行比较