Back to Articles

Intermediate

AnimatedSize: Smoothly Growing and Shrinking Changing Content

· 6 min read

AnimatedSize: Smoothly Growing and Shrinking Changing Content

What Is AnimatedSize?

A card expanding with "read more", an error message appearing below a form, an area that is small while loading and grows when the result arrives... In all of these, the size of the content changes, and by default Flutter applies that change in a single frame. The result is a noticeable jump in the UI.

Whenever the size of its child changes, AnimatedSize animates its own size from the old value to the new one. Its biggest advantage is that you don't have to calculate the target height. AnimatedSize grows or shrinks to whatever space the child takes up.

Column vs AnimatedSize
On the left, a plain Column grows instantly when new lines are added; on the right, the same content grows and shrinks smoothly inside AnimatedSize.

Basic Usage

The most common scenario is expanding and collapsing a truncated text:

class ExpandableDescription extends StatefulWidget {
  const ExpandableDescription({super.key, required this.text});

  final String text;

  @override
  State<ExpandableDescription> createState() => _ExpandableDescriptionState();
}

class _ExpandableDescriptionState extends State<ExpandableDescription> {
  bool _isExpanded = false;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AnimatedSize(
          duration: const Duration(milliseconds: 300),
          curve: Curves.easeInOut,
          alignment: Alignment.topCenter,
          child: Text(
            widget.text,
            maxLines: _isExpanded ? null : 2,
            overflow: _isExpanded ? null : TextOverflow.ellipsis,
          ),
        ),
        TextButton(
          onPressed: () => setState(() => _isExpanded = !_isExpanded),
          child: Text(_isExpanded ? 'Show less' : 'Read more'),
        ),
      ],
    );
  }
}

When maxLines changes, the height of the Text changes. AnimatedSize notices this and closes the gap over 300 ms. Thanks to alignment: Alignment.topCenter, the content stays pinned at the top and the bottom edge slides down.

Parameters

• duration (required): How long the growing animation takes. • reverseDuration: A different duration for shrinking. If omitted, duration is used. • curve: The easing curve. Defaults to Curves.linear. • alignment: Where the child sits inside the box during the animation. Defaults to Alignment.center. For content that expands downward you usually want Alignment.topCenter. • clipBehavior: Defaults to Clip.hardEdge. Anything outside the box is clipped during the animation. • onEnd: Called when the animation completes.

How Does It Work?

AnimatedSize always lays out its child at the new size; only its own box is animated. When growing, the new content is already there and is revealed as the box expands. When shrinking, the old content disappears immediately and the box narrows while clipping. That is why setting clipBehavior to Clip.none usually leads to unwanted overflow.

AnimatedSize is a StatefulWidget and manages its ticker internally. Unlike older versions, you don't need to pass a vsync parameter.

The Most Common Mistake: Tight Constraints

AnimatedSize can only change size if its parent allows it. If the parent imposes a fixed size (a tight constraint), the box of AnimatedSize is locked to that size and you won't see any animation. SizedBox(height: ...) and Expanded inside a Column are typical examples:

// Doesn't work: SizedBox fixes the height (tight constraint)
SizedBox(
  height: 200,
  child: AnimatedSize(
    duration: const Duration(milliseconds: 300),
    child: content,
  ),
)

// Works: there is only an upper bound for the height (loose constraint)
ConstrainedBox(
  constraints: const BoxConstraints(maxHeight: 200),
  child: AnimatedSize(
    duration: const Duration(milliseconds: 300),
    child: content,
  ),
)

If the animation doesn't work, first check which constraints the parent passes down. The Layout Explorer in Flutter DevTools is very helpful for this.

When the Content Changes Too: Combine It with AnimatedSwitcher

AnimatedSize only animates the size. If the child turns into a completely different widget, the new content appears all at once. To add a smooth transition on top of the size change, place an AnimatedSwitcher inside AnimatedSize:

AnimatedSize(
  duration: const Duration(milliseconds: 300),
  curve: Curves.easeInOut,
  child: AnimatedSwitcher(
    duration: const Duration(milliseconds: 300),
    child: _isLoading
        ? const SizedBox(
            key: ValueKey('loading'),
            height: 64,
            child: Center(child: CircularProgressIndicator()),
          )
        : ResultCard(key: const ValueKey('result'), result: _result),
  ),
)

In this example, ResultCard is your own widget. Don't forget to give different keys so AnimatedSwitcher can tell the old and new children apart. As a result, the loading indicator fades out, the result card fades in and the area stretches smoothly to its new height.

A Real Example: An Expandable FAQ Card

class FaqTile extends StatefulWidget {
  const FaqTile({super.key, required this.question, required this.answer});

  final String question;
  final String answer;

  @override
  State<FaqTile> createState() => _FaqTileState();
}

class _FaqTileState extends State<FaqTile> {
  bool _isOpen = false;

  @override
  Widget build(BuildContext context) {
    return Card(
      clipBehavior: Clip.antiAlias,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          ListTile(
            title: Text(widget.question),
            trailing: AnimatedRotation(
              turns: _isOpen ? 0.5 : 0,
              duration: const Duration(milliseconds: 250),
              child: const Icon(Icons.expand_more),
            ),
            onTap: () => setState(() => _isOpen = !_isOpen),
          ),
          AnimatedSize(
            duration: const Duration(milliseconds: 250),
            curve: Curves.easeInOut,
            alignment: Alignment.topCenter,
            child: _isOpen
                ? Padding(
                    padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
                    child: Text(widget.answer),
                  )
                : const SizedBox.shrink(),
          ),
        ],
      ),
    );
  }
}

When the question is tapped, the arrow icon turns half a revolution with AnimatedRotation and the answer opens downward thanks to AnimatedSize. Using SizedBox.shrink() in the closed state makes the card take up only as much space as the title.

Comparison with Alternatives

• AnimatedContainer: Can animate the height, but you have to provide the target value (e.g. height: 240). Not suitable for automatic, content-based height. • AnimatedCrossFade: Cross-fades and animates the size between two fixed children. Handy when there are only two states. • ExpansionTile: A ready-made, Material-styled expandable list item. If you need a custom design, AnimatedSize is more flexible. • SizeTransition: Use it when you need full control with an AnimationController.

Conclusion

AnimatedSize is the shortest solution to the problem of animating an area whose height depends on its content. By wrapping a single widget, you can turn jumpy interfaces into smooth ones. Make sure the parent doesn't pass tight constraints, set the direction of growth with alignment, and combine it with AnimatedSwitcher when the content changes as well.