I want to add a drop shadow to a png image (image with alpha) in flutter.
I've already tried using BoxShadow, but I don't want to have a box shadow. I need a shadow that adapts to the png image.
Desired result:
The CSS box-shadow property permits adding shadows on images. However, we can't utilize it with png images as it will put a square image shadow. If you need to make a drop shadow for the png image, the most choice decision is the filter property, that can help you with taking care of styling problems.
The normal shadow effect will always put a square image shadow for the image which can be squared or cannot be squared but the shadow will be always square. filter:drop-shadow (); and text-shadow (); property is more eye pleasant compare to box-shadow: () property.
The value of the property is called drop-shadow (), and inside it are the following parameters: x-offset: The horizontal offset of the shadow. y-offset: The vertical offset of the shadow. blur-radius: The blur radius of the shadow. color: The color of the shadow.
First, you need two images, change the color of one them(your images should be transparent to make this happen) like:
child: Image.asset("assets/cat.png", color:Colors.black,),
Then put black one to the background using Stack and change its position to match shadow position.
Then add BackdropFilter to black one.
I think will work.
Code:
Stack(children: [
Opacity(child: Image.asset(imagePath, color: Colors.black), opacity: 0.2),
ClipRect(child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 5.0, sigmaY: 5.0),
child: Image.asset(imagePath)))
])
Try this package:
https://pub.dev/packages/simple_shadow
It's very configurable, and this is an example:
Container(
child: SimpleShadow(
child: Image(
width: 70.0,
height: 70.0,
image: NetworkImage(logoUrl),
semanticLabel: title,
),
opacity: 0.25, // Default: 0.5
color: Colors.black, // Default: Black
offset: Offset(3, 3), // Default: Offset(2, 2)
sigma: 7, // Default: 2
),
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With