通过因子调整动态大小的小部件的尺寸

4

我正在尝试设置一个动态大小的 WidgetheightFactor(用于动画效果),在这种情况下是一个 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包裹在LimitedBoxConstrainedBox中似乎只会改变错误信息中提供的无效约束条件。

MainAxisSize.min添加到Column.mainAxisSize中也没有什么区别。

有没有一种可以避免出现错误的方法呢?


1
你需要提供更多的代码,因为现在我只能说你的FractionallySizedBox正在调整其大小以适应无限高度的某些内容。由于infinity / 2 == infinity,所以你的文本也具有无限高度。你可能会尝试使用Align小部件,但是如果不知道你的小部件是什么,很难确定最好的解决方案。 - rmtmckenzie
好的,已更新更多代码,对不便表示抱歉。 - nesbocaj
如果您能描述一下您试图通过这样做实现什么目标,那将非常有帮助。 - rmtmckenzie
1
好的,“Align” 看起来是个不错的选择,所以我会重新表述问题并添加一个答案。感谢你的建议。 - nesbocaj
好的,那我可以把它作为答案添加进去哈哈。很高兴能帮到你 =)。 - rmtmckenzie
1个回答

4

FractionallySizedBox会根据其父容器自适应大小。由于您在Stack中使用了Column,它实际上没有无限高度的限制。由于 infinity / 2 == infinity,因此您的文本高度也是无限的。

如果您切换到使用align,则可以将对象的大小调整为文本而不是父容器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接