Flutter:密码检查
Flutter: Password Check
我会向用户输入密码2次。但我不知道如何检查这个。我如何确定两个密码的拼写相同?我可以这样做吗?
这是我的代码,(为了不长,我只是把相关的部分放上去)
class _RegisterPageState extends State<RegisterPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
_paddingPasswordWidget('Password', 'Password'),
_paddingPasswordWidget('Password Again', 'Password Again'),
],
),
),
),
);
}
}
_paddingPasswordWidget(String hintTextStr, String labelTextStr) {
return Padding(
padding: EdgeInsets.only(top: 15, left: 22, right: 22),
child: TextFormField(
keyboardType: TextInputType.text,
style: TextStyle(
color: HexColor('#868686'),
),
decoration: CommonInputStyle.textFieldStyle(
hintTextStr: hintTextStr,
labelTextStr: labelTextStr,
),
obscureText: true,
),
);
}
class CommonInputStyle {
static InputDecoration textFieldStyle(
{String labelTextStr = "", String hintTextStr = ""}) {
return InputDecoration(
contentPadding: EdgeInsets.only(left: 20, top: 5, bottom: 5, right: 20),
labelText: labelTextStr,
hintText: hintTextStr,
labelStyle: TextStyle(fontSize: 14, color: HexColor('#868686')),
hintStyle: TextStyle(fontSize: 14, color: HexColor('#868686')),
filled: true,
fillColor: HexColor('#EEF2F4'),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide.none,
),
);
}
}
您可以尝试将 textEditingController 放在您的字段中,然后比较它们。像这样:
var textFieldPasswordController = TextEditingController();
var textFieldConfirmPasswordController = TextEditingController();
然后将它们作为控制器传递到 TextFormField 中:
TextFormField(
controller: textFieldPasswordController //as an example
keyboardType: TextInputType.text,
style: TextStyle(
color: HexColor('#868686'),
),
现在您只需要检查 textFieldPasswordController.text 是否等于 textFieldConfirmPasswordController.text。您可以在 TextFormField
的 onChange 或 validator 函数中进行检查
if(textFieldPasswordController.text == textFieldConfirmPasswordController.text){
print("Access granted");
} else{
print("Try again");
}
我会向用户输入密码2次。但我不知道如何检查这个。我如何确定两个密码的拼写相同?我可以这样做吗?
这是我的代码,(为了不长,我只是把相关的部分放上去)
class _RegisterPageState extends State<RegisterPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
_paddingPasswordWidget('Password', 'Password'),
_paddingPasswordWidget('Password Again', 'Password Again'),
],
),
),
),
);
}
}
_paddingPasswordWidget(String hintTextStr, String labelTextStr) {
return Padding(
padding: EdgeInsets.only(top: 15, left: 22, right: 22),
child: TextFormField(
keyboardType: TextInputType.text,
style: TextStyle(
color: HexColor('#868686'),
),
decoration: CommonInputStyle.textFieldStyle(
hintTextStr: hintTextStr,
labelTextStr: labelTextStr,
),
obscureText: true,
),
);
}
class CommonInputStyle {
static InputDecoration textFieldStyle(
{String labelTextStr = "", String hintTextStr = ""}) {
return InputDecoration(
contentPadding: EdgeInsets.only(left: 20, top: 5, bottom: 5, right: 20),
labelText: labelTextStr,
hintText: hintTextStr,
labelStyle: TextStyle(fontSize: 14, color: HexColor('#868686')),
hintStyle: TextStyle(fontSize: 14, color: HexColor('#868686')),
filled: true,
fillColor: HexColor('#EEF2F4'),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide.none,
),
);
}
}
您可以尝试将 textEditingController 放在您的字段中,然后比较它们。像这样:
var textFieldPasswordController = TextEditingController();
var textFieldConfirmPasswordController = TextEditingController();
然后将它们作为控制器传递到 TextFormField 中:
TextFormField(
controller: textFieldPasswordController //as an example
keyboardType: TextInputType.text,
style: TextStyle(
color: HexColor('#868686'),
),
现在您只需要检查 textFieldPasswordController.text 是否等于 textFieldConfirmPasswordController.text。您可以在 TextFormField
的 onChange 或 validator 函数中进行检查if(textFieldPasswordController.text == textFieldConfirmPasswordController.text){
print("Access granted");
} else{
print("Try again");
}