Flutter Making an Image.file() image into an ImageProvider() for zoomable photos
Asked Answered
T

4

5

I've created an app that downloads a bunch of information for off-line use, much of which are photos. In the app, I'd like to use a widget that allows me to have a pan/zoom effect, but the ones I've found don't seem to play well with Image.file() and instead use ImageProviders.

For example, Photo_View only accepts AssetImage, and NetworkImage images which both inherit from ImageProvider, while Image.file() inherits from Diagnosticable. All of the packages that I've seen operate in basically the same way.

So, my question is how to convert Image.file() data into a ImageProvider format.

Throes answered 9/1, 2020 at 1:27 Comment(0)
P
16

Instead of Image.file() use FileImage() that is type of ImageProvider.

From Doc:

Decodes the given File object as an image, associating it with the given scale.

See also:

Image.file for a shorthand of an Image widget backed by FileImage.

I think you can also use ExactAssetImage with Image File path(ex: image.path) but it should be in AssetBundle. Here what doc says about that:

ExactAssetImage class

Fetches an image from an AssetBundle, associating it with the given scale.

This implementation requires an explicit final assetName and scale on construction, and ignores the device pixel ratio and size in the configuration passed into resolve. For a resolution-aware variant that uses the configuration to pick an appropriate image based on the device pixel ratio and size, see AssetImage.

read more.

Pimental answered 9/1, 2020 at 1:38 Comment(1)
I don't think that ExactAssetImage would work in this case, since it seems like it only works with images that are bundled with the app, and not files that have been downloaded. HOWEVER, FileImage() worked like a charm. Thank you!Throes
P
3

First import dart:io; then use

PhotoView(imageProvider: FileImage(File(path)))
Passerine answered 26/8, 2020 at 13:8 Comment(0)
C
2

This is what worked for me:

import 'dart:io' as io;

FileImage(io.File(_imageFile!.path)) as ImageProvider

where _imageFile is an XFile

Carvelbuilt answered 17/2, 2022 at 11:36 Comment(0)
H
0

Use FileImage(File(imagePath)) widget for showing file image with ImageProvider.

Happening answered 13/7, 2022 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.