Introduction to Flutter

Flutter is Google's open-source UI toolkit for building natively compiled applications for mobile, web, and desktop from a single codebase. It uses the Dart programming language and its own rendering engine for pixel-perfect UIs.

Why Flutter?

  • Single codebase — Write once, deploy to iOS, Android, web, Windows, macOS, Linux
  • Hot reload — See changes instantly without losing state
  • Rich widget library — Hundreds of Material and Cupertino widgets
  • Custom rendering — Skia-based engine for consistent pixel-perfect UIs
  • Performance — Compiles to native ARM code, 60/120 FPS
  • Growing ecosystem — pub.dev has 40,000+ packages

Flutter vs Other Frameworks

Feature Flutter React Native Native (iOS/Android)
Language Dart JavaScript Swift/Kotlin
Rendering Own engine (Skia) Native components Native components
Performance Excellent Good Best
Hot reload Yes Yes Limited
Code sharing 100% cross-platform ~80% 0%
UI consistency Pixel-perfect Platform-dependent Platform-dependent
Learning curve Moderate Low (if know JS) High (2 languages)

Dart Language Basics

Dart
void main() {
  // Variables
  String name = 'Flutter';           // Explicit type
  var version = '3.16';             // Inferred type
  const pi = 3.14159;               // Compile-time constant
  final now = DateTime.now();       // Runtime constant (set once)
  
  // Null safety
  String? nullable = null;           // Can be null
  String definite = 'hello';        // Cannot be null
  
  // Functions
  int add(int a, int b) => a + b;   // Arrow syntax
  print(add(2, 3));                  // 5
  
  // Collections
  List<int> numbers = [1, 2, 3];
  Map<String, int> scores = {'alice': 95, 'bob': 87};
  
  // Loops
  for (var n in numbers) {
    print(n);
  }
  
  // Async
  Future<String> fetchData() async {
    var response = await http.get(Uri.parse('https://api.example.com'));
    return response.body;
  }
}

Project Structure

Code
my_app/
├── lib/
│   ├── main.dart              # App entry point
│   ├── app.dart               # MaterialApp widget
│   ├── models/                # Data models
│   ├── screens/               # Page widgets
│   ├── widgets/               # Reusable components
│   ├── services/              # API calls, storage
│   └── utils/                 # Helpers, constants
├── test/                      # Unit & widget tests
├── android/                   # Android-specific code
├── ios/                       # iOS-specific code
├── web/                       # Web-specific code
├── pubspec.yaml               # Dependencies
└── analysis_options.yaml      # Lint rules

Your First Flutter App

Dart
// lib/main.dart
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(
        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('Welcome to Flutter'),
      ),
      body: const Center(
        child: Text(
          'Hello, Flutter!',
          style: TextStyle(fontSize: 24),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: const Icon(Icons.add),
      ),
    );
  }
}

Running the App

Bash
# Create a new project
flutter create my_app
cd my_app

# Run on connected device/emulator
flutter run

# Run on specific device
flutter run -d chrome        # Web
flutter run -d macos         # macOS desktop

# Build for release
flutter build apk            # Android APK
flutter build ios            # iOS
flutter build web            # Web

💡 Tip: Install the Flutter extension for VS Code or Android Studio for syntax highlighting, hot reload, and device selection.

Next: Widgets and Layout