
Photo grids feel best when a thumbnail grows straight into the full image and shrinks back into its exact cell. The key is to expand the surface to the image's final on-screen rectangle, not the whole screen, so the picture itself morphs without any cross-fade offset.
Expand to the image bounds
Rect fitSquare(ExpandedBoundsContext bounds) {
final area = bounds.safeArea;
final side = area.shortestSide;
return Rect.fromCenter(center: area.center, width: side, height: side);
}
ExpandableAnimation(
mode: const ExpandableAnimationMode.custom(fitSquare),
borderRadius: BorderRadius.circular(10),
backgroundOpacity: 1, // solid black behind the photo
gestureConfig: const ExpandableAnimationGestureConfig(
minScale: 0.7,
dragResistance: 1, // the photo follows the finger exactly
),
semanticLabel: 'Open photo',
expandedBuilder: (context) => PhotoViewer(photo: photo),
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Image(image: photo.thumbnail, fit: BoxFit.cover),
),
)
For non-square photos, compute the rectangle from the image's aspect ratio inside
bounds.safeArea, the same way BoxFit.contain would.
Back navigation
The viewer is a modal route: the Android back button, Escape and a tap on the black background
(dismissOnBackgroundTap) close it, and focus returns to the thumbnail. If the grid scrolled or
the thumbnail was removed while the viewer was open, the image returns to its new position or fades out in
place.
Pinch and double-tap zoom
Wrap the full image in ExpandableAnimationZoomable. It coordinates zooming with
drag-to-dismiss, which a plain InteractiveViewer would block:
expandedBuilder: (context) => ExpandableAnimationZoomable(
maxScale: 4,
doubleTapScale: 2.5,
child: PhotoViewer(photo: photo),
),
- Zoomed out, a downward drag dismisses the viewer as usual.
- Zoomed in, one finger pans the photo; dismissal waits until you zoom back out.
- Two fingers always pinch-zoom; a double tap zooms in around the tapped point, or back out.
- If the viewer closes while zoomed (back button, Escape), the photo zooms out as it shrinks.
Try it
Open the photo gallery demo.