BoxConstraints强制无限宽度

时间:2018-09-21 11:25:29

标签: flutter flutter-layout dart-2

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

def ping(self,hostname):
        time_check = datetime.now()
        data = ""
        while not "Success" in data:
            time.sleep(1)
            data = self.pingCheck("ping 10.10.10.1 count 5")
            if (datetime.now()-time_check).seconds > 100:
                return False
        return True

这也是我的代码供参考:

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 

3 个答案:

答案 0 :(得分:14)

错误原因:

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

答案 1 :(得分:13)

如果您在function getAnchorTags(link, image) { let anchors = ($('<a/>', { 'href': link })).html('<img src="'+image+'">'); return anchors; } $(function () { $(rowEblock().append(getEblock()).appendTo('.panel-footer')); $('#anchorAppender').append(getAnchorTags('#','image_src_path'), getAnchorTags('#','image_src_path'), getAnchorTags('#','image_src_path')); }); 内部使用TextField,则需要使用RowFlexible

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

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

答案 2 :(得分:2)

仅当您尝试为widthheightRow的父对象定义无限ColumnContainer时,才会发生这种情况想要使用所有空间的TextField

要解决此问题,您应该用TextFieldFlexible包装Expanded

执行此操作可以解决上述问题。

 Expanded(
   child: txtFirstName,
 ),

 Flexible(
   child: txtLastName,
 ),

完整示例

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