Dart Tutorial

Flutter Lesson 3 of 83 3 min read

Create and Run Your First Flutter App

Create a new Flutter project with flutter create, run it on a device or in Chrome, and make your first changes to the counter app.

On this page

Create the project #

flutter create my_first_app
cd my_first_app

Project names are lower case with underscores. Two useful options:

flutter create --org com.yourname my_first_app   # sets the app id prefix
flutter create --empty my_first_app              # a minimal starting point

Run it #

flutter run

With several devices available, Flutter asks which to use. Or choose directly:

flutter run -d chrome

The first build takes a few minutes. Then the starter counter app appears. In VS Code, press F5 instead.

A simpler starting point #

The generated lib/main.dart has a lot of comments. Replace it with this shorter version of the same app.

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: 'My First App',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
      ),
      home: const CounterPage(),
    );
  }
}

class CounterPage extends StatefulWidget {
  const CounterPage({super.key});

  @override
  State<CounterPage> createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Counter')),
      body: Center(
        child: Text(
          'You pressed the button $_count times',
          style: Theme.of(context).textTheme.titleLarge,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => setState(() => _count++),
        child: const Icon(Icons.add),
      ),
    );
  }
}

What each part does #

CodeMeaning
runApp(...)Puts a widget on the screen. Every app starts here
MyAppThe root widget. It never changes, so it is a StatelessWidget
CounterPageHolds a number that changes, so it is a StatefulWidget
_countThe state: data that can change while the app runs
setState(...)Changes the state and tells Flutter to redraw
buildDescribes what the screen looks like for the current state

The idea to hold on to: you never change the screen directly. You change the state, and build runs again to describe the new screen.

Make it yours #

Try each change and save. The app updates almost instantly.

  1. Change Colors.blue to Colors.deepOrange.
  2. Change the text to 'Taps: $_count'.
  3. Add a second button that subtracts. Replace the floatingActionButton with:
floatingActionButton: Row(
  mainAxisAlignment: MainAxisAlignment.end,
  children: [
    FloatingActionButton(
      onPressed: () => setState(() => _count--),
      child: const Icon(Icons.remove),
    ),
    const SizedBox(width: 12),
    FloatingActionButton(
      onPressed: () => setState(() => _count++),
      child: const Icon(Icons.add),
    ),
  ],
),

Commands you will use every day #

CommandPurpose
flutter runRun in debug mode
flutter pub add <package>Add a package
flutter pub getDownload packages
flutter analyzeCheck the code
flutter testRun tests
flutter cleanDelete build output. Fixes many odd build errors

Try it yourself #

Add a reset button to the app bar: give AppBar an actions list containing an IconButton with Icons.refresh that sets _count to 0.

Practise in the playground Updated by Santosh Adhikari