我正在尝试使用以下代码在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

我该如何解决?

最佳答案

我遇到了同样的问题,我意识到错误代码为我提供了解决方案,将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;
        }
    )
)

10-08 03:28