DEV Community

Cover image for Understanding Layout Widgets in Flutter
Thinnakrit
Thinnakrit

Posted on • Edited on

Understanding Layout Widgets in Flutter

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.

Image description

Container(
  padding: EdgeInsets.all(16.0),
  margin: EdgeInsets.all(10.0),
  decoration: BoxDecoration(
    color: Colors.blue,
    borderRadius: BorderRadius.circular(10),
  ),
  child: Text('Hello, Flutter!'),
)
Enter fullscreen mode Exit fullscreen mode

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:

Image description

Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  children: [
    Icon(Icons.star),
    Icon(Icons.favorite),
    Icon(Icons.share),
  ],
)
Enter fullscreen mode Exit fullscreen mode

Column Example:
Image description

Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: [
    Text('Item 1'),
    Text('Item 2'),
    Text('Item 3'),
  ],
)
Enter fullscreen mode Exit fullscreen mode

3. Stack

Ref: https://api.flutter.dev/flutter/widgets/Stack-class.html

The Stack widget allows overlapping widgets on top of each other.

Image description

Stack(
  children: [
    Container(width: 100, height: 100, color: Colors.red),
    Positioned(
      top: 20,
      left: 20,
      child: Icon(Icons.star, color: Colors.white),
    ),
  ],
)
Enter fullscreen mode Exit fullscreen mode

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!

Image description

Row(
  children: [
    Expanded(child: Container(color: Colors.red, height: 50)),
    Expanded(child: Container(color: Colors.blue, height: 50)),
  ],
)
Enter fullscreen mode Exit fullscreen mode

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.

Image description

ListView(
  children: List.generate(10, (index) => ListTile(title: Text('Item $index'))),
)
Enter fullscreen mode Exit fullscreen mode

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)