0

I'm building an expansible card in flutter, which I wanto to animate. I want the parent Container to have a limited height while the card is collapsed, and to have whatever height is necessary for the child when it's expanded. I can reach this result using maxHeight: double.infinity:

Container(
          alignment: Alignment.topCenter,
          clipBehavior: Clip.hardEdge,
          constraints: isExpanded
              ? BoxConstraints(maxHeight: double.infinity)
              : BoxConstraints(maxHeight: 76),
          decoration: BoxDecoration(),
          child: widget.child,
        ),
      )

But whatever maxHeight value I use other than double.infinity causes the parent to have that exact height, instead of just having the height needed by the child. I can't use double.infinity because that's not animatable. I've tried following this answer's tips, but whatever I wrap the child with, the parent always grows. I've tried changing the child, but even if I use maxHeight on the child itself, the parent always grows to the defined maxHeight.

Why does this happen? Shouldn't maxHeight only cause the parent to expand if needed? How come it works correctly with double.infinity, but doesn't with anything else?

Also, I have tried reaching this result with AnimatedSize, it sort of works. When it is expanding, it works perfectly, but when the child is collapsing, it collapses instantly, and the AnimatedSize's animation follows after.

1 Answers1

0

Edit: I see what you mean, you want to use AnimatedContainer, but in order to be animated like you want it, it requires you to specify the height. double.infinity won't do in this case, the error message you're getting is correct. There are ways to get widget height.. I suggest you check this answer: https://stackoverflow.com/a/49650741/7248289

Also, this one might help: https://stackoverflow.com/a/54173729/7248289

galloper
  • 813
  • 1
  • 9
  • 17
  • I'm not sure I understand. I can achieve this result already with `maxHeight: double.infinity`. The problem is that, using infinity, I can't animate. When I try it with `AnimatedContainer` I get the error: `AnimatedContainer cannot interpolate between finite and unbounded constraints`. I also added clarification on why AnimatedSize doesn't work either. – Thiago Pereira Maia Mar 19 '21 at 21:12