🧱 Flutter child vs children Explained — With Examples!
When building UI in Flutter, you’ll quickly notice two properties: child and children. These look similar but are used differently — and mixing them up can cause frustrating errors. Let’s break it down clearly!
🔹 What’s the Difference?
child: Used for a single widget
children: Used for a list of widgets
🔹 When to Use child (Single Widget)
Widgets like Container, Center, and Padding use child because they only hold one widget inside.
Container(
padding: EdgeInsets.all(16),
child: Text("I am a child"),
)
🔹 When to Use children (Multiple Widgets)
Widgets like Row, Column, and Stack use children because they layout multiple widgets.
Row(
children: [
Icon(Icons.star),
SizedBox(width: 10),
Text("I am a child too!"),
],
)
🔹 Error Example: Misusing children
Container(
children: [ // ❌ ERROR
Text("This will fail"),
Text("Container only takes child"),
],
)
🧠 Tips to Remember
- ✔️ If it holds only one widget → use
child - ✔️ If it holds many widgets → use
children - ✔️ Think: Box = child, Shelf = children
- ✔️ Let Flutter’s red error lines help you debug!
🔹 Wrapping Multiple Widgets in a child Widget
When a widget only allows one child but you want to include multiple widgets, wrap them inside a Column or Row.
Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Hello'),
Text('World'),
],
),
)
🔚 Summary Table
| Widget | child |
children |
Notes |
|---|---|---|---|
| Container | ✅ | ❌ | Single box element |
| Center | ✅ | ❌ | Centers one widget |
| Column | ❌ | ✅ | Vertical layout |
| Row | ❌ | ✅ | Horizontal layout |
| Stack | ❌ | ✅ | Overlapping layout |
| Padding | ✅ | ❌ | Adds space around one widget |
| ListView | ❌ | ✅ | Scrollable list |
Now that you understand child vs children, your Flutter layouts will be cleaner and more efficient. Keep coding and experimenting — Flutter will guide you with errors and suggestions!
