Валидатор TextFormField, ошибка не видна в TextFormField - PullRequest
0 голосов
/ 29 ноября 2018

Я пытаюсь создать форму с колонкой.

Например, Form => Column => TextFields (s),

TextFields будет иметь валидаторы.Кроме того, TextFields будет иметь украшение.

Но когда я нажимаю на RaisedButton, я не могу найти ошибку в TextField.Хотя возврат в TextFormField выполняется успешно. (я проверял с помощью отладчика)

Ссылки, которые я упоминал до сих пор, следующие: https://www.youtube.com/watch?v=xiEYNzU4bDg

https://iirokrankka.com/2017/10/17/validating-forms-in-flutter/

https://medium.com/@nitishk72/form-validation-in-flutter-d762fbc9212c

Ниже приведен мой код с просьбой помочь мне исправить это

import 'package:flutter/material.dart';

class Profile extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new ProfileState();
  } //createState closes here....
} //Profile closes here.....

class ProfileState extends State<Profile> {
  @override
  Widget build(BuildContext context) {
    final _formKey = GlobalKey<FormState>();

    return new Scaffold(
        appBar: new AppBar(title: new Text("Profile")),
        body: Form(
          key: _formKey,
          child: new Column(
            children: <Widget>[
              //Full Name TextGormFeild goes below....
              new TextFormField(
                  validator: (String value) {
                    if (value.isEmpty) {
                      return "Full name cannot be empty.";//Control comes here when I check using the Debugger.
                    } //if(value.isEmpty) closes here....
                  },
                  keyboardType: TextInputType.text,
                  textInputAction: TextInputAction.next,
                  maxLines: 1,
                  decoration: InputDecoration(
                      border: new OutlineInputBorder(
                          borderSide: new BorderSide(color: Colors.grey)),
                      hintText: "Enter name")),

              new RaisedButton(
                child: new Text("Test"),
                onPressed: () {
                  setState(() {
                    if (_formKey.currentState.validate()) {
                      print("Validations are correct.");
                    }
                  });
                },
              )
            ],
          ),
        )

        //,
        // helperText: "Enter full name"
        );
  } //build closes here.....

} //ProfileState closes here....

Ранее я использовал helperText, но я его удалил.

1 Ответ

0 голосов
/ 29 ноября 2018

Попробуйте удалить setState с вас при нажатии

setState(() {

});

Положите только

if (_formKey.currentState.validate()) {
  print("Validations are correct.");
}

Как показано ниже:

onPressed: () {
    if (_formKey.currentState.validate()) {
      print("Validations are correct.");
    }
},
...