User authentication plays a crucial role in mobile applications to ensure security and provide personalized experiences. With the growing popularity of the Flutter framework, creating a secure and visually appealing login form has become a breeze.
Flutter Login Form Example
This tutorial will guide you through the process of building a stunning Flutter login form example, offering insights into the code structure and implementation details. By the end of this guide, you’ll be equipped with the knowledge to create a secure, functional, and stylish login form for your Flutter applications.
Setting up the Flutter Environment
To start, make sure you have the latest version of Flutter installed on your computer. You can check the installation and version by running the command “flutter –version” in your terminal. If you haven’t installed Flutter yet, follow the official documentation at https://flutter.dev/docs/get-started/install to set it up.
Creating a New Flutter Project
Open your terminal, navigate to your preferred directory, and create a new Flutter project with the following command:
flutter create flutter_login_form_example
Then, navigate to the project directory using:
cd flutter_login_form_example
Setting Up Dependencies
Before diving into the code, add the necessary dependencies to your pubspec.yaml
file. For this tutorial, we’ll use the “flutter_bloc” package for state management and the “email_validator” package for validating email addresses. Add the following lines under the “dependencies” section:
dependencies:
flutter:
sdk: flutter
flutter_bloc: ^8.0.1
email_validator: ^2.0.1
Don’t forget to run “flutter pub get” to fetch the dependencies.
Related Content, Rate My app Flutter Example.
Creating the Login Form
In your “lib” folder, create a new Dart file named “login_form.dart.” Inside this file, import the necessary packages and start building the login form widget:
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:email_validator/email_validator.dart';
class LoginForm extends StatefulWidget {
@override
_LoginFormState createState() => _LoginFormState();
}
class _LoginFormState extends State<LoginForm> {
final _formKey = GlobalKey<FormState>();
String _email = '';
String _password = '';
@override
Widget build(BuildContext context) {
return Form(
key: _formKey,
child: Column(
children: <Widget>[
// Email TextFormField
// Password TextFormField
// Submit Button
],
),
);
}
}
Adding Email and Password Fields:
Now, add the email and password TextFormFields to the form. Use the “validator” property to ensure that both fields are not empty and the email is valid:
TextFormField(
decoration: InputDecoration(labelText: 'Email'),
keyboardType: TextInputType.emailAddress,
onChanged: (value) => setState(() => _email = value.trim()),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter your email';
} else if (!EmailValidator.validate(value)) {
return 'Please enter a valid email';
}
return null;
},
),
TextFormField(
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
onChanged: (value) => setState(() => _password = value.trim()),
validator: (value) {
if (value == null || value.isEmpty) {
return '
Please enter your password';
}
return null;
},
),
Adding the Submit Button:
Create the submit button and set its “onPressed” property to validate and process the form data:
ElevatedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
// Process login data
print('Email: $_email, Password: $_password');
}
},
child: Text('Submit'),
),
Implementing the Login Form in the Main Screen:
Open the “main.dart” file and replace its content with the following code to implement the LoginForm widget:
import 'package:flutter/material.dart';
import 'package:flutter_login_form_example/login_form.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Login Form Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Login Form Example'),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: LoginForm(),
),
),
);
}
}
Running the Application:
Now, run the application in your preferred emulator or physical device using the “flutter run” command. You should see the login form displayed on the main screen, complete with email and password fields and a submit button.
Read also, Flutter staggered Animations Example.
Conclusion
In this tutorial, you’ve learned how to create a beautiful and functional login form using Flutter. With this knowledge, you can now enhance the user experience of your applications by providing secure and visually appealing authentication. The example code given here can be easily customized to match the design and requirements of your specific project. So go ahead, implement this login form in your Flutter apps, and take your user experience to the next level!