首页 > 解决方案 > BoxConstraints 强制无限高度误差

问题描述

目标:我需要让此列可滚动(垂直)。

预期:此列可滚动。

实际:我的控制台出现错误,我的应用程序是空白屏幕。

值得一提的是,我确实在 Stack Overflow 上阅读了其他 BoxConstraints 问题。

不幸的是,它没有用。

最终,我有一个包含不同身高孩子的专栏。子项的组合高度大于视口高度。

我使用 SingleChildScrollview 并按照以下说明进行操作:

https://api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html

LayoutBuilder、ConstrainedBox、BoxConstraints 和 IntrinsicHeight 都被使用了,但我仍然得到那个错误。

我的手机目前是白屏(右上角有“Debug”)。

所以这是我的理解:

SingleChildScrollView 通过授予无限高度使内容可滚动。

Column 自然占据了其父级的全部高度。

2 组合意味着一个 Column 将一直延伸到永远,或者直到应用程序崩溃。

LayoutBuilder 用于获取视口的大小。

ConstrainedBox 用于设置 Column 的 MINIMUM 高度。这对我说,Column 不能小于给定的高度,但可以大到无穷大。

“魔术”发生在 IntrinsicHeight 上,它强制列与其内容一样大。这就是阻止列走向无穷大的原因。

尽管如此,我仍然遇到错误。

我希望有人能帮帮忙; 以下是我的代码:

              padding: EdgeInsets.all(15),
              color: Colors.white,
              child: LayoutBuilder(
                builder:
                    (BuildContext context, BoxConstraints viewportConstraints) {
                  return SingleChildScrollView(
                    child: ConstrainedBox(
                      constraints: BoxConstraints(
                        minHeight: viewportConstraints.maxHeight,
                      ),
                      child: IntrinsicHeight(
                        child: Column(
                          children: <Widget>[
                            Column(
                              children: [
                                Text(),
                                RaisedButton(),
                                Text(),
                               ],
                            ),

                            Column(
                              children: [
                                Text(),
                                RaisedButton(),
                                Text(),
                               ],
                            ),

                            Column(
                              children: [
                                Text(),
                                RaisedButton(),
                                Text(),
                               ],
                            ),

抛出的错误是这样的:

══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
The following assertion was thrown during performLayout():
BoxConstraints forces an infinite height.
These invalid constraints were provided to RenderIntrinsicHeight's layout() function by the
following function, which probably computed the invalid constraints in question:
  RenderConstrainedBox.performLayout (package:flutter/src/rendering/proxy_box.dart:270:13)
The offending constraints were:
  BoxConstraints(0.0<=w<=330.0, h=Infinity)
The relevant error-causing widget was:
  ConstrainedBox

有这样一条有趣的线:

The following RenderObject was being processed when the exception was fired: RenderConstrainedBox#d1ae8 relayoutBoundary=up15 NEEDS-LAYOUT NEEDS-PAINT NEEDS-COMPOSITING-BITS-UPDATE:
  creator: ConstrainedBox ← _SingleChildViewport ← IgnorePointer-[GlobalKey#f983f] ← Semantics ←
    _PointerListener ← Listener ← _GestureSemantics ←
    RawGestureDetector-[LabeledGlobalKey<RawGestureDetectorState>#d70da] ← _PointerListener ← Listener
    ← _ScrollableScope ← _ScrollSemantics-[GlobalKey#d055d] ← ⋯
  parentData: <none> (can use size)
  constraints: BoxConstraints(0.0<=w<=330.0, 0.0<=h<=Infinity)
  **size: MISSING**
  additionalConstraints: BoxConstraints(0.0<=w<=Infinity, h=Infinity)

提前感谢所有提供帮助的人!

标签: flutterdartscrollflutter-layoutvertical-scroll

解决方案


我已经将我的 listView(以及所有其他可滚动的小部件)包裹在一个列周围。我最初使用一列将所有内容堆叠在一起,但显然 listView 也是如此。


推荐阅读