Adding frosted glass effect over image - flutter

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,
),
)

Related

Flutter - Issue with Container size when using BoxDecoration

I'm trying to create color overlay for Image to make it a bit gray. I'm using the following code:
Container(
padding: EdgeInsets.only(bottom: 20.0),
decoration: new BoxDecoration(
color: const Color(0xff7c94b6),
image: new DecorationImage(
fit: BoxFit.fitWidth,
colorFilter: new ColorFilter.mode(Colors.black.withOpacity(0.2), BlendMode.dstATop),
image: new AssetImage("assets/sunset.jpg"),
),
),
)
However without a Container child, color overlay takes all the screen.
[Result is the following][1]
I expect the Image to be on Top with overlay on Image only
[like this but with overlay][2]
Any thoughts, what am I'm missing?
[1]: https://i.stack.imgur.com/ifPEu.png
[2]: https://i.stack.imgur.com/4P0Cb.png
The overlay is over the whole screen because the Container has no constraints and therefore the parent's constraints (which in your case probably the screen size) are being used. When Container has a child, it uses its constraints.
The Column is not really necessary but if you want to have the image stuck to the top I assume you want to add other stuff below.
Column(
children:[
Container(
child: Image.asset(
"assets/images/sunset.JPG",
colorBlendMode: BlendMode.dstATop,
color: Colors.black.withOpacity(0.2),
),
decoration: BoxDecoration(
color: const Color(0xff7c94b6),
),
),
],
)

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 to display a part of a big image?

I got an image that is landscape and want to use it on my phone (portrait).
I don't want to resize my image. I just want to use what I need based on my screen size and start from the middle of it. And if I want to start from the left, what would be the solution?
I am not sure I am clear, so I created a basic image to explain what I meant.
Thanks
UPDATE
Scaffold(
body: Stack(
children: <Widget>[
Container(
decoration: BoxDecoration(
image: DecorationImage(
image:
const AssetImage("assets/images/login_960x540.png"),
fit: BoxFit.cover,
// colorFilter: ColorFilter.mode(
// Colors.black.withOpacity(0.3), BlendMode.dstATop),
),
),
),
#David You can use FittedBox and Alignment for positioning background image, for example:
Container(
height: 100,
width: 100,
child: FittedBox(
alignment: Alignment.centerLeft, //Positioned in start
fit: BoxFit.fitHeight,
child: Image.asset('assets/image.png'), //If image 100x300, you will see just 100x100
),
);

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,
),
),
),
)

How to overlay a widget in Flutter with another widget, opacity set to multiply?

This is what I want to achieve:
I know that there's a colorBlendMode on the image widget, but it doesn't allow me to color a part of the image, just the whole. Is it possible to overlay color for a percentage of the image?
For color overlay, you could simply use Container with color that has opacity. Then if you put your image and Container in Stack you could get same result as you have.
EDIT:
If you want to use multiply blending only option that comes to my mind is take two picture one with colorFiler, second(cutted in half) without and put them together. But it's definitely not clean or somehow nice solution.
Code for similar result as your image:
Column(children: <Widget>[
Stack(children: <Widget>[
Image(
image: NetworkImage('https://picsum.photos/600?image=9'),
colorBlendMode: BlendMode.multiply,
color: Colors.green),
AspectRatio(
aspectRatio: 2 / 1,
child: new Container(
decoration: new BoxDecoration(
image: new DecorationImage(
fit: BoxFit.fitWidth,
alignment: FractionalOffset.topCenter,
image: NetworkImage(
'https://picsum.photos/600?image=9',
))))),
]),
Stack(children: <Widget>[
Image(
image: NetworkImage(
'https://www.solidbackgrounds.com/images/1920x1080/1920x1080-white-solid-color-background.jpg'),
height: 100,
fit: BoxFit.fitWidth,
width: MediaQuery.of(context).size.width,
colorBlendMode: BlendMode.multiply,
color: Colors.green),
Center(
child: Text('5',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: 40)))
])
])
I would definitely consider looking into the Stack Widget, which allows you to overlay children on top of one another. You could have your base image on the bottom, and that filter just above it.