Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

First Flutter Application

First Flutter Application

This section will introduce how to use the Flutter CLI to create a new Flutter project, run the application, and understand the most basic code structure.


Creating a Project with Flutter CLI

Flutter CLI is Flutter's command-line tool for creating, managing, and building Flutter projects.

Creating a new project

Run the following command in the terminal to create a new Flutter project:



$ flutter create my_first_app

where my_first_app is your project name. Flutter will create the corresponding folders and files based on this name.

The project name must use lowercase letters and underscores, and cannot contain spaces or special characters.

Command Parameters

flutter create The command supports a variety of parameters:

Parameters Description Example
--org Specify the organization identifier (reverse domain name format) --org com.example
--platforms Specify the supported platforms --platforms ios,android
--empty Create a project using the minimal template --empty


$ flutter create hello_world

Creating project "hello_world"...

  .gitignore                    2026-04-01 10:30:22

  .metadata                     2026-04-01 10:30:22

  analysis_options.yaml         2026-04-01 10:01:15

  pubspec.yaml                  2026-04-01 10:30:22

  README.md                     2026-04-01 10:30:22

  lib/

    main.dart                    2026-04-01 10:30:22

  test/

    widget_test.dart             2026-04-01 10:22:34

  android/

  ios/

  web/

  ...

Running "flutter pub get"...                     13.2s

Running "flutter analyze"...                     3.2s


Running the Flutter Application

Start the Dev Server

After the project is created, enter the project directory and run the application:



$ cd hello_world

$ flutter run

The first run may take some time, because Flutter needs to compile the Dart code and start the application.

Specifying the Run Device

If your computer is connected to multiple devices (or emulators), you can use the -d parameter to specify the run device:

Device Command
Android emulator flutter run -d android
iOS simulator flutter run -d iphone
Chrome browser flutter run -d chrome
Windows desktop flutter run -d windows

View Available Devices

Run the following command to view all available run devices:



$ flutter devices

Example output:



2 connected devices:

Chrome (web)      • chrome           • web-javascript • Chrome  • web

Windows (windows) • windows          • windows        • x64    • Windows 10.0.0


Hot Reload

One of Flutter's most powerful features is Hot Reload. When you are running the application:

  1. Modify the code and save
  2. Press in the terminal r Key
  3. The application will reload, and you can see the latest changes

Hot Reload is very fast, usually taking only one or two seconds. This greatly improves development efficiency, allowing you to preview the interface in real time.

Hot Reload does not lose application state, which means you can quickly iterate on UI design without restarting the application.


main.dart File Analysis

Every Flutter project has an entry file lib/main.dart. Let's look at the default generated content:

Example: main.dart Code

// lib/main.dart

// Entry file of the Flutter application

// Import the Material Design component library

import

'package:flutter/material.dart'

;

// Application entry function

void

main

(

)

{

 

// runApp is Flutter's startup function

 

// It receives a Widget as the root widget

  runApp

(

const

MyApp

(

)

)

;

}

// Root Widget (stateless component)

class

MyApp

extends

StatelessWidget

{

 

const

MyApp

(

{

super

.

key

}

)

;

  @override

  Widget build

(

BuildContext context

)

{

   

// MaterialApp is the root component in Material Design style

   

return

MaterialApp

(

     

// Set the application title

      title

:

'Flutter Demo'

,

     

// Set the theme color

      theme

:

ThemeData

(

        colorScheme

:

ColorScheme.

fromSeed

(

seedColor

:

Colors.

deepPurple

)

,

       

// Use Material 3

        useMaterial3

:

true

,

     

)

,

     

// The application's main page

      home

:

const

MyHomePage

(

title

:

'Flutter Home Page'

)

,

   

)

;

 

}

}

// Stateful component (can have internal state)

class

MyHomePage

extends

StatefulWidget

{

 

const

MyHomePage

(

{

super

.

key

,

required

this

.

title

}

)

;

 

// Page title (immutable)

 

final

String title

;

  @override

  State

<

MyHomePage

createState

(

)

=>

_MyHomePageState

(

)

;

}

class

_MyHomePageState

extends

State

<

MyHomePage

{

// Counter state

 

int

_counter

=

0

;

 

// Method to increment the counter

 

void

_incrementCounter

(

)

{

   

// setState notifies Flutter that the state has changed and the UI needs to be rebuilt

    setState

(

(

)

{

      _counter

++

;

   

}

)

;

 

}

  @override

  Widget build

(

BuildContext context

)

{

   

// Scaffold is the page scaffold of Material Design

   

return

Scaffold

(

     

// AppBar is the top application bar

      appBar

:

AppBar

(

        backgroundColor

:

Theme.

of

(

context

)

.

colorScheme

.

inversePrimary

,

       

// Display the page title

        title

:

Text

(

widget.

title

)

,

     

)

,

     

// Main content of the page

      body

:

Center

(

        child

:

Column

(

          mainAxisAlignment

:

MainAxisAlignment.

center

,

          children

:

[

           

const

Text

(

'How many times you clicked the button:'

)

,

           

// Display the counter value

            Text

(

             

'$_counter'

,

              style

:

Theme.

of

(

context

)

.

textTheme

.

headlineMedium

,

           

)

,

         

]

,

       

)

,

     

)

,

     

// Floating button

      floatingActionButton

:

FloatingActionButton

(

        onPressed

:

_incrementCounter

,

        tooltip

:

'Increment'

,

        child

:

const

Icon

(

Icons.

add

)

,

     

)

,

   

)

;

 

}

}

Code Structure Explanation

Section Description
import statement Import the required packages; here the Material Design component library is imported
main function The entry point of the Dart program; calls runApp to start the application
runApp function Flutter's startup function; receives the root Widget as a parameter
StatelessWidget Stateless component; UI does not change over time
StatefulWidget Stateful component; UI can respond to data changes
build method A method that every Widget must implement, used to build the UI