Hey there, Flutter enthusiasts! π If you're diving into Flutter and want to master its layout system, you're in the right place. I love sharing my knowledge, and I hope this article helps you build beautiful and responsive UIs with ease. Let's explore some essential layout widgets together!
1. Container
Ref: https://api.flutter.dev/flutter/widgets/Container-class.html
The Container
widget is one of the most commonly used widgets for layout. It allows you to apply padding, margins, borders, and decorations to its child.
Container(
padding: EdgeInsets.all(16.0),
margin: EdgeInsets.all(10.0),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(10),
),
child: Text('Hello, Flutter!'),
)
2. Row and Column
Ref (Row): https://api.flutter.dev/flutter/widgets/Row-class.html
Ref (Column): https://api.flutter.dev/flutter/widgets/Column-class.html
The Row
and Column
widgets are fundamental for arranging widgets horizontally and vertically, respectively.
Row Example:
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(Icons.star),
Icon(Icons.favorite),
Icon(Icons.share),
],
)
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Item 1'),
Text('Item 2'),
Text('Item 3'),
],
)
3. Stack
Ref: https://api.flutter.dev/flutter/widgets/Stack-class.html
The Stack
widget allows overlapping widgets on top of each other.
Stack(
children: [
Container(width: 100, height: 100, color: Colors.red),
Positioned(
top: 20,
left: 20,
child: Icon(Icons.star, color: Colors.white),
),
],
)
4. Expanded & Flexible
Ref (Expanded): https://api.flutter.dev/flutter/widgets/Expanded-class.html
Ref (Flexible): https://api.flutter.dev/flutter/widgets/Flexible-class.html
Sometimes, you need widgets to adjust their size dynamically. Thatβs where Expanded
and Flexible
come in handy!
Row(
children: [
Expanded(child: Container(color: Colors.red, height: 50)),
Expanded(child: Container(color: Colors.blue, height: 50)),
],
)
5. ListView
Ref: https://api.flutter.dev/flutter/widgets/ListView-class.html
When dealing with lists, ListView
is your best friend. It helps you create scrollable content effortlessly.
ListView(
children: List.generate(10, (index) => ListTile(title: Text('Item $index'))),
)
Wrapping Up
I hope this breakdown of layout widgets makes your Flutter journey smoother. Layouts are the backbone of any great UI, so mastering them will take your apps to the next level. Keep experimenting, and most importantly, have fun coding! π
Let's Connect! π
If you found this helpful and want to stay updated with more Flutter tips, feel free to follow me:
Dev.to: https://dev.to/thinnakrit
LinkedIn: https://www.linkedin.com/in/thinnakrit
GitHub: https://github.com/thinnakrit
Happy coding! π
Top comments (0)