【问题标题】:Flutter: SizedBox with Dynamic HeightFlutter:具有动态高度的 SizedBox
【发布时间】:2020-01-03 09:42:21
【问题描述】:

我正在尝试创建一个自定义列表(替代使用 ListTile 小部件)。

我的代码如下所示:

Widget build(BuildContext context) {
  return Padding(
    padding: EdgeInsets.symmetric(vertical: 10.0),
    child: SizedBox(
      height: 70,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          thumbnail,
          Expanded(
            child: Padding(
              padding: EdgeInsets.fromLTRB(10.0, 0.0, 2.0, 0.0),
              child: CommentDescription(
                author: author,
                comment: comment,
                createdAt: createdAt,
                isLiked: isLiked,
                likesCount: likesCount,
              )
            )
          ),
          menu
        ],
      )
    )
  );
}

评论描述小部件

Widget build(BuildContext context) {
  return Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      Expanded(
        flex: 2,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              '$author',
              maxLines: 1,
              style: TextStyle(fontWeight: FontWeight.bold)
            ),
            Padding(padding: EdgeInsets.only(bottom: 4.0)),
            Text('$comment')
          ],
        )
      ),
      Expanded(
        flex: 1,
        child: Row(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: 8.0),
              child: Text(
                '$createdAt',
                style: TextStyle(
                  fontSize: 12.0,
                  color: Colors.grey
                )
              )
            ),
            IconButton(
              onPressed: () => print('liking comment'),
              icon: Icon(
                isLiked ? Icons.favorite : Icons.favorite_border,
                color: Colors.red,
                size: 16.0
              )
            ),
            Padding(
              padding: EdgeInsets.only(top: 8.0),
              child: Text(
                '$likesCount',
                style: TextStyle(
                  color: Colors.grey,
                  fontSize: 12.0
                )
              )
            )
          ],
        )
      )
    ],
  );
}

我遇到的问题是,代码使用SizedBox 小部件来包装每个图块,这需要固定高度。但是我想要一些可以使用动态高度的东西,因为无法预先确定内容的高度。请问我该如何处理。

注意:当我删除 SizedBox 小部件时,我在控制台中收到此错误消息:

The following assertion was thrown during performLayout(): RenderFlex children
have non-zero flex but incoming height constraints are unbounded. When a column
is in a parent that does not provide a finite height constraint, for example if 
it is in a vertical scrollable, it will try to shrink-wrap its children along 
the vertical axis. Setting a flex on a child (e.g. using Expanded) indicates 
that the child is to expand to fill the remaining space in the vertical 
direction. These two directives are mutually exclusive. If a parent is to
shrink-wrap its child, the child cannot simultaneously expand to fit its parent. 
Consider setting mainAxisSize to MainAxisSize.min and using FlexFit.loose fits 
for the flexible children (using Flexible rather than Expanded). This will allow 
the flexible children to size themselves to less than the infinite remaining 
space they would otherwise be forced to take, and then will cause the RenderFlex 
to shrink-wrap the children rather than expanding to fit the maximum constraints 
provided by the parent. 

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您应该简单地删除 CommentDescription 中的所有 Expanded 小部件。 Expanded 只能在父 Row 或 Column 的宽度或高度固定时使用。

    Widget build(BuildContext context) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(
                '$author',
                maxLines: 1,
                style: TextStyle(fontWeight: FontWeight.bold)
              ),
              Padding(padding: EdgeInsets.only(bottom: 4.0)),
              Text('$comment')
            ],
          ),
          Row(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.only(top: 8.0),
                child: Text(
                  '$createdAt',
                  style: TextStyle(
                    fontSize: 12.0,
                    color: Colors.grey
                  )
                )
              ),
              IconButton(
                onPressed: () => print('liking comment'),
                icon: Icon(
                  isLiked ? Icons.favorite : Icons.favorite_border,
                  color: Colors.red,
                  size: 16.0
                )
              ),
              Padding(
                padding: EdgeInsets.only(top: 8.0),
                child: Text(
                  '$likesCount',
                  style: TextStyle(
                    color: Colors.grey,
                    fontSize: 12.0
                  )
                )
              )
            ],
          )
        ],
      );
    }
    

    【讨论】:

    • 完美。谢谢你。您说“扩展只能在父列或行的高度固定时使用”。但是在第一个小部件中,Expanded 在没有任何固定高度的 Row 中使用,并且仍在工作。你能解释一下为什么吗?
    • @IsraelObanijesu 抱歉,我的描述有误。对于 Row 小部件,宽度必须是固定的。对于 Column,高度必须是固定的。
    【解决方案2】:

    我假设您想限制缩略图的大小。如果是这种情况,试试这个:

      Widget build(BuildContext context) {
        return Padding(
          padding: EdgeInsets.symmetric(vertical: 10.0),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              SizedBox(
                height: 70,
                child: thumbnail,
              ),
              Expanded(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(10.0, 0.0, 2.0, 0.0),
                  child: CommentDescription(
                    author: author,
                    comment: comment,
                    createdAt: createdAt,
                    isLiked: isLiked,
                    likesCount: likesCount,
                  ),
                ),
              ),
              menu
            ],
          ),
        );
      }
    

    【讨论】:

    • 您好,我看到您不理解我的问题或我想要达到的目标。这是代码在应用程序中的样子:imgur.com/hHrul8k 如该图所示,最后一条评论有溢出问题,因为长度大于大小的框高度,所以我试图动态处理它。缩略图大小很好。另外,我认为您的代码不起作用,因为我之前尝试删除 SizedBox 并引发错误,所以我认为 Expanded 需要某种边界
    • @IsraelObanijesu 您不需要为 ListView 的子项设置固定高度。请尝试我的代码,如果您遇到错误,请告诉我您遇到了什么错误。
    • @IsraelObanijesu 你遇到了什么错误?问题可能出在 CommentDescription 小部件中。
    • 是的,这个问题需要更多细节,我现在将提供CommentDescription 小部件的代码,flutter 给了我一些描述性的错误消息,我认为你可能能够理解...... ..我现在将编辑帖子
    猜你喜欢
    • 2012-11-11
    • 2019-09-10
    • 1970-01-01
    • 2021-08-18
    • 2020-02-07
    • 2012-10-19
    • 2016-04-21
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多