Build Your First Mobile-App in Flutter
Posted on January 13, 2025 • 4 minutes • 800 words
What is Flutter?
Flutter is a UI toolkit that is open-source and created by Google, enabling developers to build natively compiled applications for various platforms using a single codebase.
It supports building apps for:
- Mobile: Android and iOS
- Web: Modern browsers
- Desktop: Windows, macOS, and Linux
- Embedded Devices
Key Features of Flutter
- Single Codebase: Write one codebase for all platforms.
- Hot Reload: Instantly see changes in your code without restarting the app.
- Widgets: Everything in Flutter is a widget, allowing for highly customizable UIs.
- Performance: Flutter apps are compiled into native ARM code, ensuring high performance.
- Material Design and Cupertino: Support for Google’s Material Design and Apple’s Cupertino design for cross-platform consistency.
- Open-Source: Free to use and has a vibrant community.
Hello World Application in Flutter
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Hello World App'),
),
body: Center(
child: Text('Hello, Flutter!'),
),
),
);
}
}
Flutter Widgets
Widgets are the foundation of Flutter’s UI. They are categorized into:
-
Stateless Widgets:
- Immutable.
- Their state cannot change after being created.
- Example:
Text
,Icon
.
-
Stateful Widgets:
- Have mutable state.
- Use a State object to track changes.
- Example:
Checkbox
,TextField
.
In this blog, we’ll walk you through creating a simple Flutter application with a login page and a home page.
The application will include navigation between these two screens and basic user input handling.
Prerequisites
Before you start, ensure the following:
-
Flutter SDK is installed Install Flutter
-
A code editor like
Visual Studio Code
orAndroid Studio
is set up.
Project Structure
We’ll create a Flutter application with the following files:
-
main.dart: The entry point of the application.
-
login_page.dart: The login screen.
-
home_page.dart: The home screen.
Step 1: Create a New Flutter Project
Run the following command in your terminal to create a new Flutter project:
flutter create login_home_app
cd login_home_app
Step 2: Set Up the Main File
In lib/main.dart
, update the code to configure routes for navigation between the login page and home page.
import 'package:flutter/material.dart';
import 'login_page.dart';
import 'home_page.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Login App',
initialRoute: '/',
routes: {
'/': (context) => LoginPage(),
'/home': (context) => HomePage(),
},
);
}
}
Step 3: Create the Login Page
Create a file named login_page.dart
in the lib folder and add the following code:
import 'package:flutter/material.dart';
class LoginPage extends StatefulWidget {
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
void _login() {
if (_usernameController.text == 'user' && _passwordController.text == 'password') {
Navigator.pushReplacementNamed(context, '/home');
} else {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Login Failed'),
content: Text('Invalid username or password'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text('OK'),
),
],
),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Login Page')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _usernameController,
decoration: InputDecoration(labelText: 'Username'),
),
TextField(
controller: _passwordController,
decoration: InputDecoration(labelText: 'Password'),
obscureText: true,
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _login,
child: Text('Login'),
),
],
),
),
);
}
}
Step 4: Create the Home Page
Create a file named home_page.dart
in the lib folder and add the following code:
import 'package:flutter/material.dart';
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Home Page'),
actions: [
IconButton(
icon: Icon(Icons.logout),
onPressed: () => Navigator.pushReplacementNamed(context, '/'),
),
],
),
body: Center(
child: Text(
'Welcome to the Home Page!',
style: TextStyle(fontSize: 24),
),
),
);
}
}
Step 5: Run the Application
Use the following command to run your app:
flutter run
When you launch the app:
-
You’ll see the Login Page.
-
Enter the username as
user
and password aspassword
to navigate to the Home Page. -
You can log out from the Home Page by clicking the
logout
button in the app bar.
You have successfully built a simple Flutter application with a login page and a home page!
This example introduced navigation, user input handling, and basic state management.
From here, you can expand the app by adding features like user authentication with a backend, form validation, and more.
Happy coding!