无法在行内扩展

12
我正在尝试扩展放置在行中的文本小部件,但是我遇到了错误。
代码:
class MainContainer extends StatelessWidget
{
  @override
  Widget build(BuildContext context)
  {
    return Column (
      children: <Widget>[
        Container(

        ),
        Container(
          child: Row(
            children: <Widget>[
            Row(
              children: <Widget>[
              Text("label1"),
              Expanded(child: Text("label2")) // Problem is here

            ],)

          ],),

        ),
        Container(
          child: Row(children: <Widget>[
            Text("Some Text")
          ],),
        ),
      ],    
    );
  }

}

错误:

I/flutter(5480):═══╡渲染库捕获的异常╞════════════════════════════════════════════════════════ I/flutter(5480):在执行布局期间抛出以下断言: I/flutter(5480):RenderFlex子项具有非零flex,但传入的宽度约束未定义。 I/flutter(5480):当行位于不提供有限宽度约束的父级中时,例如如果它位于水平可滚动区域中,则它将尝试沿水平轴缩小包装其子项。在子项上设置灵活度(例如使用扩展)表示子项将扩展以填充水平方向上剩余的空间。 I/flutter(5480):这两个指令是相互排斥的。如果父项要收缩包装其子项,则子项不能同时扩展以适合其父项。 I/flutter(5480):考虑将mainAxisSize设置为MainAxisSize.min,并对灵活子项(使用Flexible而不是Expanded)使用FlexFit.loose fits。这将允许灵活子项将自己调整为比它们本来应该占用的无限剩余空间更小的大小,然后将导致RenderFlex收缩包装子项,而不是扩展以适合父级提供的最大约束。

2个回答

25
问题在于您的

4

由于需要为第二行设置宽度,因此应该将其放在Expanded中。这也是你收到width constraints are unbounded错误的原因。

你的代码应该是:

@override
  Widget build(BuildContext context) {
    return Column (
      children: <Widget>[
        Container(

        ),
        Container(
          child: Row(
            children: <Widget>[
              Expanded(
                child: Row(
                  children: <Widget>[
                    Text("label1"),
                    Expanded(child: Text("label2")) // Problem is here

                  ],),
              )

            ],),

        ),
        Container(
          child: Row(children: <Widget>[
            Text("Some Text")
          ],),
        ),
      ],
    );
}

2
你的第二行应该是“Expanded”。我认为默认情况下,行应该占据所有可用空间,不知道我是否错了? - Dmitry Bubnenkov

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