Flutter & Firebase App Tutorial — Part 8 toggle between forms and form validation
Create a flutter authenticate app in 10 mins by using firebase as the back-end. You will learn how to toggle between widgets and direct users to the right screen. After that, use the Flutter built-in validation features to make sure you collect the right data you need.
More Tutorials:
- Flutter & Firebase App Tutorial — Part 1 Get Started
- Flutter & Firebase App Tutorial — Part 2 App Structure
- Flutter & Firebase App Tutorial — Part 3 Firebase Sign In Anonymously
- Flutter & Firebase App Tutorial — Part 4 Custom model in Flutter App
- Flutter & Firebase App Tutorial — Part 5 Stream & Provider in Flutter
- Flutter & Firebase App Tutorial — Part 6 Firebase Sign Out
- Flutter & Firebase App Tutorial — Part 7 Firebase Sign In and Register With Email
- Flutter & Firebase App Tutorial — Part 8 toggle between forms and form validation
- Flutter & Firebase App Tutorial — Part 9 loading Spinkit and theme & layout
- Flutter Animation Tutorial
- Using SQLite In Flutter
These are our dart files and widgets so far. Now, we will show users either the Sign In
or Sign Up
widget while toggling between forms.
Table of Contents
- Toggle between forms
- Flutter built-in form validation feature
Step 1️⃣: Go to the Authenticate.dart
file
Step 2️⃣: Add the logic to show either SignIn
widget orSignUp
widget
✅ Add a boolean property called showSignIn.
If it’s true, we show theSignIn
widget. While if it’s false, we show theSignUp
widget.
✅Add a function called toggelView()
in the Authenticate.dart
file to toggle the showSignIn
property from true to false or vice versa.
✅ Call the function toggelView()
from SignIn
widget by passing down toggelView()
as a parameter
Step 3️⃣: Go toSignIn
widget
✅ Add a final function called toggelView()
inside the SignIn
widget itself
✅ Create a constructor of the widget that accepts toggelView()
parameter and set the parameter equal to the final function called toggelView()
✅ Add a button on the top right inside the appBar.
The onPressed
function inside the button triggers the widget’s toggelView()
function.
✅ Do the same toSignUp
widget
Be default, the app will show the SignIn
widget since the showSignIn
property is set to true in the Authenticate.dart
file. Once users click Sign up
inside the appBar
in the SignIn
widget, the toggelView()
function is activated and then the value of showSignIn
property is set to false and consequently, the app shows SignUp
widget
Build a form with validation
Since the login form needs users to enter email and password, in order to make the app secure, we check the information that users provide is valid or not.
👉 if users correctly fill out the form, we let users log in.
👉if users submit incorrect information, we display an error message.
Step 1️⃣: Create a Form
with a GlobalKey
Using Form widget to handle or manipulate the fields for validation, submitting, and resetting.
✅When creating the form widget, we define a form key _formKey
that is a global key of type FormState.
The is
GlobalKey
a unique identifier and it helps to allow validation of the form in a later step.
// declared the formKey as a field in our State object
final _formKey = GlobalKey<FormState>();child: Form(
key: _formKey,
);
Step 2️⃣: add the _formKey
to the form widget to associate the form with the global FormState
key, which will keep track of the form widget and the state of the form. In the future, you can validate the form via this _formKey
because you can access the validation features and states from this _formKey.
For example, TextFormField
has a vlidator
that takes a function to check if the value of the TextFormField
is an empty string.
TextFormField(
// The validator receives the text that the user has entered.
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Step 3️⃣: Create a button to validate and submit the login form
When submitting a form, it checks whether the form is valid. If it is, it displays a success message. if it’s not, it displays an error message
ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid
if (_formKey.currentState!.validate()) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('a success message')),
);
}
},
child: const Text('Submit'),
),
✅ How does this work?
The _formKey
has a method called_formKey.currentState()
that can be used to access the FormState
which is automatically created by Flutter when building a form.
The FormState
has a validate()
method. When validate()
method is called, it executes the validator()
function for each text field in the form. If any text field has errors, the validate()
method rebuilds the form to show the error message and return false
.
More Tutorials:
- Flutter & Firebase App Tutorial — Part 1 Get Started
- Flutter & Firebase App Tutorial — Part 2 App Structure
- Flutter & Firebase App Tutorial — Part 3 Firebase Sign In Anonymously
- Flutter & Firebase App Tutorial — Part 4 Custom model in Flutter App
- Flutter & Firebase App Tutorial — Part 5 Stream & Provider in Flutter
- Flutter & Firebase App Tutorial — Part 6 Firebase Sign Out
- Flutter & Firebase App Tutorial — Part 7 Firebase Sign In and Register With Email
- Flutter & Firebase App Tutorial — Part 8 toggle between forms and form validation
- Flutter & Firebase App Tutorial — Part 9 loading Spinkit and theme & layout
- Flutter Animation Tutorial
- Using SQLite In Flutter
Seeing my followers grow will encourage me to write more articles. If you have any questions or anything to be improved please write a comment in the comments section.
I am a Flutter lover, and I talk about flutter, mobile development, and artificial intelligence.
👉 The source code is updated in Github