1. Install the package
flutter pub add expandable_animation
Then import it:
import 'package:expandable_animation/expandable_animation.dart';
The package supports Flutter 3.27 and later and has no runtime dependencies besides the Flutter SDK.
2. Wrap a widget
ExpandableAnimation needs two things: the child to show in place, and an
expandedBuilder that builds the larger view.
ExpandableAnimation(
borderRadius: BorderRadius.circular(20),
expandedBuilder: (context) => const ProductDetails(),
child: const ProductCard(),
)
Tap the card: it lifts out of the layout and morphs into ProductDetails. Drag the page down,
press the back button, press Escape, or call Navigator.pop(context) to collapse it.
3. Make the morph seamless
The morphing surface sits behind both widgets. Give it the same corner radius and color as your card, and the color of your detail page, so the transition looks like one object changing shape:
ExpandableAnimation(
borderRadius: BorderRadius.circular(20),
collapsedColor: Theme.of(context).colorScheme.surfaceContainerHigh,
expandedColor: Theme.of(context).colorScheme.surface,
expandedBuilder: (context) => ProductDetails(product: product),
child: ProductCard(product: product),
)
4. Add a close button
Drag-to-dismiss is convenient but not discoverable for everyone. Add a visible close button inside the expanded view:
IconButton(
tooltip: 'Close',
icon: const Icon(Icons.close),
onPressed: () => ExpandableAnimation.of(context).collapse(),
)
Next steps
- Read the documentation for modes, the controller, physics and gestures.
- Browse the examples, from music players to App Store-style cards.
- Experiment in the transitions playground and copy the generated code.