In this article, I will guide you through the process of creating a login page in Flutter. As a Flutter developer myself, I have found that implementing a login page is an essential part of any mobile app. It allows users to securely access their accounts and protects the app’s data.
Using Flutter, you can build beautiful and functional login pages with ease. Whether you are developing an authentication system or just want to add a login screen to your app, Flutter provides all the necessary tools and widgets to make it happen.
Setting Up the Project
Before we dive into creating the login page, let’s set up our Flutter project. Open your preferred IDE, create a new Flutter project, and navigate to the project directory in your command line interface.
flutter create login_page
Once the project is set up, open the lib/main.dart
file and remove the default code. We will start from scratch to build our login page.
Creating the Login Page
First, we need to import the necessary packages and dependencies. In the pubspec.yaml
file, add the following dependencies:
dependencies:
flutter:
sdk: flutter
flutter_bloc: ^7.0.0
Save the file and run flutter pub get
to fetch the dependencies.
Now, let’s create the login page UI. In the lib/main.dart
file, add the following code:
import 'package:flutter/material.dart';
void main() {
runApp(LoginApp());
}
class LoginApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Login Page',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginPage(),
);
}
}
class LoginPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login Page'),
),
body: Center(
child: Text(
'Welcome to the Login Page!',
style: TextStyle(fontSize: 24),
),
),
);
}
}
Here, we’ve defined two classes: LoginApp
and LoginPage
. The LoginApp
class is the entry point of our app, where we define the app’s title and theme. The LoginPage
class represents the UI of the login page and contains a simple text widget for now.
Next, let’s enhance the login page UI by adding text fields for the user to enter their username and password:
class LoginPage extends StatelessWidget {
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Login Page'),
),
body: Padding(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextFormField(
controller: _usernameController,
decoration: InputDecoration(
labelText: 'Username',
),
),
SizedBox(height: 16),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
),
obscureText: true,
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () {
// TODO: Implement login functionality
},
child: Text('Login'),
),
],
),
),
);
}
}
Here, we’ve added two TextFormField
widgets for the username and password fields. We’ve also included an ElevatedButton
widget for the login button. Don’t forget to add the necessary controllers to retrieve the values entered by the user.
Implementing the Login Functionality
Now that we have our login page UI ready, let’s implement the login functionality. Replace the // TODO: Implement login functionality
comment with the following code:
onPressed: () {
String username = _usernameController.text;
String password = _passwordController.text;
if (username.isNotEmpty && password.isNotEmpty) {
// TODO: Add your login logic here
} else {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Error'),
content: Text('Please enter both username and password.'),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('OK'),
),
],
);
},
);
}
}
In this code snippet, we retrieve the values entered by the user from the TextEditingController
instances and perform basic validation to ensure both the username and password fields are filled. If the fields are empty, we display an error dialog using the showDialog
method.
Conclusion
That’s it! You have successfully created a login page in Flutter. With Flutter’s powerful widget system and flexiblity, you can customize and enhance your login page to fit the needs of your specific app.
Remember, the code provided in this article is just a starting point. You can further improve the login page by adding features like password encryption, biometric authentication, or integrating with a backend server for user authentication.
Happy coding!