Flutter has two well-known ways to connect a small element to a larger view: the built-in
Hero widget and OpenContainer from the official animations package.
Both are good tools. This page explains where ExpandableAnimation differs, so you can pick the
right one.
Side by side
| Hero | OpenContainer | ExpandableAnimation | |
|---|---|---|---|
| Model | Shared element flies between two routes | Container transforms into a new route | Container transforms into an expanded view (a modal route) |
| Setup | Matching tags in both pages | One widget, closed and open builders | One widget, child and expanded builder |
| Target | A full page | A full page | Fullscreen, dialog, sheet or custom bounds |
| Drag-to-dismiss | Not built in | Not built in | Built in, with scroll coordination |
| Spring physics | No | No | Optional |
| Controller | Navigation | openContainer callback | ExpandableAnimationController |
| Dependency | Flutter SDK | animations package | expandable_animation package |
When Hero is the better choice
Use Hero when one element — an avatar, a product photo — should travel between two
full pages that are otherwise independent, and the destination is a normal navigation push.
When OpenContainer is the better choice
Use OpenContainer when you want the Material container transform pattern exactly as the
Material motion guidelines describe it, and a tap-to-open, back-to-close flow is enough.
When ExpandableAnimation fits
- You want users to dismiss the view by dragging it, including from scrollable content.
- You want to expand into a dialog or a partial sheet, not only a page.
- You want spring motion, interruptible transitions and a controller with phase and progress.
Migrating from OpenContainer
// Before
OpenContainer(
closedShape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
closedBuilder: (context, open) => ProductCard(product: product),
openBuilder: (context, close) => ProductDetails(product: product),
)
// After
ExpandableAnimation(
borderRadius: BorderRadius.circular(16),
expandedBuilder: (context) => ProductDetails(product: product),
child: ProductCard(product: product),
)
Where your open page called close(), call
ExpandableAnimation.of(context).collapse() or Navigator.pop(context).
Try it
Compare the feel in the live demo, or start with the card to fullscreen guide.