Flutter: Compact Dialogs and Button Groups with mainAxisSize
mainAxisSize controls how much space a Row or Column takes along its main axis. It is especially useful when you want compact, clean layouts for dialogs, button groups, and small cards.
Live Demo
You can try the mainAxisSize behavior in the interactive example below:
💡 If the example does not open, click the DartPad link to run it in a new tab.
Why Compact Layouts Matter
Sometimes the goal is not to fill every possible pixel. In dialogs, small control groups, and icon rows, a tighter layout looks more intentional and easier to read.
This topic also matches searches such as "flutter mainAxisSize", "compact row column", and "flutter dialog layout".
mainAxisSize.max vs mainAxisSize.min
MainAxisSize.max: Uses all available space.MainAxisSize.min: Uses only the space needed by the content.
Row(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(Icons.star),
SizedBox(width: 8),
Text('Compact Row'),
],
)Why It Works Well for Dialogs
Dialogs often look better when they do not stretch unnecessarily. mainAxisSize: MainAxisSize.min keeps the content compact and balanced.
AlertDialog(
content: Column(
mainAxisSize: MainAxisSize.min,
children: const [
Icon(Icons.warning, size: 48, color: Colors.orange),
SizedBox(height: 16),
Text('Warning message'),
],
),
)Using It in Button Groups
Small toolbars or button clusters often benefit from Row(mainAxisSize: MainAxisSize.min).
Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(onPressed: () {}, icon: const Icon(Icons.remove)),
const Text('0'),
IconButton(onPressed: () {}, icon: const Icon(Icons.add)),
],
)Compact Card Example
Card(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: const [
Text('Title'),
SizedBox(height: 8),
Text('Short description'),
],
),
),
)Common Mistakes
- Using
MainAxisSize.mineverywhere by default - Choosing
minin a layout that actually needs full width or height - Misreading how
mainAxisAlignmentbehaves when less space is available - Assuming Row and Column behave identically on both axes
How mainAxisAlignment Relates
When you use mainAxisSize.min, there is less extra room to distribute, so mainAxisAlignment often feels more constrained and more predictable.
Frequently Asked Questions
Why is mainAxisSize.min popular in dialogs?
Because it keeps the dialog content compact and more natural-looking.
When should I use mainAxisSize.max?
When the layout should consume all available space along the main axis.
Is the behavior the same for Row and Column?
Yes in principle, but Row works horizontally and Column works vertically.
Summary
mainAxisSize is one of the simplest ways to control space in Flutter layouts. Used in the right places, it improves compact dialog design, button groups, and card structures while keeping the UI clean.