我无法验证确认密码文本字段

时间:2019-03-03 15:53:49

标签: dart flutter

我无法验证确认密码TextFormField,这是我的代码有问题:

Widget _buildPasswordTextField() {
return Padding(
  padding: EdgeInsets.symmetric(horizontal: 13.0),
  child: TextFormField(
    textAlign: TextAlign.right,
    style: TextStyle(color: Colors.white),
    decoration: InputDecoration(
      contentPadding: EdgeInsets.only(top: 30.0, left: 30.0),
      border: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.white),
        borderRadius: BorderRadius.all(
          Radius.circular(50.0),
        ),
      ),
      prefixIcon: Icon(
        Icons.visibility_off,
        color: Colors.white,
        size: 15.0,
      ),
      hintText: '**********',
      hintStyle: TextStyle(color: Colors.white, fontSize: 12.0),
      filled: true,
      fillColor: Colors.white.withOpacity(0.2),
    ),
    obscureText: true,
    controller: _passwordTextController,
    validator: (String value) {
      if (value.isEmpty || value.length < 6) {
        return '**********';
      }
    },
    onSaved: (String value) {
      _formData['password'] = value;
      },
    ),
  );
}

Widget _buildPasswordConfirmTextField() {
return Padding(
  padding: EdgeInsets.symmetric(horizontal: 13.0),
  child: TextFormField(
    textAlign: TextAlign.right,
    style: TextStyle(color: Colors.white),
    decoration: InputDecoration(
      contentPadding: EdgeInsets.only(top: 30.0, left: 30.0),
      border: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.white),
        borderRadius: BorderRadius.all(
          Radius.circular(50.0),
        ),
      ),
      prefixIcon: Icon(
        Icons.visibility_off,
        color: Colors.white,
        size: 15.0,
      ),
      hintText: '*********',
      hintStyle: TextStyle(color: Colors.white, fontSize: 12.0),
      filled: true,
      fillColor: Colors.white.withOpacity(0.2),
    ),
    obscureText: true,
    validator: (String value) {
      if (_passwordTextController.text != value) {
        return '*****************';
        }
      },
    ),
  );
}

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您必须用TextFormField小部件包装Form,然后为表单设置密钥,最后在formKey.currentState.validate()之前调用_formData['password'] = value;

喜欢

    final GlobalKey<FormState> formkey = GlobalKey<FormState>();

    Widget _buildPasswordTextField() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 13.0),
       child: Form(key: formkey,
      child: TextFormField(
      textAlign: TextAlign.right,
      style: TextStyle(color: Colors.white),
      decoration: InputDecoration(
        contentPadding: EdgeInsets.only(top: 30.0, left: 30.0),
        border: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.white),
          borderRadius: BorderRadius.all(
            Radius.circular(50.0),
          ),
        ),
        prefixIcon: Icon(
          Icons.visibility_off,
          color: Colors.white,
          size: 15.0,
        ),
        hintText: '**********',
        hintStyle: TextStyle(color: Colors.white, fontSize: 12.0),
        filled: true,
        fillColor: Colors.white.withOpacity(0.2),
      ),
      obscureText: true,

      validator: (String value) {
        if (value.isEmpty || value.length < 6) {
          return '**********';
        }
      },
      onSaved: (String value) {
     if(formkey.currentState.validate()){
     _formData['password'] = value;
     }
      },
    ),
  ),
    );
    }

您可以通过使用autovalidate: true ;来实现此目的,而无需为您的Form设置密钥。