Flutter submit form on enter

To enable submitting a form in Flutter when the user presses the enter key on the keyboard, you can use the following steps:

  1. Wrap your form with a GestureDetector widget:
  2. 
    GestureDetector(
      onTap: () {
        // FocusScope.of(context).requestFocus(new FocusNode());
        // Close the keyboard when the user taps outside the form fields
      },
      child: Form(
        key: _formKey,
        child: Column(
          children: [
            // Form fields
          ],
        ),
      ),
    )
    

    The GestureDetector allows you to handle events like tapping, but in this case, we only need it to listen to taps outside the form fields to close the keyboard.

  3. Handle the text field submissions:
  4. 
    TextEditingController _textFieldController = TextEditingController();
    
    ...
    
    TextFormField(
      controller: _textFieldController,
      onFieldSubmitted: (value) {
        // Handle the submission
      },
    )
    

    The TextFormField widget has an onFieldSubmitted property that takes a callback function. This callback will be called when the user submits the field, either by pressing the enter key or by tapping the submit button on the keyboard.

  5. Handle the form submission:
  6. 
    Form(
      key: _formKey,
      child: Column(
        children: [
          // Form fields
          RaisedButton(
            onPressed: () {
              if (_formKey.currentState.validate()) {
                // All form fields are valid, submit the form
                _formKey.currentState.save();
                // Handle the form submission
              }
            },
            child: Text('Submit'),
          ),
        ],
      ),
    )
    

    The Form widget has a key property that allows you to access its state. The _formKey.currentState.validate() method is used to check if all the form fields are valid. If they are, the _formKey.currentState.save() method is called to save the form data. You can then handle the form submission inside the onPressed callback of the submit button.

Here’s the complete example showcasing the above steps:


import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final GlobalKey _formKey = GlobalKey();
  TextEditingController _textFieldController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Submit Form on Enter'),
        ),
        body: GestureDetector(
          onTap: () {
            FocusScope.of(context).requestFocus(new FocusNode());
          },
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    controller: _textFieldController,
                    decoration: InputDecoration(
                      labelText: 'Enter a value',
                    ),
                    onFieldSubmitted: (value) {
                      if (_formKey.currentState.validate()) {
                        _formKey.currentState.save();
                        _handleSubmit();
                      }
                    },
                  ),
                ),
                RaisedButton(
                  onPressed: () {
                    if (_formKey.currentState.validate()) {
                      _formKey.currentState.save();
                      _handleSubmit();
                    }
                  },
                  child: Text('Submit'),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _handleSubmit() {
    // Handle the form submission
    print('Submitted value: ${_textFieldController.text}');
  }
}

After running this application, you can enter a value in the text field, press the enter key on the keyboard, and the form will be submitted, printing the value in the console.

Leave a comment