Free Ebook cover How to create apps from scratch to advanced using Flutter and Dart complete course

How to create apps from scratch to advanced using Flutter and Dart complete course

5

(4)

267 pages

Working with forms in Flutter

Capítulo 148

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

Working with forms is an essential part of application development. In Flutter, this task is simplified thanks to the Flutter Form package. This package provides a number of widgets and classes that help handle form validation, handling input events, and displaying error messages.

Before we dive into working with forms, it's important to understand what a form is. In simple terms, a form is a collection of input fields that collect information from the user. In a Flutter app, a form is represented by the Form class, which is a container widget for grouping and validating multiple form fields.

To start working with forms in Flutter, we first need to create an instance of a Form. This can be done as follows:

Form(
  key: _formKey,
  child: Column(
    children: [
      // your form fields go here
    ],
  ),
)

In this example, _formKey is a GlobalKey that helps us identify our Form for validation.

The next step is to add form fields to our Form. There are several types of form fields that we can use, such as TextFormField, Checkbox, Radio, Switch, Slider, etc. Let's see how we can add a TextFormField to our Form:

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

TextFormField(
  decoration: InputDecoration(labelText: 'Name'),
  validator: (value) {
    if (value.isEmpty) {
      return 'Please enter your name';
    }
    return null;
  },
)

Here, we provide a validation function that checks whether the field is empty. If it is empty, it returns an error message; otherwise, it returns null.

In addition to validating user input, we can also handle input events. For example, we can use the onChanged argument of a TextFormField to update our widget's state whenever the form field's value changes:

TextFormField(
  decoration: InputDecoration(labelText: 'Name'),
  onChanged: (value) {
    setState(() {
      _name = value;
    });
  },
)

Here, whenever the user types something into the form field, the onChanged function is called and the state of the widget is updated.

Finally, to present error messages to the user, we can use our Form's validate method. This method loops through all of the Form's form fields and calls their validation functions. If any of the validation functions return a string (ie an error message), that string will be displayed below the corresponding form field.

if (_formKey.currentState.validate()) {
  // if the form is valid, do something
} else {
  // otherwise display error messages
}

In summary, working with forms in Flutter is a simple task, thanks to the Flutter Form package. This package provides a number of widgets and classes that help us handle form validation, handling input events, and displaying error messages. With a little practice, you'll be able to create complex forms with ease.

Now answer the exercise about the content:

What is a form in the context of Flutter app development and how is it represented?

You are right! Congratulations, now go to the next page

You missed! Try again.

In Flutter, a form is a collection of input fields used to gather user information and is represented by the Form class, serving as a container to group and validate these fields.

Next chapter

Working with forms in Flutter: Creating a basic form

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.