how to show asset image inside box decoration - flutter

this is my code using network image inside box decoration.It works completely fine.
Container(
decoration: new BoxDecoration(
borderRadius:BorderRadius.circular(24.0),
image: DecorationImage(
image: new NetworkImage(img),
fit: BoxFit.fill,
)
)
),
)
I want to change images from network to asset image so I can use images from assets folder.

Change new NetworkImage(img) to
Image.asset("path/to/image").image
or
AssetImage("path/to/image")
This will load an image from the assets instead of the internet.

Related

How to convert local storage image path into network image url in flutter

I have a local storage image path like:
/Users/newuser/Library/Developer/CoreSimulator/Devices/D7515987-8FDF-47A7-A6E1-A210739CB2A5/data/Containers/Data/Application/DC529635-F924-40DF-B530-85710D23A8D0/Library/Application Support/localstorage_images/2022-05-04/12:50:41.png
I have display this image using
image: FileImage(
url),
fit: BoxFit.cover,
),
here var url contains the local storage image path /Users/newuser/Library/Developer/CoreSimulator/Devices/D7515987-8FDF-47A7-A6E1-A210739CB2A5/data/Containers/Data/Application/DC529635-F924-40DF-B530-85710D23A8D0/Library/Application Support/localstorage_images/2022-05-04/12:50:41.png in File data type
I want to pass this var url in like below:
image: NetworkImage(
url),
fit: BoxFit.cover,
),
my problem was how to convert FileImage type to NetworkImage type because my other images are NetworkImage type it is possible to convert FileImage into Network image in flutter
Thanks in advance.

Invalid image data error: how to display image in Flutter app using aws s3 url?

I'm using this code to display image:
Container(
child: Image(
image: CachedNetworkImageProvider(
"https://zyle.s3.amazonaws.com/IMG_1_v%2BjwOGN%2BK6FmX2wA.png")),
);
But it's showing the error:
Try this:
Image.network("https://zyle.s3.amazonaws.com/IMG_1_v%2BjwOGN%2BK6FmX2wA.png")

Converting base64 to Image in Flutter Error

I have this image as a base64 String which I then want to convert to image in BoxDecoration like so:
Container(
decoration: BoxDecoration(image: DecorationImage(image: Utility.imageFromBase64String(drink.image))),
Here is the function that decodes the String to Image.
Function
class Utility {
static Image imageFromBase64String(String base64String) {
return Image.memory(
base64Decode(base64String),
fit: BoxFit.fill,
);
}}
However, I am getting an error in BoxDecoration:
The argument type 'Image' can't be assigned to the parameter type 'ImageProvider<Object>'
The error is in this part, I suppose I can't put in Image since it asks for ImageProvider:
image: DecorationImage(image: Utility.imageFromBase64String(drink.image))
Any ideas how to make it work? Thanks!
The image property from DecorationImage is of type ImageProvider<object> not Image.
Return a MemoryImage from imageFromBase64String like so
static MemoryImage imageFromBase64String(String base64String) {
return MemoryImage(
base64Decode(base64String)
);
}
Then this.
image: DecorationImage(
image: Utility.imageFromBase64String(drink.image),
fit: BoxFit.fill,
)

FadeInImage with unknown image at runtime

I have a FadeInImage in my app, with the image loaded in response to a user action. This means at runtime, I dont know the image, and just want the FadeInImage to not display anything.
If I pass in null for the image, or an empty string, I get a runtime error.
flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
flutter: The following ArgumentError was thrown resolving an image codec:
flutter: Invalid argument(s): No host specified in URI file:///
Here it the relevant Widget declaration:
Container(
height: 100,
child:FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
image: _d2apiModel.emblemUrl,
fit: BoxFit.fitWidth,
),
(where _d2apiModel is a Provider which is updated based on user actions).
I know I could put a default image in the asset bundle, copy it out of the bundle to the doc storage, get the file URI for the image, and use that as the default but 1) that seems like overkill, 2) i run into some async issues.
It seems like I am missing something obvious, but I cant figure out the approach. Any suggestions would be appreciated.
You can show a regular Image with kTransparentImage while your provider hasn't been updated, and then show your FadeInImage when it updates:
Container(
height: 100,
child: hasProviderBeenUpdated
? Image.memory(
kTransparentImage,
fit: BoxFit.fitWidth,
)
: FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
image: _d2apiModel.emblemUrl,
fit: BoxFit.fitWidth,
),
),

How to fadein a decorationImage in flutter?

I'm trying to fade in a decoration image and can't figure out how.
The image property needs an ImageProvider, while FadeInImage widget is a StatefulWidget.
This is what I've tried to work with:
decoration: BoxDecoration (
image: DecorationImage(
fix: BoxFit.cover,
image: ...
),
)
You're not going to be able to animate a DecorationImage. As you've stated, DecorationImage only provides an ImageProvider, which doesn't really allow for animation (at least as far as I know).
You might be able to write a new AnimatedDecorationImage by taking part of the code from DecorationImage and editing it, but that would be pretty complicated.
What I'd recommend is instead to use a stack to simulate the same thing as DecorationImage. This would allow you to use the FadeInImage widget.
That would look something like this:
Stack(
children: [
FadeInImage(
placeholder: MemoryImage(....),
image: NetworkImage(...),
),
<your widget, I assume a container?>
],
)