我正在尝试设置一个动态大小的 Widget
的 heightFactor
(用于动画效果),在这种情况下是一个 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
添加到Column.mainAxisSize
中也没有什么区别。
有没有一种可以避免出现错误的方法呢?
infinity / 2 == infinity
,所以你的文本也具有无限高度。你可能会尝试使用Align小部件,但是如果不知道你的小部件是什么,很难确定最好的解决方案。 - rmtmckenzie