BoxConstraints强制宽度无限

71

当我在列中添加一行时,出现了错误。我收到以下错误:

I/flutter ( 6449): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 6449): The following assertion was thrown during performLayout():
I/flutter ( 6449): BoxConstraints forces an infinite width.
I/flutter ( 6449): These invalid constraints were provided to RenderAnimatedOpacity's layout() function 

还有参考我的代码:

return new Scaffold(
      backgroundColor: whiteColor,
      body: new Column(
        children: <Widget>[
          imgHeader,
          lblSignUp,
          txtEmail,
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              txtFirstName,
              txtLastName
            ],
          ),
        ],
      ),
    );
3个回答

98

太好了!“灵活”正好产生了我想要的行为。谢谢!=^,^= - Kira Resari

64

错误原因:

TextField 水平方向上扩展,Row 也是如此,因此我们需要限制 TextField 的宽度,有很多方法可以做到。

  1. 使用 Expanded

 Row(
  children: <Widget>[
    Expanded(child: TextField()),
    // more widgets
  ],
)
使用Flexible
Row(
  children: <Widget>[
    Flexible(child: TextField()),
    // more widgets
  ],
)
  • 将其包装在ContainerSizedBox中并提供width

  • Row(
      children: <Widget>[
        SizedBox(width: 100, child: TextField()),
        // more widgets
      ],
    )       
    

    1
    我有一个ListView,它抛出了这个异常,将其包装在SizedBox中并设置宽度对我很有帮助!谢谢。 - joshpetit
    1
    Flexible()小部件救了我的一天!感谢您的答案。 - Soorya
    给出原因帮助我解决了另一个(ListView)问题。 - Paul Sumpner

    3

    在使用Positioned小部件的时候,我遇到了这个错误:

    Positioned(
      left: _left,
      child: MyWidget(...)
    )
    

    通过添加bottomtop参数解决,例如:

    Positioned(
      left: _left,
      bottom: 0,
      top: 0,
      child: MyWidget(...)
    )
    

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