aspectRatio method

Widget aspectRatio(
  1. double aspectRatio, [
  2. Key? key
])

Sets the aspect ratio constraint for this widget.

The aspect ratio is defined as width / height. When set, one dimension will be automatically calculated based on the other to maintain the ratio.

The optional key parameter assigns a key to the wrapped widget.

Example:

Image(...).aspectRatio(16 / 9),  // 16:9 aspect ratio
Container().aspectRatio(1.0),     // Square

Implementation

Widget aspectRatio(double aspectRatio, [Key? key]) {
  return _WidgetWrapper._wrapOrCopyWith(
    child: this,
    key: key != null ? () => key : null,
    aspectRatio: () => aspectRatio,
  );
}