Why Flutter and Dart?

Flutter is Google's open-source UI toolkit for building natively compiled, multi-platform applications from a single codebase. Paired with Dart—a fast, client-optimized programming language with ahead-of-time (AOT) compilation—Flutter enables developers to deliver 60fps/120fps smooth animations on Android and iOS devices.

Hangbug's own Android application is built with Flutter and Dart, connecting directly to our shared Vercel REST APIs.


1. Dart Language Essentials: Sound Null Safety

Dart prevents runtime null crashes with sound null safety:

dart
// Non-nullable type: cannot be null
String appName = 'Hangbug';

// Nullable type: marked with a question mark (?)
String? userAvatarUrl;

// Null-aware operator (??): fallback if null
String displayAvatar = userAvatarUrl ?? 'https://www.hangbug.com/default-avatar.png';

// Null-aware assignment (??=)
userAvatarUrl ??= 'https://www.hangbug.com/avatar1.png';

2. The Core Philosophy: "Everything is a Widget"

In Flutter, visual components, layout structures, and styling rules are all widgets composed into a tree:

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

void main() => runApp(const HangbugQuizApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Hangbug',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF193D35)),
        useMaterial3: true,
      ),
      home: const QuizCategoryScreen(),
    );
  }
}

3. StatelessWidget vs. StatefulWidget

Choosing the right widget depends on whether the component needs to hold mutable state:

StatelessWidget (Static UI)

Used when configuration values do not change over time:

dart
class ScoreBadge extends StatelessWidget {
  final int score;
  const ScoreBadge({super.key, required this.score});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      decoration: BoxDecoration(
        color: const Color(0xFFF3E5C5),
        borderRadius: BorderRadius.circular(16),
      ),
      child: Text(
        'Score: $score%',
        style: const TextStyle(color: Color(0xFF193D35), fontWeight: FontWeight.bold),
      ),
    );
  }
}

StatefulWidget (Dynamic UI)

Used when user interaction or network responses update the UI:

dart
class QuestionCard extends StatefulWidget {
  final String questionText;
  const QuestionCard({super.key, required this.questionText});

  @override
  State<QuestionCard> createState() => _QuestionCardState();
}

class _QuestionCardState extends State<QuestionCard> {
  int? _selectedOptionIndex;

  void _selectOption(int index) {
    setState(() {
      _selectedOptionIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Card(
      child: ListTile(
        title: Text(widget.questionText),
        trailing: _selectedOptionIndex != null ? const Icon(Icons.check_circle, color: Colors.green) : null,
      ),
    );
  }
}

Frequently Asked Questions

What is Hot Reload in Flutter?

Hot Reload injects updated source code files into the running Dart Virtual Machine without losing the application's current state, allowing you to test UI changes in milliseconds.

Can Flutter connect to REST APIs?

Yes, using the official http package, Flutter can execute GET, POST, and PUT requests to any JSON backend API.


Practice on Hangbug