
Drag-to-dismiss lets users close a fullscreen view the way they opened it: directly, with their finger.
It is on by default in ExpandableAnimation.
How the gesture works
- The user drags the expanded view downward. It follows the finger (scaled by
dragResistance), shrinks towardminScale, rounds its corners and the background fades. - On release it dismisses if it was dragged at least
dismissThreshold× its height, or flung downward faster thanvelocityThreshold. - Otherwise it springs back, carrying the release velocity. A fast upward fling always cancels.
- When it dismisses, the collapse starts from exactly where the finger left it, at the speed the
finger was moving (set
matchReleaseVelocity: falseto use the plain reverse animation).
ExpandableAnimation(
enableDragToDismiss: true,
gestureConfig: const ExpandableAnimationGestureConfig(
dismissThreshold: 0.2,
velocityThreshold: 700,
minScale: 0.85,
dragResistance: 0.6,
enableHapticFeedback: true,
matchReleaseVelocity: true,
),
expandedBuilder: (context) => const DetailsPage(),
child: const SummaryCard(),
)
An interactive page transition
A regular Flutter page route plays its transition from start to finish once it begins. Here the dismissal is an interactive page transition: the finger drives the expanded view directly, frame by frame, and can pull it back the other way at any time. Nothing is decided until release.
- Follows the finger: the drag offset, scale, corner radius and background fade are computed from the current drag on every frame.
- Reversible: dragging back toward the start restores the full view; an upward fling cancels.
- Continuous on release: the collapse continues from the dragged position at the finger's speed instead of restarting a fixed animation.
- Interruptible: calling
controller.expand()during a collapse (orcollapse()during an expansion) reverses from the current position. - Observable:
controller.progressandcontroller.isDraggingreport the gesture, so other widgets can react (see below).
The same expanded view is a real route, so the back button, Escape and PopScope keep
working alongside the gesture.
Directions
Dismissal is downward by default. Allow any combination of directions:
gestureConfig: const ExpandableAnimationGestureConfig(
dismissDirections: {
ExpandableAnimationDismissDirection.down,
ExpandableAnimationDismissDirection.up,
ExpandableAnimationDismissDirection.left,
ExpandableAnimationDismissDirection.right,
},
),
Each drag locks to the axis it starts on. The surface shrinks away from the edge it leaves, so a rightward drag anchors at the left edge and slides right.
Scrollable content
Vertical lists inside the expanded view keep scrolling normally. The dismiss gesture only starts when a
list that is already at its top is pulled further down, and it hands control back if the user drags up
again. Horizontal scrollables, such as a PageView, are only involved when left or
right is allowed: then swiping past the first or last page dismisses. Set
dismissFromScrollableContent: false to dismiss only from non-scrolling areas.
Preventing a dismissal
PopScope(
canPop: !hasUnsavedChanges,
onPopInvokedWithResult: (didPop, result) {
if (!didPop) showDiscardDialog(context);
},
child: const EditForm(),
)
With canPop: false, drags spring back and background taps, Escape and the back button are
ignored; controller.collapse() still works.
Observing the drag
ListenableBuilder(
listenable: controller,
builder: (context, _) => Opacity(
opacity: controller.isDragging ? controller.progress : 1,
child: const Toolbar(),
),
)
Try it
Change the thresholds in the playground, or drag the article cards and the music player. Related: iOS-style animations.