Use a Map with AssetImage - flutter

I have a scenario where I would like to load an image based on the text that is chosen to be displayed on the app.
For example, I have a map that looks like this:
final _activity = const [
{'Activity': 'Rock Climbing','Image':'assets/images/rockclimbing.jpg'},
{'Activity': 'Running','Image':'assets/images/running.jpg'},
];
If I want to display the text related to an Activity in a Text Widget I can do the following:
Text(
activity[activityNumber]['Activity'],
),
However, I would also like to display the corresponding image, here is what I have tried that does not work:
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: const DecorationImage(
image:AssetImage (activity[activityNumber]['Image']),
fit: BoxFit.cover,
),
I have also tried wrapping this in quotes, curly braces etc but I cannot make this work.

The way you are doing should work:
Container(
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
image: DecorationImage(
image: AssetImage(_activity[activityNumber]['Image']),
fit: BoxFit.cover,
),
),
);
Have you considered using Image.asset? Something like: Image.asset(_activity[activityNumber]['Image'])

Related

Flutter Error: type 'Image' is not a subtype of type 'ImageProvider<Obejct>' in type cast - when trying to use a GIF as background

My goal is to use a GIF as the background.
I tried this:
return Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: true? Image.asset(
"images/gif_day.mp4",
height: 125.0,
width: 125.0,
) as ImageProvider :
const AssetImage('assets/images/Environment.png'),
),
),
)
But I get the error:
type 'Image' is not a subtype of type 'ImageProvider<Obejct>' in type cast
I think this is a known issue and I tried the solutions from other posts with the same problem, but I can't get it to work.
you can not assign Image to the ImageProvider try this instead
return Container(
decoration: BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: true? AssetImage(
"images/gif_day.gif",
)
const AssetImage('assets/images/Environment.png'),
),
),
)
if you want to use gif as image this is the website convert your video to gif here and use it as AssetImage widget

Set container width according to image.network widh in flutter?

i want to fix my container size(width) according to the image which is getting from api(image.network..).So, how can i go for this?
i am trying to set dynamic width of container according to image which i get from image.network() in flutter.
You will get overflow error in your device when the image was bigger than your device screen. I recommend you to use this
Container(
width: 100,
height: 100,
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.cover,
image: NetworkImage(
'image link'),
),
),
),

Flutter - Change image opacity of decoration image in container not going smooth

I have two containers 'Stack'ed one on top of another.
Both use decorationImage as a background. The one on TOP looks like the following:
Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
decoration: BoxDecoration(
color: Colors.transparent,
image: widget.imageAfter.isEmpty
? const DecorationImage(image: AssetImage('images/camera.png'))
: DecorationImage(
image: MemoryImage(base64Decode(widget.imageAfter)),
colorFilter: ColorFilter.mode(Colors.transparent.withOpacity(OPACITY), BlendMode.dstATop),
fit: BoxFit.cover),
border: Border.all(color: Colors.black, width: 1.0)
),
),
I change the value of opacity using a slider. when it goes 1, I can only see the image on top and at value 0 I can see the image at the bottom.
The problem I am facing is that the transition of the image transparency is not smooth when I am moving the slider. I get the result when I stop moving the slider but I am unable to see the transition in a smooth way. I guess this is because flutter is not able to draw the images in time. Is there way to do it?

Why does my image lose its gradient in flutter?

So I was trying to create a welcome page when I got this problem. At first, everything was fine, but when I insert an image using DecorationImage, it looks like my image has an error in the color gradient. The real image look like this:
But when i run my app, i get this:
I use PageView.Builder in Scaffold to show my images. My code look like this:
return Scaffold(
body: PageView.builder(
scrollDirection: Axis.vertical,
itemCount: img.length,
itemBuilder: (_,index){
return Container(
width: double.maxFinite,
height: double.maxFinite,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
"assets/image/"+img[index]
),
fit: BoxFit.cover
)
),
I put all my image names in a list and use the index to call each images.
The goldfish looks pretty much the same but the gradient at the bottom is disturbed. At first, I thought it was because I was using a low spec virtual device, but then I tried with a different virtual device and still got the same result. Why this happened? Does anyone know where I went wrong?
Edit: i try to add FilterQuality
return Container(
width: double.maxFinite,
height: double.maxFinite,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
"assets/image/"+img[index]
),
fit: BoxFit.cover,
filterQuality:FilterQuality.high
)
),
But nothing happen. Did I put it wrong?
i tried using real device and it works, but i don't know why

How to use a custom class for decoration?

I want to use a background image for every page in my Flutter app. Therefore, I want to have a class that I can use to return a BoxDecoration to call it from every new page that I create. For the moment, I'm using a simple global function backgroundImage() that returns a BoxDecoration. Is there a way to have a custom class that I can directly pass to decoration: instead of calling a global function?
BoxDecoration backgroundImage() {
return BoxDecoration(
image: DecorationImage(
image: AssetImage("images/background.png"),
fit: BoxFit.cover,
),
);
}
class HomeScreen extends StatelessWidget{
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: backgroundImage(),
child: null /* add child content here */,
),
);
}
}
It seems to me that what you want is a variable that you can import and re-use. Creating a new class in which you are only going be returning an instance of BoxDecoration with a few custom options is probably overkill. Why not create a separate file, import material, declare a variable with your customisation and use it? Like this example:
Custom decoration file to re-use:
import 'package:flutter/material.dart';
BoxDecoration baseBackgroundDecoration =
BoxDecoration(
image: DecorationImage(
image: AssetImage("images/background.png"),
fit: BoxFit.cover,
),
);
Using it:
import 'custom_decorations.dart';
Using the variable:
Container(
decoration: baseBackgroundDecoration,
),
There's nothing wrong with your original idea though. You wouldn't be using it as a global variable, just a file that you import when you need it, which contains a class with your custom decorations, like this:
import 'package:flutter/material.dart';
class CustomDecorations {
BoxDecoration baseBackgroundDecoration(){
return BoxDecoration(
image: DecorationImage(
image: AssetImage("images/background.png"),
fit: BoxFit.cover,
),
);
}
}
Which you can then just use in one of these ways:
Container(
decoration: CustomDecorations().baseBackgroundDecoration(),
),
Or:
// Declare it
CustomDecorations customDecorations = CustomDecorations();
...
// Use it
Container(
decoration: customDecorations.baseBackgroundDecoration(),
),