Flutter:密码检查

问题描述

我将两次向用户输入密码。但我不知道如何检查。如何确定两个密码的拼写相同?我可以这样做吗?

这是我的代码,(为了避免篇幅太长,我只放置了相关部分)

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,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");
}