Login Interface using Flutter - Cross Platform
Creating a Login UI Using Flutter
Flutter is a powerful framework for building cross-platform mobile applications. It provides a wide range of widgets and tools that make UI development straightforward and efficient. In this article, we will create a comprehensive login user interface (UI) using Flutter, detailing each step to help you understand the process thoroughly.
Prerequisites
Before we begin, ensure you have the following:
- Flutter SDK installed on your machine. Follow the official Flutter installation guide.
- An IDE like Visual Studio Code or Android Studio set up for Flutter development.
- Basic knowledge of Dart programming language.
Project Setup
Let's start by creating a new Flutter project. Open your terminal and run:
flutter create login_ui
Navigate into your project directory:
cd login_ui
Designing the Login UI
We will design a clean and simple login screen with the following elements:
- A title
- Email input field
- Password input field
- Login button
Open the lib/main.dart file and replace its content with the following code:
```dart
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Login UI',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginScreen(),
);
}
}
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
class _LoginScreenState extends State {
final _formKey = GlobalKey();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
void _login() {
if (_formKey.currentState.validate()) {
// Perform login action
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Logging in...')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Login',
style: TextStyle(
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 32.0),
TextFormField(
controller: _emailController,
decoration: InputDecoration(
labelText: 'Email',
border: OutlineInputBorder(),
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
return 'Please enter a valid email';
}
return null;
},
),
SizedBox(height: 16.0),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
border: OutlineInputBorder(),
),
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 6) {
return 'Password must be at least 6 characters long';
}
return null;
},
),
SizedBox(height: 32.0),
ElevatedButton(
onPressed: _login,
child: Text('Login'),
),
],
),
),
),
);
}
}
```
Code Explanation
Let’s delve into the code and understand the key components:
MyApp Widget
The MyApp widget is the root of the application. It sets up a MaterialApp with a theme and specifies the home screen as LoginScreen:
```dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Login UI',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: LoginScreen(),
);
}
}
```
LoginScreen Stateful Widget
The LoginScreen is a stateful widget that manages the state of the login form. We use a GlobalKey to identify the form and validate user input:
```dart
class LoginScreen extends StatefulWidget {
@override
_LoginScreenState createState() => _LoginScreenState();
}
```
_LoginScreenState
The _LoginScreenState class holds the state of the LoginScreen. It includes text controllers for managing input fields and a method to handle the login action:
```dart
class _LoginScreenState extends State {
final _formKey = GlobalKey();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
void _login() {
if (_formKey.currentState.validate()) {
// Perform login action
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Logging in...')),
);
}
}
}
```
Form Widget
We use a Form widget to group and validate the email and password fields:
```dart
Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// Text fields and button here
],
),
);
```
TextFormField Widgets
Two TextFormField widgets are used for the email and password input fields. They include validation logic to ensure the input is valid:
```dart
TextFormField(
controller: _emailController,
decoration: InputDecoration(
labelText: 'Email',
border: OutlineInputBorder(),
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your email';
}
if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
return 'Please enter a valid email';
}
return null;
},
),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(
labelText: 'Password',
border: OutlineInputBorder(),
),
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return 'Please enter your password';
}
if (value.length < 6) {
return 'Password must be at least 6 characters long';
}
return null;
},
),```
Login Button
The ElevatedButton triggers the login action when pressed. It calls the _login method, which validates the form and displays a snackbar:
```dart ElevatedButton( onPressed: _login, child: Text('Login'), ); ```
Running the Application
To run the application, use the following command in your terminal:
flutter run
Make sure you have an emulator running or a physical device connected. The login UI should appear on the screen.
Conclusion
In this in-depth tutorial, we covered the creation of a basic login UI using Flutter. We walked through the setup, design, and implementation of the login form, providing detailed explanations for each step. This foundation can be expanded to include additional features such as password recovery, registration, and backend integration to handle authentication.
Flutter's comprehensive widget library and flexible layout system make it an excellent choice for building beautiful and responsive UIs. We hope this guide helps you get started with your own Flutter projects. Happy coding!