🚀 Limited spots available for Summer '25 cohort — Apply now before it's too late!
Day 6: Flutter Widget Tree & Stateless Widgets

Welcome to Day 6 of the "Hundred Days of Flutter" course! Today, we'll dive into Flutter's widget system, starting with the widget tree and stateless widgets. This is where we begin building actual Flutter applications!

Understanding the Widget Tree

In Flutter, everything is a widget. The widget tree is a hierarchical structure that represents your app's UI. Each widget can contain other widgets, creating a tree-like structure.

Basic Widget Tree Structure

void main() {
  runApp(const MyApp());
}
 
class MyApp extends StatelessWidget {
  const MyApp({super.key});
 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('My First App'),
        ),
        body: Center(
          child: Text('Hello, Flutter!'),
        ),
      ),
    );
  }
}

In this example:

  • MyApp is the root widget
  • MaterialApp provides the app's theme and navigation
  • Scaffold provides the basic app structure
  • AppBar is the top bar
  • Center centers its child widget
  • Text displays text

Stateless Widgets

Stateless widgets are widgets that don't maintain any state. They are immutable and rebuild only when their parent widget rebuilds.

Creating a Stateless Widget

class WelcomeCard extends StatelessWidget {
  final String name;
 
  const WelcomeCard({
    super.key,
    required this.name,
  });
 
  @override
  Widget build(BuildContext context) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              'Welcome, $name!',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: () {
                // Button pressed
              },
              child: const Text('Get Started'),
            ),
          ],
        ),
      ),
    );
  }
}

Common Stateless Widgets

  1. Container
Container(
  width: 100,
  height: 100,
  decoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(8),
  ),
  child: const Center(
    child: Text('Container'),
  ),
)
  1. Row and Column
Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: [
    Icon(Icons.star),
    Icon(Icons.star),
    Icon(Icons.star),
  ],
)
 
Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Text('First'),
    Text('Second'),
    Text('Third'),
  ],
)
  1. Stack
Stack(
  children: [
    Container(
      color: Colors.blue,
      width: 200,
      height: 200,
    ),
    Positioned(
      top: 20,
      left: 20,
      child: Container(
        color: Colors.red,
        width: 100,
        height: 100,
      ),
    ),
  ],
)

Widget Properties

Widgets have various properties that control their appearance and behavior:

Styling Properties

Container(
  width: 200,
  height: 200,
  margin: const EdgeInsets.all(16),
  padding: const EdgeInsets.all(8),
  decoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(8),
    boxShadow: [
      BoxShadow(
        color: Colors.black.withOpacity(0.2),
        blurRadius: 8,
        offset: const Offset(0, 4),
      ),
    ],
  ),
  child: const Text(
    'Styled Container',
    style: TextStyle(
      color: Colors.white,
      fontSize: 20,
      fontWeight: FontWeight.bold,
    ),
  ),
)

Layout Properties

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  crossAxisAlignment: CrossAxisAlignment.stretch,
  mainAxisSize: MainAxisSize.min,
  children: [
    Container(
      height: 100,
      color: Colors.red,
    ),
    Container(
      height: 100,
      color: Colors.blue,
    ),
  ],
)

Hot Reload and Hot Restart

Flutter provides two powerful features for development:

  1. Hot Reload (r): Updates the UI while preserving the app's state
  2. Hot Restart (R): Completely rebuilds the app while preserving the widget tree

Knowledge Check

Let's test your understanding of today's concepts:

?

It's time to take a quiz!

What is the main difference between StatelessWidget and StatefulWidget?

?

It's time to take a quiz!

Which widget is used to create a basic app structure with an app bar and body?

?

It's time to take a quiz!

What is the purpose of the build method in a StatelessWidget?

Mini-Challenge: Profile Card

Create a profile card widget that displays a user's information:

  1. Create a ProfileCard stateless widget
  2. Include the following elements:
    • User's avatar (use CircleAvatar)
    • User's name
    • User's bio
    • A row of social media icons
  3. Style it with proper spacing and colors

Here's a starting point:

class ProfileCard extends StatelessWidget {
  final String name;
  final String bio;
  final String avatarUrl;
 
  const ProfileCard({
    super.key,
    required this.name,
    required this.bio,
    required this.avatarUrl,
  });
 
  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.all(16),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            CircleAvatar(
              radius: 50,
              backgroundImage: NetworkImage(avatarUrl),
            ),
            const SizedBox(height: 16),
            Text(
              name,
              style: Theme.of(context).textTheme.headlineSmall,
            ),
            const SizedBox(height: 8),
            Text(
              bio,
              textAlign: TextAlign.center,
              style: Theme.of(context).textTheme.bodyLarge,
            ),
            const SizedBox(height: 16),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconButton(
                  icon: const Icon(Icons.facebook),
                  onPressed: () {},
                ),
                IconButton(
                  icon: const Icon(Icons.twitter),
                  onPressed: () {},
                ),
                IconButton(
                  icon: const Icon(Icons.instagram),
                  onPressed: () {},
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

Key Takeaways

  • Everything in Flutter is a widget
  • The widget tree represents your app's UI hierarchy
  • Stateless widgets are immutable and don't maintain state
  • Widgets can be styled using various properties
  • Hot reload and hot restart help in rapid development
  • Common widgets like Container, Row, Column, and Stack help in layout
  • The build method defines a widget's UI structure

Tomorrow, we'll explore StatefulWidgets and learn how to create interactive UI elements!

Previous

Day 5: Object-Oriented Programming in Dart

Learn the fundamentals of Object-Oriented Programming in Dart, including classes, inheritance, interfaces, and mixins to build well-structured Flutter applications.

Start Previous Day
Next Up

Day 7: Layout Widgets - Part 1

Master Flutter's layout widgets including Row, Column, Expanded, and Flexible to create responsive and well-structured user interfaces.

Start Next Day