Full-screen image viewer with zoom and save-to-gallery in Flutter (no plugins)
flutterandroidimage-viewerinteractiveviewermediastoremethodchannelimmersive
TLDR's DM image viewer (v1.28.0) opens an image full screen, supports pinch and double-tap zoom, saves to the phone's gallery (or downloads on web), and offers to open the saved copy. It uses only Flutter built-ins plus a small native MethodChannel, which avoided the plugin JVM-target problems described in Making a Flutter app an Android share target (native, no plugin).
Viewer
- Immersive mode on open (
SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive)); a single tap toggles the app chrome and system bars; restoreedgeToEdgeindispose. InteractiveViewerwithminScale 0.5,maxScale 4.0for pinch.- Double-tap zoom: capture the tap point in
onDoubleTapDown, then animate aTransformationControllerbetween identity and about 2.5x centered on that point over 200 ms withCurves.easeOut(anAnimationControllerdriving aMatrix4Tween). Build the target matrix explicitly; theMatrix4.translate/scalehelpers are deprecated. - Pass the original filename through so saves keep it.
Saving
- Fetch the bytes over HTTP (this needs CORS on web; TLDR's upload route carries it).
- Web: trigger a blob download.
- Android, API 29+: insert into
MediaStoreunderPictures/<app>via a native channel; scoped storage needs no permission. Below API 29, fall back toWRITE_EXTERNAL_STORAGE, declared withandroid:maxSdkVersion="28"so newer versions never ask. - Return the saved
content://URI to Dart. The "Saved" snackbar's View action calls nativeACTION_VIEWon it withFLAG_GRANT_READ_URI_PERMISSIONandFLAG_ACTIVITY_NEW_TASK; Android shows its chooser if there is no default gallery. CatchActivityNotFoundException.
Cropping without a plugin (avatars)
For a circular avatar editor: an InteractiveViewer (constrained: false, boundaryMargin zero,
scale 1 to 6) inside a RepaintBoundary, the circular mask painted outside the boundary so it is
not captured, a 90-degree rotate button, and export with
boundary.toImage(pixelRatio: 512 / viewportSize) to a 512x512 PNG. Decode the source down to at most
2048 px on the long edge first. image_cropper was rejected (native plugin, JVM-target risk) and
crop_your_image was not worth a dependency for about 150 lines.