Flatter TextFormField validator - PullRequest
       4

Flatter TextFormField validator

0 голосов
/ 22 января 2020

У меня проблема, кто-то знает, как поместить сообщение об ошибке из валидатора в AlertDialog или всплывающее окно с кнопкой «ОК», чтобы закрыть всплывающее окно. Эта ошибка имеет return =>

Тип возврата 'AlertDialog' не является 'String', как определено анонимным закрытием.

                              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 Ответ

1 голос
/ 22 января 2020

Это потому, что вы возвращаете 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;
}
...