How to put random photo in Flutter AssetImage? - flutter

I have a code like this:
List backgrounds = ["bg.jpeg", "bg2.jpg", "bg3.jpg", "bg4.jpg", "bg5.jpg", "bg6.jpg", "bg7.jpg", "bg8.jpg", "bg9.jpg", "bg10.jpg", "bg11.jpg", "bg12.jpg"];
var randomBG;
randomBG = backgrounds[Random().nextInt(backgrounds.length)];
...
Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
randomBG.toString()),
fit: BoxFit.fill,
),
),
But getting an error:
How can I solve this problem?

BoxDecoration's image will get on runtime, therefor you cannot use const. Just remove const from BoxDecoration.
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(randomBG.toString()),
fit: BoxFit.fill,
),
),
),

Related

How to apply a mask over a png image excluding the transparent Part in flutter

I want a greenish tint over the non transparent part of the image, a tried searching for an image manipulation library so that i can manipulate the image directly but couldn't find
Use a container for the image
Container(
decoration: new BoxDecoration(
color: const Color(0xff7c94b6),
image: new DecorationImage(
fit: BoxFit.cover,
colorFilter: new ColorFilter.mode(Colors.green.withOpacity(0.2)),
image: new NetworkImage(
'Imgurl',
),
),
),
),

How to set asset images in container background image with flutter

I know how to set image file in container.
However when I try to set background of Container in ListView, the images can not shown up.
Of course, I wrote these image files in pubspec.yaml.
body: ListView(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/IMG_01.JPG'),
fit: BoxFit.cover,
),
),
),
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/IMG_02.JPG'),
fit: BoxFit.cover,
),
),
),
Give me advice.
Thank you.

How can I add an opaque overlay over the image on box decoration in Flutter

I am struggling to come up with a solution to this seemingly simple task. I need an opaque overlay over an image in Flutter. I have tried to add the overlay different ways, but the image will always overlay on top of whatever I do. Mind you I need the opaque overlay over the image, but not over the title or text that sits on top of both the image and the overlay. Alternatively, I could have a black background and make the image opaque resulting possibly in the same effect that I want to achieve? Before I start to hack too much I would like to see how pros are doing it the way it should be done. Thank you all for your kind advices.
Container(
width: 320.0,
height: 180.0,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(
news[index]['jetpack_featured_media_url'],
),
fit: BoxFit.cover,
),
color: Color(0xFF1F1F98),
gradient: LinearGradient(
begin: Alignment.topCenter,
colors: [Colors.black, Colors.black],
stops: [0.0, 0.5]
),
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(8.0),
topRight: const Radius.circular(8.0)
),
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Text(
cardTitle(news[index]['title']['rendered']),
style: kCardOverlayTitleTextStyle
)
) /* add child content here */,
),
You can use the ColorFilter option available in the DecoratedImage Widget
Container(
width: 320.0,
height: 180.0,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage(news[index['jetpack_featured_media_url']),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.black.withOpacity(0.2),
BlendMode.darken
),
),
),
BlendMode.srcOver composite the source image over the destination image. So, if you want to add the overlay, this will be the option of choice. You can find more about this blend mode here
Here is the snippet that will work:
Container(
decoration: BoxDecoration(
image: DecorationImage(
image: const AssetImage('../your-image.png'),
fit: BoxFit.cover,
colorFilter: ColorFilter.mode(
Colors.red.withOpacity(0.8),
BlendMode.srcOver,
),
),
),
)

Adding frosted glass effect over image

I am trying to add a frosted glass effect over an image and based on my research, the below seems to be one way to go about it. However, while there is no linting, it is giving me runtime error: "cannot provide both a color and a decoration". Is there a better way to blur the image in the background with an orange frosted effect?
return Consumer<UserModel>(
builder: (context, model, _) => Scaffold(
body: Container(
color: Colors.orange.withOpacity(0.75),
decoration: BoxDecoration(
image: DecorationImage(
alignment: Alignment.bottomCenter,
image: AssetImage("assets/images/pngguru.com-id-bnwsh.png"),
fit: BoxFit.cover,
),
),
Thats mean, if you have a BoxDecoration property in your container, you need to move color to inside BoxDecoration
Like that
Container(
decoration: BoxDecoration(
color: Colors.orange.withOpacity(0.75), // <-------------
image: DecorationImage(
alignment: Alignment.bottomCenter,
image: AssetImage("assets/images/pngguru.com-id-bnwsh.png"),
fit: BoxFit.cover,
),
)

I get an error when I try to use the image network in Flutter instead of the image entity image What is the solution?

I get an error when I want to use image network instead of asset image. Can you find a solution?I get an error when I try to use the image network in Flutter instead of the image entity image What is the solution?
new GestureDetector(
onTap: () {
_fraURL();
},
child: Container(
decoration: new BoxDecoration(
color: Colors.transparent,
image: new DecorationImage(
-----> image: AssetImage(
"res/c3.jpg"),<----
fit: BoxFit.scaleDown,
),
),
),
),
new GestureDetector(
onTap: () {
_fraURL();
},
child: Container(
decoration: new BoxDecoration(
color: Colors.transparent,
image: new DecorationImage(
image: new Image.network(
'https://firebasestorage.googleapis.com/v0/b/book-7a285.appspot.com/o/ic_launcher.png?alt=media&token=605f6d54-7098-487c-ae4c-f70ae4476670'),
fit: BoxFit.scaleDown,
),
),
),
),
image: new Image.network(
'https://firebasestorage.googleapis.com/v0/b/book-7a285.appspot.com/o/ic_launcher.png?alt=media&token=605f6d54-7098-487c-ae4c-f70ae4476670')
<-----i can't run it