Flutter TextFormField 验证器

Flutter TextFormField validator

我有问题,有人知道如何将来自验证器的错误消息放在 AlertDialog 或带有 'ok' 按钮的弹出窗口 window 上以关闭弹出窗口 window. 此错误有 return =>

The return type 'AlertDialog' isn't a 'String', as defined by anonymous closure.

                              Align(
                                  alignment: Alignment.centerLeft,
                                  child: Padding(
                                    padding: EdgeInsets.fromLTRB(18, 22, 0, 4),
                                    child: Text(
                                      "Code Postal",
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 16),
                                    ),
                                  )),
                              Align(
                                  alignment: Alignment.centerLeft,
                                  child: Container(
                                    height:
                                        MediaQuery.of(context).size.height / 13,
                                    width:
                                        MediaQuery.of(context).size.width / 1.5,
                                    decoration: BoxDecoration(
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(10.0))),
                                    padding: EdgeInsets.fromLTRB(18, 0, 18, 0),
                                    child: TextFormField(
                                      controller: codePostalController,
                                      onChanged: (value) {
                                        setState(() {
                                          codePostal = value;
                                        });
                                      },
                                      validator: (value) => value.length != 5
                                          ? AlertDialog(content: Text('Postal Code must be five digits.'))
                                          : null,
                                      keyboardType: TextInputType.number,
                                      decoration: InputDecoration(
                                        contentPadding: EdgeInsets.symmetric(
                                            vertical: 0, horizontal: 10),
                                        hintStyle: TextStyle(
                                            color: Color.fromRGBO(
                                                133, 133, 133, 1.0),
                                            fontSize: 16),
                                        border: OutlineInputBorder(
                                          borderSide: BorderSide.none,
                                          borderRadius:
                                              BorderRadius.circular(10.0),
                                        ),
                                        suffixIcon: Icon(Icons.search,
                                            color: Color.fromRGBO(
                                                133, 133, 133, 1.0)),
                                        hintText: 'Code postal',
                                        fillColor:
                                            Color.fromRGBO(40, 40, 40, 1.0),
                                        filled: true,
                                      ),
                                    ),
                                  )),

那是因为你 return 在你应该 return String 的时候 Dialog

替换这个

validator: (value) => value.length != 5
  ? AlertDialog(content: Text('Postal Code must be five digits.'))
  : null,

有了这个

validator: (value) => value.length != 5
  ? 'Postal Code must be five digits.'
  : null,

如果您想显示 AlertDialog,请使用 validator 中的 showDialog() 方法,例如:

validator: (value) {
  if (value.length != 5) {
    showDialog(context: context, builder: (_) => AlertDialog(title: Text("Error")));
  }
  return null;
}