Flutter has widgets for UI and we use Form and Field widgets today for creating a log in form. So first you need to look Flutter input widgets page. It has Form widget and FormField widget.

First, let’s create a Flutter project and cleanup starter project codes in main.dart file. Next, create a LoginPage class and _LoginPageState class in main.dart file. And add a appbar and sample text for showing the application.

Let’s look my code in this step.

Let’s give it a shot.

Step #1

Now, we are ready to add a form in this page. Let’s create a Form but be careful because we need a key for this Form because we will use this widget state in our class. So if you look at the Form Widget documents we have key property. So, we can use this for giving id of widget.

Now, we are ready to using this form but we need a class for passing your field values. So, let’s create a _LoginData class with email and password properties. And add our field widgets and a submit button.

Yes, we have UI components now. Let’s give it a shot.

Step #3

It’s cool. So we have UI components now but we have not any data or functionality for log in process. So, first we should validate this inputs for taking correct value. Flutter FormField widget has validator property. It’s so simple. This property need a function which return error string or null. And this function taking a input which is value of form field. So, we have 2 inputs which are email and password. I’m creating a two function for validating these fields and I’m using validate package for checking the value.

Yes, we have validation functions now. So, we can validate the fields and save these field values to LoginData class. Finally, we need to add a handler function for submit button. We need to validate our form in this function and if everything okey, we can send our data where you want.

Let’s give it a shot.

Final

If you enter correct values, you can see this data in console.

Console

That’s good.

Source: https://flutter-news.com/tutorials/forms-in-flutter.4