0

我将向用户输入密码 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,
      ),
    );
  }
}
4

1 回答 1

1

您可以尝试将 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");
}
于 2020-09-16T15:27:48.180 回答