当我的自定义窗口小部件位于扩展窗口小部件(扩展>行> CustomWidget)内部的行中时,它的效果很好。但是,如果我随后将自定义窗口小部件包装在行内的一列内(展开>行>列> CustomWidget),则会引发一个断言:“ BoxConstraints强制无限宽度和无限高度。”
我了解断言的要旨,但我不明白为什么在行和自定义小部件之间添加列会导致问题出现,而以前根本就不存在。如果没有该列,则必须将约束从父项传递到树状结构的子项。将专栏放在那里似乎打破了这一链条。
我尝试过:
设置列mainAxisSize:MainAxisSize.min
在“展开”(在“行”和“列”小部件之间)中包装列
使用LayoutBuilder创建一个SizedBox,其高度为BoxConstraints的maxHeight,用于包装列
使用ConstrainedBox(也用于LayoutBuilder)
Expanded(
flex: 2,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('Title goes here'),
Stack(
alignment: Alignment.center,
children: <Widget>[
restWidget,
if (!_appBarStopwatch.isRunning)
...startButton,
],
),
],
),
],
),
),
restWidget
是一个引用类型为RestIndicator的自定义窗口小部件的变量
Expanded
是以下树的“ SafeArea”>“ Padding”>“ Column”的子代。
flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performLayout():
flutter: BoxConstraints forces an infinite width and infinite height.
.
.
.
flutter: The following RenderObject was being processed when the exception was fired:
flutter: RenderConstrainedBox#7d17f relayoutBoundary=up10 NEEDS-LAYOUT NEEDS-PAINT
flutter: creator: SizedBox.expand ← Stack ← LayoutBuilder ← WorkoutRestIndicator ← StreamBuilder<Duration>
flutter: ← StreamBuilder<RestStep> ← RestIndicator ← Stack ← Column ← Row ← Expanded ← Column ← ⋯
flutter: parentData: not positioned; offset=Offset(0.0, 0.0) (can use size)
flutter: constraints: BoxConstraints(unconstrained)
flutter: size: MISSING
flutter: additionalConstraints: BoxConstraints(biggest)
从树中删除Column
小部件可以解决该错误,但是我无法创建所需的设计。有没有一种方法可以重新建立约束,使其像我介绍《专栏》之前一样工作?我需要放弃使用扩展功能吗?