我刚学了 Flutter。这里想问一下如何匹配密码和确认密码。在这里,我将给出我的代码。我也使用 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,
),
Run Code Online (Sandbox Code Playgroud)
Pra*_*ndo 25
使用包含内置验证器的 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()
Run Code Online (Sandbox Code Playgroud)
小智 5
只需先声明一个变量,然后将其值分配给我们正在使用的值即可。
然后往下比较如下图:
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),
),
),
),
),
),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15506 次 |
| 最近记录: |