BoxConstraints 强制无限宽度

BoxConstraints forces an infinite width

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

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
            ],
          ),
        ],
      ),
    );

如果您在 Row 中使用 TextField,那么您需要使用 FlexibleExpanded

此答案中提供了更多详细信息。

错误原因:

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

  1. 使用Expanded

     Row(
      children: <Widget>[
        Expanded(child: TextField()),
        // more widgets
      ],
    )
    
  2. 使用Flexible

    Row(
      children: <Widget>[
        Flexible(child: TextField()),
        // more widgets
      ],
    )
    
  3. 将其包裹在ContainerSizedBox中并提供width

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

仅当您尝试为作为父项的 RowColumnContainer 定义无限 widthheight 时才会发生这种情况想要使用所有 space.

TextField

要解决这个问题,您应该将 TextField 换成 FlexibleExpanded

这样做是为了解决上述及以后的问题。

 Expanded(
   child: txtFirstName,
 ),

 Flexible(
   child: txtLastName,
 ),

完整示例

 Column(
    children: <Widget>[
      imgHeader,
      lblSignUp,
      txtEmail,
      Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Expanded(
            child: txtFirstName,
          ),
          Flexible(
            child: txtLastName,
          ),
        ],
      ),
    ],
  )