Layout Basics
Sizing
Elements can be sized using theSize enum with several options:
Size Options
Size::px(value)- Fixed size in pixelsSize::percent(value)- Percentage of parent sizeSize::fill()- Fill available spaceSize::fill_minimum()- Fill to minimum required sizeSize::flex(ratio)- Flexible size with ratio
Content Modes
Control how elements size themselves based on their content:Content Types
Content::Fit - Element sizes to fit its content:Layout Direction
Vertical Layout (Default)
Elements stack vertically by default:Horizontal Layout
Arrange elements horizontally:Alignment
Main Axis Alignment
Control how children are positioned along the main axis:Alignment Options
Alignment::Start- Align to start (default)Alignment::Center- Center alignmentAlignment::End- Align to endAlignment::SpaceBetween- Even spacing between itemsAlignment::SpaceAround- Even spacing around itemsAlignment::SpaceEvenly- Equal spacing between and around items
Example: All Alignment Types
Cross Axis Alignment
Align children perpendicular to the main axis:Spacing
Add consistent spacing between children:Padding and Margin
Padding (Inside Spacing)
Margin (Outside Spacing)
Common Layout Patterns
Centered Content
Header-Content-Footer
Sidebar Layout
Grid-like Layout
Responsive Card Grid
Scrollable Layouts
For content that exceeds the viewport:Advanced Techniques
Aspect Ratio
Maintain aspect ratio for images or video:Min/Max Constraints
Visible Area
Access layout information programmatically:Performance Tips
-
Use
expanded()instead ofSize::fill()when possible - it’s more explicit and easier to read - Minimize layout nesting - deep hierarchies can impact layout performance
-
Use
VirtualScrollViewfor large lists instead of regularScrollView:
- Avoid unnecessary re-layouts - use specific sizing when possible instead of dynamic content sizing
Best Practices
- Be explicit with sizing - always specify width and height for predictable layouts
- Use semantic spacing - define spacing constants for consistency:
-
Start with
rect()- it’s the most flexible container element - Compose layouts - break complex layouts into reusable components
Next Steps
- Learn about Styling to add visual polish to your layouts
- Explore Components for pre-built layout components
- Check out Torin documentation for deep dive into the layout engine