按大小调整动态大小的小部件的大小

时间:2018-08-14 22:19:35

标签: user-interface layout dart flutter

我正在尝试设置动态尺寸的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) 包裹在TextLimitedBox中似乎只会更改错误消息中提供的无效约束。

也没有将ConstrainedBox添加到MainAxisSize.min似乎有所不同。

有没有一种方法可以避免错误发生?

1 个答案:

答案 0 :(得分:4)

A FractionallySizedBox会将其自身大小调整为其父级。因为您正在堆栈中使用一列,所以它实际上对无限高度没有限制。由于infinity / 2 == infinity,因此您的文本也具有无限的高度。

如果您使用对齐方式,则可以将对象的大小设置为文本大小,而不是父大小。