我正在尝试设置动态尺寸的heightFactor
的{{1}}(出于动画目的),在本例中是Widget
:
Text
但是会导致以下异常:
Stack(
children: <Widget>[
Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
FractionallySizedBox(
heightFactor: 0.5,
child: Text(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
),
),
],
),
Positioned.fill(
child: Center(
child: Text(
"Lorem ipsum".toUpperCase(),
),
),
),
],
);
将I/flutter ( 7123): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 7123): The following assertion was thrown during performLayout():
I/flutter ( 7123): BoxConstraints forces an infinite height.
I/flutter ( 7123): These invalid constraints were provided to RenderParagraph's layout() function by the following
I/flutter ( 7123): function, which probably computed the invalid constraints in question:
I/flutter ( 7123): RenderFractionallySizedOverflowBox.performLayout
I/flutter ( 7123): (package:flutter/src/rendering/shifted_box.dart:909:13)
I/flutter ( 7123): The offending constraints were:
I/flutter ( 7123): BoxConstraints(w=272.0, h=Infinity)
包裹在Text
或LimitedBox
中似乎只会更改错误消息中提供的无效约束。
也没有将ConstrainedBox
添加到MainAxisSize.min
似乎有所不同。
有没有一种方法可以避免错误发生?
答案 0 :(得分:4)
A FractionallySizedBox会将其自身大小调整为其父级。因为您正在堆栈中使用一列,所以它实际上对无限高度没有限制。由于infinity / 2 == infinity
,因此您的文本也具有无限的高度。
如果您使用对齐方式,则可以将对象的大小设置为文本大小,而不是父大小。