AspectRatio Widget
The AspectRatio widget sizes its child to a specific aspect ratio, which is the ratio of width to height.
Properties
aspectRatio
: number - The ratio of width to heightchild
: Widget - The widget to size according to the aspect ratio
Common Aspect Ratios
-
16:9 (1.78:1)
- Widescreen video
- Modern displays
AspectRatio({ aspectRatio: 1.78, child: VideoPlayer() })
-
4:3 (1.33:1)
- Traditional photos
- Older displays
AspectRatio({ aspectRatio: 1.33, child: Image() })
-
1:1 (1:1)
- Square images
- Profile pictures
AspectRatio({ aspectRatio: 1, child: ProfileImage() })
Use Cases
-
Responsive Video Players:
Container({ width: "100%", child: AspectRatio({ aspectRatio: 1.78, child: VideoPlayer() }) })
-
Image Galleries:
GridView({ children: images.map(image => AspectRatio({ aspectRatio: 1, child: Image(image) }) ) })
-
Cards with Media:
Column({ children: [ AspectRatio({ aspectRatio: 1.78, child: HeaderImage() }), CardContent() ] })
Best Practices
-
Responsive Design:
- Use AspectRatio for consistent layouts
- Consider different screen sizes
- Test with various parent constraints
-
Content Fitting:
- Use appropriate aspect ratios for content type
- Consider content cropping
- Handle overflow appropriately
-
Performance:
- Avoid unnecessary nesting
- Use const widgets when possible
- Consider caching for lists
Common Patterns
-
Media Players:
- Video players
- Image galleries
- Slideshows
-
Cards and Tiles:
- Product cards
- News articles
- Photo galleries
-
Profile Pictures:
- User avatars
- Team member photos
- Contact images
Tips and Tricks
-
Dynamic Ratios:
- Calculate ratios based on content
- Consider orientation changes
- Handle edge cases
-
Content Scaling:
- Use appropriate fit modes for images
- Handle different content sizes
- Consider placeholder content
-
Layout Integration:
- Combine with other layout widgets
- Handle parent constraints
- Consider overflow cases