0

我正在尝试迁移到flutter_hooks,但无法进行一些简单的 textformField 验证。

我有 2 个文本字段和一个按钮,我想在某些情况下显示错误(或者至少在文本字段为空时)

我的代码:-控制器:

final _emailFieldController =
        useTextEditingController.fromValue(TextEditingValue.empty);

    final _password =
        useTextEditingController.fromValue(TextEditingValue.empty);
    final _onSavePressed = useState(false);

-小部件:

TextFormField(
                  decoration: InputDecoration(hintText: "Your email"),
                  controller: _emailFieldController,
                ),
                TextFormField(
                  obscureText: true,
                  onSaved: (newValue) => print(newValue),
                  validator: (value) =>
                      _onSavePressed.value && _password.text.isEmpty
                          ? "Can't be empty"
                          : null,
                  decoration: InputDecoration(
                    hintText: "Password",
                  ),
                  controller: _password,
                ),
                RaisedButton(
                  child: Text("Create"),
                  onPressed: () {
                    _onSavePressed.value = true;
                  },
                )

谢谢您的帮助!

4

1 回答 1

1

使用value从验证器获得的。例如

validator: (value) => value.isEmpty ? "Can't be empty" : null

使您可以访问密码字段的值。

于 2020-09-30T18:55:54.480 回答