ahmed osman
ahmed osman

Reputation: 305

RenderAspectRatio has unbounded constraints

I'm trying to build a scroll slider in a flutter app using this code to give to build the constraints:

var cardAspectRatio = 12.0 / 16.0;
var widgetAspectRatio = cardAspectRatio * 1.2;

AspectRatio (
aspectRatio: widgetAspectRatio,
  child: LayoutBuilder(builder: (context, constraints) {
    var width = constraints.maxWidth;
    var height = constraints.maxHeight;
})

In order to give it its width and height constraints but the resulting error says that the box constraints are unbounded:

I/flutter ( 6957): RenderAspectRatio has unbounded constraints.
I/flutter ( 6957): This RenderAspectRatio was given an aspect ratio of 
                   0.8999999999999999 but was given both unbounded
I/flutter ( 6957): constraints: BoxConstraints(unconstrained)
I/flutter ( 6957): size: MISSING
I/flutter ( 6957): aspectRatio: 0.9

How can i solve this?

Upvotes: 12

Views: 6378

Answers (2)

Amin Memariani
Amin Memariani

Reputation: 912

Tom's answer seems to be the solution but you could also use the Expanded widget and give it a flex value instead of width and height.

like:

   Expanded(
    flex: 3,
       child: AspectRatio (
        aspectRatio: widgetAspectRatio,
        child: LayoutBuilder(builder: (context, constraints) {
       })
   ),

Upvotes: 2

tom gates
tom gates

Reputation: 111

I had the same problem, I realized the error code gives me the solution, put the AspectRatio inside a container widget and specify its width and height. In your case, the code below will work out

Container(
    width = your width;
    height = your height;
    child:AspectRatio (
        aspectRatio: widgetAspectRatio,
        child: LayoutBuilder(builder: (context, constraints) {
            var width = constraints.maxWidth;
            var height = constraints.maxHeight;
        }
    )
)

Upvotes: 6

Related Questions