flutter - Flutter:密码检查
问题描述
我将向用户输入密码 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 或验证器函数中检查这一点
if(textFieldPasswordController.text == textFieldConfirmPasswordController.text){
print("Access granted");
} else{
print("Try again");
}
推荐阅读
- centos7 - 如何修复“wkhtmltopdf”字体配置错误
- linux - 为什么运行`strace echo "hello world" | grep write`显示完整的strace输出?
- amazon-web-services - 有没有办法解决 AWS DynamoDB 中的嵌套属性以用于 documentClient.query() 调用?
- go - 覆盖错误。Is() 不适用于自定义错误
- maven - 简单的 Java Mail 依赖项不在 jar 文件中
- ruby-on-rails - 将 ruby on rails 添加到现有的 react native 应用程序
- node.js - 为 MongoDB 集合中的每个文档设置具有不同值的新字段
- html - Angular 中的 Bootstrap Essentials(Bootstrap 扩展)“不起作用”
- swift - 类实例的内存访问
- javascript - 尝试使用 document.queryselector 将事件侦听器添加到 onclick 事件不起作用