Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Your First Flutter App

3. Your First Flutter App

In this detailed tutorial, we'll guide you through creating your first Flutter app. By the end of this tutorial, you'll have a basic Flutter app up and running on either an emulator or a physical device. We'll go step by step, explaining each part of the code to ensure you understand the fundamentals of Flutter development.

Prerequisites:

Before you begin, make sure you have completed the steps in the "Setting up Flutter Development Environment Tutorial" to set up your Flutter development environment.

Step 1: Create a New Flutter Project

1. Open Your Code Editor: Open your preferred code editor, such as Visual Studio Code.

2. Create a New Flutter Project: Click on "File" > "New Project" or use the command-line to create a new Flutter project. For the command-line approach, run the following command:

flutter create my_first_flutter_app
 

Replace 'my_first_flutter_app' with your preferred project name.

Step 2: Explore the Project Structure

Once the project is created, take a moment to explore the project structure. Here are some key directories and files:

  • 'lib/': This directory contains the Dart code for your app.
  • 'main.dart': The entry point for your app. This is where your app's main function is defined.
  • 'pubspec.yaml': The configuration file for your Flutter app, where you specify dependencies and other project information.

Step 3: Write Your First Flutter Code

1. Open 'lib/main.dart': This is where you'll write the code for your first Flutter app.

2. Replace the Default Code: Replace the default code in 'main.dart' with the following simple Flutter app code:

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('My First Flutter App'),
        ),
        body: Center(
          child: Text('Hello, Flutter!'),
        ),
      ),
    );
  }
}
 

Let's break down this code:

  • We import the 'package:flutter/material.dart' library, which provides widgets and tools for building Material Design apps.

  • In the 'main' function, we call 'runApp()' and pass an instance of 'MyApp'.

  • 'MyApp' is a custom widget we create. It extends 'StatelessWidget', which means it won't change after being built.

  • In the 'build' method of 'MyApp', we return a 'MaterialApp', which is the root widget for our app.

  • Within 'MaterialApp', we define a 'Scaffold' widget, which provides the basic structure for a typical app, including an app bar and body.

  • Inside the 'Scaffold', we set the app bar title to "My First Flutter App" using the 'AppBar'widget.

  • In the 'body'property of the 'Scaffold', we center a 'Text' widget that displays "Hello, Flutter!".

Step 4: Run Your App

Now, it's time to run your Flutter app. You can choose to run it on an emulator or a physical device.

Running on an Emulator

If you have set up an Android or iOS emulator:

1. Open a Terminal: Open a terminal window.

2. Navigate to Your Project's Directory: Use the 'cd'command to navigate to your project's directory:

cd my_first_flutter_app
 

 Replace 'my_first_flutter_app' with your project's actual name.

3. Run the App on the Emulator: Run the app on the emulator by executing the following command:

flutter run
 

This command compiles and deploys your app to the emulator. You should see your app's UI displayed on the emulator screen.

Running on a Physical Device

If you have a physical Android or iOS device connected to your computer:

1. Ensure Your Device is Connected: Make sure your device is connected and recognized by your computer.

2. Navigate to Your Project's Directory: Use the 'cd' command to navigate to your project's directory:

cd my_first_flutter_app
 

3. Run the App on Your Device: Run the app on your physical device by executing the following command:

flutter run
 

This command will install and launch your app on the connected device.

Step 5: Explore and Experiment

Congratulations! You've created and run your first Flutter app. Take some time to explore the code you've written and experiment with making changes. Try modifying the text, colors, and widgets to see how they affect your app's appearance.

Conclusion:

In this tutorial, you've learned how to create a simple Flutter app and run it on either an emulator or a physical device. This is just the beginning of your Flutter journey. As you become more familiar with Flutter, you can explore more advanced widgets and features to build more complex and feature-rich applications. Happy Fluttering!