Flutter TextFormField验证器

时间:2020-01-22 12:16:10

标签: validation flutter popup widget

我有问题,有人知道如何将来自验证器的错误消息放置在AlertDialog或带有“确定”按钮的弹出窗口中,以关闭弹出窗口。 该错误有return =>

返回类型“ AlertDialog”不是由匿名闭包定义的“字符串”。

                              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,
                                      ),
                                    ),
                                  )),

1 个答案:

答案 0 :(得分:1)

那是因为当您应该返回Dialog时将返回String

替换此

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,请在showDialog()中使用validator方法,例如:

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