Flutter: Flutter Introduction and Environment Setup

Flutter lets you build iOS, Android, Web, and Desktop apps with a single Dart codebase — like using a master key to open four doors.

📋 Prerequisites: No prior experience required

1. What You Will Learn


2. A Real Story from an Entrepreneur

(1) The Pain: Four Platforms, Four Codebases

Bob is an entrepreneur whose ShopApp e-commerce project needs to cover iOS, Android, Web, and Desktop simultaneously. If each platform requires its own native codebase, he would need 4 development teams and 4 codebases, with personnel costs exceeding 500 thousand USD/year. Worse yet, a promotional campaign takes 6 weeks from requirements to launch — because each team must implement it independently.

(2) Flutter's Solution

Flutter adopts a "write once, run anywhere" strategy, using Dart to write the UI layer and rendering pixels directly through its custom paint engine, without relying on native UI components.

DART
import 'package:flutter/material.dart';

// One codebase, four platforms
void main() => runApp(const ShopApp());

class ShopApp extends StatelessWidget {
  const ShopApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ShopApp',
      home: Scaffold(
        appBar: AppBar(title: const Text('ShopApp')),
        body: const Center(child: Text('Hello, Bob!')),
      ),
    );
  }
}
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

(3) The Result: 1 Team Replaces 4

After adopting Flutter, Bob's single team maintains one codebase, launching simultaneously on all 4 platforms. Promotional campaigns shrank from 6 weeks to 1 week, and annual development costs dropped from 500 thousand USD to 150 thousand USD.


3. Flutter Technical Architecture

Flutter uses a layered architecture, from the upper Framework to the lower Engine, each with its own responsibilities.

100%
graph TD
    A[Flutter SDK] --> B[Dart SDK]
    A --> C[Framework Layer]
    A --> D[Engine Layer]
    C --> E[Material Library]
    C --> F[Cupertino Library]
    D --> G[Skia / Impeller]
    D --> H[Platform Embedder]
    H --> I[Android]
    H --> J[iOS]
    H --> K[Web]
    H --> L[Windows/macOS/Linux]
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

(1) Framework Layer

The Framework layer is what developers interact with directly, written entirely in Dart:

Layer Responsibility Language
Widget Declare UI configuration Dart
Element Manage Widget tree diff Dart
RenderObject Layout and painting Dart
Engine Render and I/O C++

(2) Engine Layer

The Engine layer is written in C++, with core components:

💡 Tip: Flutter 3.x enables the Impeller rendering engine by default, replacing Skia, to resolve first-frame jank and shader compilation delays.

(3) Platform Embedder

Each platform has a different Embedder implementation, responsible for:

Platform Embedder Rendering Method
Android flutter_shell (Java/Kotlin) GPU (Vulkan/OpenGL)
iOS FlutterViewController (Swift/ObjC) GPU (Metal)
Web HTML Canvas / WebGL CanvasKit / HTML renderer
Desktop GLFW / Win32 / Cocoa GPU

4. Everything is a Widget

Flutter's core philosophy: everything is a Widget. Buttons, fonts, spacing, and even layout constraints are all Widgets.

(1) Widget Categories

Type Description Examples
StatelessWidget Immutable, builds once Text, Icon, Image
StatefulWidget Mutable state, can rebuild TextField, Checkbox
RenderObjectWidget Controls layout and painting Flex, Stack, Positioned
ProxyWidget Passes data to subtree InheritedWidget, Provider

▶ Example

TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

: StatelessWidget product card

DART
import 'package:flutter/material.dart';

class ProductCard extends StatelessWidget {
  final String name;
  final double price;

  const ProductCard({
    super.key,
    required this.name,
    required this.price,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(name, style: const TextStyle(fontSize: 18)),
            Text('\$${price.toStringAsFixed(2)}',
                style: const TextStyle(color: Colors.green, fontSize: 16)),
          ],
        ),
      ),
    );
  }
}
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

▶ Example

TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

: StatefulWidget counter

DART
import 'package:flutter/material.dart';

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

  @override
  State<QuantitySelector> createState() => _QuantitySelectorState();
}

class _QuantitySelectorState extends State<QuantitySelector> {
  int _count = 1;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        IconButton(
          icon: const Icon(Icons.remove),
          onPressed: () => setState(() => _count = (_count - 1).clamp(1, 99)),
        ),
        Text('$_count', style: const TextStyle(fontSize: 20)),
        IconButton(
          icon: const Icon(Icons.add),
          onPressed: () => setState(() => _count++),
        ),
      ],
    );
  }
}
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

5. Environment Setup

(1) Flutter SDK Installation

Step Windows macOS Linux
Download SDK flutter.dev/docs/get-started/install/windows flutter.dev/docs/get-started/install/macos flutter.dev/docs/get-started/install/linux
Extract path C:\flutter ~/development/flutter ~/development/flutter
PATH config Add to system environment variables Add to ~/.zshrc Add to ~/.bashrc

▶ Example

TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

: PATH configuration (macOS/Linux)

BASH
# Add Flutter to PATH in ~/.zshrc or ~/.bashrc
export PATH="$HOME/development/flutter/bin:$PATH"

# Verify installation
flutter --version
# Output: Flutter 3.x.x • channel stable
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

▶ Example

TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

: flutter doctor check

BASH
# Run doctor to check all dependencies
flutter doctor

# Expected output:
# [✓] Flutter (Channel stable, 3.x.x)
# [✓] Android toolchain
# [✓] Chrome - develop for the web
# [✓] Android Studio
# [!] VS Code (install Flutter extension)
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

(2) IDE Configuration

IDE Required Plugins Recommended Plugins
VS Code Flutter, Dart Flutter Widget Snippets, Error Lens
Android Studio Flutter, Dart Flutter Intl, Riverpod Snippet

▶ Example

TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

: Create your first project

BASH
# Create a new Flutter project
flutter create shop_app

# Navigate to project directory
cd shop_app

# Run on connected device or emulator
flutter run
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

6. First Hello World

▶ Example

TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

: ShopApp Hello World

DART
import 'package:flutter/material.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'ShopApp',
      theme: ThemeData(
        colorSchemeSeed: Colors.blue,
        useMaterial3: true,
      ),
      home: const HomePage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('ShopApp')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const Icon(Icons.shopping_bag, size: 80, color: Colors.blue),
            const SizedBox(height: 16),
            const Text('Welcome to ShopApp!',
                style: TextStyle(fontSize: 24)),
            const SizedBox(height: 8),
            Text('Million-level products, USD settlement',
                style: TextStyle(fontSize: 14, color: Colors.grey[600])),
            const SizedBox(height: 24),
            ElevatedButton(
              onPressed: () {},
              child: const Text('Start Shopping'),
            ),
          ],
        ),
      ),
    );
  }
}
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

▶ Example

TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

: Hot reload experience

BASH
# Start app with hot reload support
flutter run

# After modifying code, press:
# r - Hot reload (keep state, update UI)
# R - Hot restart (reset state, rebuild)
# q - Quit

# Hot reload updates UI in <1 second
# Try: change 'Welcome to ShopApp!' to 'Hello Bob!'
# Press r → UI updates instantly
TEXT
> Output: Run locally with Flutter SDK (Flutter 3.x / Dart 3.x). The Piston server does not have Flutter installed; use `flutter run` on your local machine for comparison. Actual UI/state may vary slightly across platforms.

7. Flutter 3.x New Features

Feature Description Impact
Impeller engine Pre-compiled shaders, eliminates first-frame jank 30%+ rendering performance improvement
Foldable support Adaptive layout for foldable screens Expanded device coverage
Material 3 New design specification, dynamic color More modern UI
macOS / Linux stable Official Desktop platform support Full 4-platform coverage
Rendering Engine First Frame Time Shader Compilation Applicable Version
Skia 200-500ms Runtime JIT compilation Flutter 2.x
Impeller <50ms Build-time AOT compilation Flutter 3.x

❓ FAQ

Q What's the difference between Flutter and React Native?
A Flutter's custom paint engine doesn't rely on native components, providing better UI consistency; React Native calls native components via a JS Bridge, which creates a performance bottleneck.
Q Is Dart hard to learn?
A Dart syntax is similar to Java and JavaScript. With experience in either language, you can get started in 1-2 days.
Q What if flutter doctor reports "Android licenses not accepted"?
A Run flutter doctor --android-licenses and accept each license one by one.
Q Can Flutter Web be used in production?
A Flutter 3.x Web is stable, but SEO support is limited. It's suitable for admin dashboards and tool applications, not for content-driven SEO sites.
Q Does the Impeller engine need to be manually enabled?
A Flutter 3.16+ enables Impeller by default on Android/iOS. No manual configuration needed.
Q Should I use JIT or AOT for development?
A Use JIT during development (supports hot reload), and AOT for release (best performance; Flutter handles this automatically).

📖 Summary


📝 Exercises

  1. Basic (difficulty ⭐): Install the Flutter SDK on your computer, run flutter doctor, and ensure at least 4 items pass.
  2. Intermediate (difficulty ⭐⭐): Modify the Hello World example to add a Text widget that displays the current date.
  3. Challenge (difficulty ⭐⭐⭐): Create a Column with 3 product cards, each showing a product name and USD price. Clicking a card should display product information using a SnackBar.

← Previous | Next →

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏