Module 6
Topic 1

Custom Widgets

Building reusable, composable widgets in Flutter — the foundation of every great Flutter app.

Flutter Widget Catalog Widget Docs
What Are Custom Widgets?

Custom widgets are the building blocks of Flutter applications. While Flutter provides a rich set of built-in widgets (like Container , Row , Column , Text , etc.), you'll quickly find that you need to create your own widgets to encapsulate reusable UI components and business logic.

💡 Key Concept

In Flutter, everything is a widget . Custom widgets allow you to compose existing widgets into new, reusable components. This is the core philosophy of Flutter — building UIs by composing widgets.

📄
StatelessWidget
Widgets that don't change state
Use when the UI depends only on constructor parameters
🔄
StatefulWidget
Widgets that can change state
Use when the UI needs to update dynamically
🔧
Composition
Building widgets from other widgets
Always prefer composition over inheritance
Stateless vs Stateful

Understanding when to use StatelessWidget vs StatefulWidget is crucial for building efficient Flutter apps.

StatelessWidget

  • Immutable — properties can't change
  • Build method runs once unless parent rebuilds
  • Perfect for presentation-only widgets
  • More performant
  • Example: Text , Icon , Container

StatefulWidget

  • Mutable — can change over time
  • Has a separate State object
  • Can call setState() to rebuild
  • More complex lifecycle
  • Example: Checkbox , TextField , animations

✅ Best Practice: Prefer Stateless

Always start with a StatelessWidget . Only switch to StatefulWidget when you need to manage local state that changes over time. This keeps your code simpler and more performant.

Composition Over Inheritance

In Flutter, composition is preferred over inheritance. Instead of extending a widget to customize it, you compose it with other widgets.

💡 Why Composition?

  • Flexibility – You can combine widgets in any way
  • Reusability – Composed widgets can be used in many places
  • Testability – Smaller, focused widgets are easier to test
  • Maintainability – Changes are isolated to specific widgets
  • Performance – Composition is more efficient than deep inheritance
Building Complex UIs

Custom widgets shine when building complex, reusable UI components. Here's how to build a User Profile Card that can be reused across your app:

Custom Widget Design Patterns

1. The "Container" Pattern

Create widgets that accept a child and wrap it with styling, layout, or behavior.

2. The "List View" Pattern

Create reusable list builders for consistent data display.

3. The "Loading State" Pattern

Build widgets that handle loading, error, and success states.

Widget of the Week

The Flutter team produces Widget of the Week videos — short, 1-minute explainer videos covering individual widgets. These are excellent resources for learning about specific widgets and how to use them.

🎥 Popular Widget Videos

  • Container – The most common layout widget
  • Row & Column – Flexible layout in one dimension
  • Stack – Overlapping widgets
  • ListView – Scrollable list of widgets
  • GridView – Scrollable grid of widgets
  • FutureBuilder – Async data handling
  • StreamBuilder – Real-time data handling
  • Hero – Shared element transitions
Step-by-Step Implementation

Follow these steps to create your own custom widgets:

1.
Identify reusable UI patterns – Look for UI elements that repeat across your app
2.
Design the widget interface – Define required and optional parameters
3.
Start with StatelessWidget – Use StatelessWidget unless you need local state
4.
Compose existing widgets – Build your widget using Flutter's built-in widgets
5.
Use const constructors – Make your widget const when possible for performance
6.
Add documentation – Document your widget's purpose and parameters
7.
Test in isolation – Test your widget in a separate screen or storybook
Common Mistakes
❌ Mistake 1: Using StatefulWidget when Stateless is enough

Not every widget that accepts data needs to be Stateful. If the widget doesn't change its own state, use StatelessWidget.

✅ Correct: Start with StatelessWidget

Always start with StatelessWidget. Only switch to StatefulWidget when you need to manage local state that changes over time.

❌ Mistake 2: Using inheritance instead of composition

Extending widgets to customize them leads to brittle code and makes it harder to change the implementation.

✅ Correct: Compose widgets

Build custom widgets by combining existing widgets. This is more flexible and maintainable.

❌ Mistake 3: Not making widgets const

Non-const widgets are rebuilt every frame, even when nothing changes, hurting performance.

✅ Correct: Use const constructors

Make your widget's constructor const when possible. This allows Flutter to optimize rebuilds.

❌ Mistake 4: Mixing business logic with UI

Putting business logic inside widgets makes them harder to test and reuse.

✅ Correct: Separate concerns

Use ViewModels, BLoCs, or providers for business logic. Keep widgets focused on presentation.

🎯 Key Takeaway

Custom widgets are the foundation of Flutter development. By composing existing widgets into new ones, you can build reusable, maintainable, and performant UIs. Always prefer StatelessWidget over StatefulWidget and composition over inheritance . Use const constructors for performance and keep your widgets focused on presentation.