Getting Started with Flutter: Your first Flutter app

If you’re new to app development or want to create your first cross-platform mobile app, Flutter is one of the best frameworks to get started with. Backed by Google, Flutter enables developers to build fast, beautiful, and natively compiled apps for mobile, web, and desktop from a single codebase. This means you can write your app once and run it on both Android and iOS devices without needing separate codebases.

Your first Flutter app

In this blog, we’ll guide you through the process of building your first Flutter app, step by step. By the end of this tutorial, you’ll have a working Flutter app that runs on both Android and iOS.

What is Flutter?

Flutter is an open-source UI toolkit that allows you to build natively compiled apps using a single codebase. It uses Dart, a programming language created by Google, and provides a rich set of pre-designed widgets that make it easy to create beautiful and responsive user interfaces.

Some of the key benefits of Flutter include:

Prerequisites for Building Your First Flutter App

Before we begin, make sure you have the following installed on your system:

Once you’ve installed Flutter, open your terminal and run the following command to ensure Flutter is properly set up:

flutter doctor

This command will check for any missing dependencies and show instructions on how to fix them.

Step 1: Create a New Flutter Project

To create a new Flutter project, open your terminal (or command prompt) and navigate to the directory where you want to store your project. Then run the following command:

flutter create my_first_app

This command creates a new Flutter project named my_first_app with a basic structure already set up.

Once the project is created, navigate into the project’s directory:

cd my_first_app

Project Structure

After creating the project, you’ll notice that Flutter sets up several folders and files for you. Here’s an overview of the key directories and files:

Step 2: Run the Default Flutter App

Before making any changes, let’s run the default Flutter app that was generated when we created the project.

On an Android Emulator:

  1. Open Android Studio.
  2. In the toolbar, click on the AVD Manager (Android Virtual Device Manager) icon.
  3. Create a new Android virtual device or start an existing one.
  4. In the terminal, run the following command: flutter run This will launch the app on the Android emulator.

On an iOS Simulator:

  1. If you’re on macOS, open Xcode.
  2. Select Xcode > Open Developer Tool > Simulator.
  3. In the terminal, run the same flutter run command to launch the app on the iOS simulator.

By default, Flutter creates a simple counter app that increments a number each time you press the floating action button. This is a great starting point to understand the basic structure of a Flutter app.

Step 3: Understanding the Default Flutter Code

Open the lib/main.dart file in your code editor. This file contains the code for the default Flutter app. Let’s break it down:

import 'package:flutter/material.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}

Key Parts of the Code:

Step 4: Modifying the App

Now that you understand the basic structure, let’s make some changes to the app. We’ll modify the text and add a simple feature to reset the counter.

Changing the App Title and Text

Open the MyHomePage widget and modify the title and body text.

In the home parameter of the MyApp widget, change the title:

dartCopy codehome: const MyHomePage(title: 'Welcome to My First Flutter App'),

Then, modify the Text widget inside the body to update the message:

const Text(
'You have pressed the button this many times:',
),

Change it to:

const Text(
'Number of times you pressed the button:',
),

Adding a Reset Button

To add a reset button, we’ll modify the _MyHomePageState class. Add the following method to reset the counter:

void _resetCounter() {
setState(() {
_counter = 0;
});
}

Next, add another floating action button below the existing one. Inside the Scaffold, update the floatingActionButton section like this:

floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
const SizedBox(height: 10), // Add some space between buttons
FloatingActionButton(
onPressed: _resetCounter,
tooltip: 'Reset',
child: const Icon(Icons.refresh),
),
],
),

Hot Reload to See Changes Instantly

Thanks to Flutter’s hot reload feature, you can save your changes and see them immediately without restarting the app. Just press Ctrl + S (or Cmd + S on macOS), and your updated app should reflect the changes instantly.

Step 5: Running the App on a Physical Device

Testing your app on a physical device ensures that it works as expected in real-world scenarios. Follow these steps to run the app on an actual Android or iOS device:

For Android:

  1. Connect your Android device to your computer via USB.
  2. Enable Developer Mode and USB Debugging on your device.
  3. Run flutter devices in the terminal to ensure Flutter recognizes your device.
  4. Run the app using flutter run.

For iOS:

  1. Connect your iPhone or iPad to your Mac.
  2. Open Xcode and enable the necessary permissions to run the app on your device.
  3. Run flutter run from the terminal.

Conclusion

You’ve successfully created and modified your first Flutter app! You learned how to set up your development environment, understand the basic Flutter structure, and make changes to your app’s UI and logic. Flutter’s flexibility, ease of use, and powerful features like hot reload make it one of the best frameworks for building cross-platform apps.

Now that you have a working app, you can explore more complex layouts, animations, and even integrate backend services. The possibilities are endless!

Explore Other Flutter Topics…

  1. Introduction to Flutter and Dart
  2. Why choose Flutter
  3. Installing Flutter On Your Windows Mac And Linux System
  4. Your first Flutter app
  5. Flutter project structure
  6. Building blocks of Flutter
  7. Stateful vs. Stateless Widgets Explained
  8. Flutter layout system
  9. Flutter text widget
  10. Creating Buttons in Flutter: ElevatedButton, TextButton, and IconButton
  11. Handling User Input with Flutter Forms
  12. Container class in Flutter
  13. Flutter Navigation
  14. Flutter – Pass Data One Screen To Another Screen
  15. Managing Device Orientation in Flutter
  16. Stateful widget lifecycle in Flutter
  17. Future of Flutter
  18. Flutter Themes
  19. Flutter Animations
  20. Flutter AppBar Customization
  21. ListView in Flutter
  22. Flutter GridView
  23. Flutter Expanded Widget
  24. Flutter BottomNavigation Bar
  25. Floating Action Button
  26. Drawer Widgets in Flutter
  27. Form Validation in Flutter
  28. Flutter TextField
  29. Adding AdMob ads to a Flutter app
  30. Building Flutter Web & Desktop Applications
  31. What is Async and Await in Flutter
  32. HTTP requests in Flutter
  33. Parsing JSON in Flutter
  34. Tinder-Style Swipe Cards in Flutter
  35. Flutter Tic Tac Toe Game Tutorial
  36. Flutter Login UI Tutorial
  37. Flutter Card Widget Tutorial
  38. Flutter music player app tutorial
  39. Flutter introduction screens
  40. Shared Preferences in Flutter
  41. SQLite Database in Flutter
  42. Firebase Authentication in Flutter
  43. Firebase Firestore in Flutter
  44. Push Notifications in Flutter
  45. Handling File Uploads in Flutter
  46. Responsive Design in Flutter
  47. Provider in Flutter
  48. Riverpod in Flutter
  49. Flutter BLoC Pattern Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *