Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

Form required text field (Mailer,Flutter)

I explain to you I am trying at the moment making it compulsory to fill in information sending the information sent via mailer.

If it is possible to have an explanation so that I understand how adding the code to my form works.

Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Demande'),
  ),
  body: Container(
    color: Color(0xffd8edff),
    child: Column(
      children: <Widget>[
        Padding(
          padding: const EdgeInsets.all(8.0),
          child: Card(
            child: Column(
              children: <Widget>[
                Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: TextFormField(
                    controller: nomController,
                    decoration: InputDecoration(
                      labelText: 'Nom:',
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: TextFormField(
                    controller: prenomController,
                    decoration: InputDecoration(
                      labelText: 'Prénom:',
                    ),
                    keyboardType: TextInputType.name,
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: TextFormField(
                    controller: emailController,
                    decoration: InputDecoration(
                      labelText: 'E-mail:',
                      hintText: 'Ex: [email protected]',
                    ),
                  ),
                ),
                RaisedButton(
                  onPressed: () async {
                    showDialog(
                      context: context,
                      barrierDismissible: false,
                      builder: (BuildContext context) {
                        // return object of type Dialog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will want to wrap your entire form with a Form widget.

You can then pass a GlobalKey<FormState> into the Form to get a reference to it that you can use in other methods

You should create your form key in the initState() of your State class (make sure you are using a StatefulWidget).

Once you have done this, you can pass a validator to your TextFormFields. The validator takes a string, and returns null if the input is valid, or an error message string if the input is invalid.

Then, on the submit button, you can call formKey.currentState.validate(). This method returns false if any text field failed its validator.

For example:

GlobalKey<FormState> _formKey;


@override
void initState() {
    super.initState();
    _formKey = GlobalKey();
}

@override
Widget build(BuildContext context) {
    return Form(key: _formKey, child: Column(
        children: [
            TextFormField(
                validator: (value) {
                    if (value.isEmpty) return 'Please enter a value';
                    else return null;
                },
                controller: ...  // whatever controller you are using
            ),
            FlatButton(
                child: Text('Submit'),
                onPressed: () {
                    if (_formKey.currentState.validate()) {
                      //  if this returns true, all the text fields have got good data (i.e. all fields are non-empty)
                      doSomething();
                    } else {
                      // if the call to validate() returns false, the error messages will appear automatically.
                      // you can run any extra code you need here
                    }
                }
            )
        ]
    ));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...