【问题标题】:BoxConstraints forces an infinite widthBoxConstraints 强制无限宽度
【发布时间】:2019-02-25 19:18:20
【问题描述】:

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

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

【问题讨论】:

    标签: flutter dart flutter-layout width


    【解决方案1】:

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

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

    https://stackoverflow.com/a/45990477/4652688

    【讨论】:

      【解决方案2】:

      错误原因:

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

      【讨论】:

      • 我有一个 ListView 抛出此异常并将其包装在 SizedBox 中并设置宽度为我工作!谢谢。
      • Flexible() 小部件拯救了我的一天!。感谢您的回答。
      【解决方案3】:

      仅当您尝试为 RowColumnContainer 定义无限 widthheight 时才会发生这种情况,该 TextField 是想要使用所有空间的父级 TextField .

      要解决这个问题,你应该用FlexibleExpanded 包裹你的TextField

      这样做是为了解决上述问题。

       Expanded(
         child: txtFirstName,
       ),
      
       Flexible(
         child: txtLastName,
       ),
      

      完整示例

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

      【讨论】:

        猜你喜欢
        • 2020-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-22
        • 2020-04-27
        • 2019-11-04
        • 1970-01-01
        相关资源
        最近更新 更多