Israel Obanijesu
Israel Obanijesu

Reputation: 696

Flutter: SizedBox with Dynamic Height

I am trying to create a custom list (alternative to using ListTile widget).

My code looks like this:

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

CommentDescription Widget

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

The problem I have is that, the code is using SizedBox widget to wrap each tile, which requires a fixed height. But I want something that can work with dynamic height because the height of the content cannot be predetermined. Please how do I approach this.

Note: When I remove SizedBox widget, i get this error message in the console:

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. 

Upvotes: 7

Views: 31533

Answers (4)

Arvid
Arvid

Reputation: 53

For anyone coming here and simply wanting to find a way of setting the SizedBox to the intrinsic height of its child, like me, just set the height to null. For example, I was animating a widget from height = 0 to full height. Then this would work:

AnimatedSize(
  duration: const Duration(milliseconds: 700),
  curve: Curves.fastOutSlowIn,
  child: SizedBox(
    height: conditionFalse ? 0 : null,
    child: ClipRect(
      child: MyWidget(),
    ),
  ),
),

Upvotes: 0

Vicky Leekha
Vicky Leekha

Reputation: 61

Just use shrink Wrap=true in list property and wrap with container , sizeBox Heigth and Width will be auto adjusted

Container(child:ListView.separated(
itemCount:1
                                  shrinkWrap: true,
                                  itemBuilder: (context, index) {
                                    return Container();
}
)

Upvotes: 2

dshukertjr
dshukertjr

Reputation: 18690

You should simply remove all the Expanded widget in CommentDescription. Expanded can be used only when the width or height of the parent Row or Column is fixed.

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

Upvotes: 3

dshukertjr
dshukertjr

Reputation: 18690

I assume you want to limit the size of the thumbnail. If this is the case, try this:

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

Upvotes: 1

Related Questions