flutter 验证口令和确认口令

dfty9e19  于 2023-02-25  发布在  Flutter
关注(0)|答案(1)|浏览(140)

我们可以用这个方法验证密码和确认密码吗?

SizedBox(
  height: 55,
  child: MyTextField(
      controller: TextEditingController(text: user.password),
      validator: _validatePassword,
      hintText: 'Password',
      obsecureText: true,
      icon: const Icon(null)),
),
const SizedBox(
  height: 15,
),
SizedBox(
  height: 55,
  child: MyTextField(
      controller: TextEditingController(text: user.confirmPassword),
      validator: (val) {
        if (val!.isEmpty) {
          return 'Empty';
        } else if (val != user.password) {
          return 'Not Match';
        }
      },
      hintText: 'Confirm Password',
      obsecureText: true,
      icon: const Icon(null)),
),
const SizedBox(height: 30),

我尝试使用控制器执行此操作,但无法将其与API调用链接

0sgqnhkj

0sgqnhkj1#

您可以为每个控件创建两个单独的不同控件,然后检查它们是否具有相同的文本。

final passWordController = TextEditingController();

final confirmPassWordController = TextEditingController();
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Column(
      children: [
        SizedBox(
          height: 55,
          child: TextFormField(
            controller: passWordController,
            validator: (value) {
                // .... others validator
                if (passWordController.text ==
                    confirmPassWordController.text) {
                  return "password didnt match";
                }
            },
          ),
        ),
        const SizedBox(
          height: 15,
        ),
        SizedBox(
          height: 55,
          child: TextFormField(
              controller: confirmPassWordController,
              validator: (val) {
                
              // .... others validator
                if (passWordController.text ==
                    confirmPassWordController.text) {
                  return "password didnt match";
                }
              }),
        ),
        const SizedBox(height: 30),
      ],
    ),
  );
}

相关问题