RenderAspectRatio具有无限的约束

时间:2019-04-22 02:34:58

标签: dart flutter

我正在尝试使用以下代码在flutter应用程序中构建滚动滑块,以建立约束:

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;
})

为了给它提供宽度和高度限制,但产生的错误表明框限制是无界的:

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

我该如何解决?

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,我意识到错误代码为我提供了解决方案,将AspectRatio放在容器小部件中并指定其宽度和高度。就您而言,下面的代码可以解决

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

答案 1 :(得分:0)

Tom的答案似乎是解决方案,但您也可以使用Expanded小部件并为其指定flex值,而不是width和height。

喜欢:

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