AspectRatio (Flutter Widget of the Week)

Flutter · 247K views · 2019 · 2:05

Open on YouTube

Web developmentUX & UI designtutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering Flutter layout questions about aspect ratios, responsive sizing and widgets that ignore their intended proportions.

Recommended by 2 people across 2 different months, 2019 to 2020.

What it is

A roughly two-minute entry in the Flutter team's Widget of the Week series, explaining how the AspectRatio widget keeps a widget's width-to-height ratio consistent. It also covers how parent widgets like Expanded can override AspectRatio and how to work around that. It is presented by the official Flutter channel.

What people said

Recommends Hudson Kim · 2020-06 · Stack Overflow · on “Should I use absolute pixel values in my Flutter widgets, or should I scale them to the screen?”

I would recommend you to scale widgets based on the size of the screen. This allows your application to be more flexible and adjust to various platforms and sizes such as large tablets or small phones. In order to do this, I recommend you to use the widget FractionallySizedBox which allows you to size widgets using a percentage of the screen size. For example, if you want a button widget to fill up 50 percent of a screen's width you can use the following code: Container( alignment: Alignment.center, child: FractionallySizedBox( widthFactor: 0.5, child: FlatButton( onTap: () {}, child: Text("PRESS HERE") ) ) ) This code creates a button positioned in the center of the screen with a width of 5

Recommends ByteMe · 2020-07 · Stack Overflow · on “How to calculate childAspectRatio for GridView.builder in flutter”

Child aspect ratio is basically width/height of the grid relative to the device. So let's say you want the width of each grid to be 30 and the height to be 20, you would set the aspect ratio to be 3/2. I suggest you watch this video [this video] directly from the Flutter team. Although its about the AspectRatio widget, the part about setting the aspectRatio applies to this problem

Cites Ravinder Kumar · 2019-11 · Stack Overflow · on “Select source image resolution dynamically”

Try AspectRatio [this video] , this is responsive for all sizes AspectRatio( aspectRatio: 4 / 3, //change value here child: new Container( color: Colors.red, ), ),

Cites MaskyS · 2020-06 · Stack Overflow · on “How to limit width of multiple TextFields inside a Column? Wrapping the latter with a SizedBox doesn't work”

I found the answer while watching [this video] . "If you put AspectRatio into something like Expanded, then that will be forced by its parent to expand. Tightly fitted widgets like Expanded don't give their children a choice-- harsh. If this happens to you, just put something like Align between the Expanded and the AspectRatio. Align will be forced by Expanded to fill the area, but it will let its child assume its own proportions. Thanks, Align!" I have to wrap my Column in a Container , and then wrap the Container in an Align to break free of Expanded .