Answer
Overview
The widget forces its child to maintain a specific width-to-height ratio, regardless of the available space.textAspectRatio
Basic Usage
dartAspectRatio( aspectRatio: 16 / 9, // Width:Height = 16:9 child: Container( color: Colors.blue, ), )
If the available width is
, the height will automatically betext320px(320 × 9/16).text180px
Common Aspect Ratios
| Ratio | Use Case |
|---|---|
text | Video players, YouTube thumbnails |
text | Classic photos, older screens |
text | Square images, profile photos |
text | Photography (DSLR standard) |
text | Ultrawide video |
Video Player Example
dartAspectRatio( aspectRatio: 16 / 9, child: VideoPlayer(_videoController), )
Image Card Example
dartCard( child: Column( children: [ AspectRatio( aspectRatio: 16 / 9, child: Image.network( 'https://example.com/image.jpg fit: BoxFit.cover, ), ), Padding( padding: EdgeInsets.all(8), child: Text('Image Title'), ), ], ), )
Inside a List
dartListView.builder( itemCount: items.length, itemBuilder: (context, index) { return AspectRatio( aspectRatio: 16 / 9, child: Image.network(items[index].imageUrl, fit: BoxFit.cover), ); }, )
How It Works
- receives the available width from its parent constrainttext
AspectRatio - It calculates height = width / aspectRatio
- If the calculated height exceeds available height, it uses height and calculates width instead
Use Case: Use
whenever you need a child widget to always maintain consistent proportions across different screen sizes — especially for media content like images and videos.textAspectRatio