Shadows in a rounded rectangle in Flutter - flutter

I tried adding a shadow to a rounded container but Flutter adds shadows as if the container is a perpendicular rectangle, which I don't want. I looked up for this problem on Google but couldn't find any appropriate solution, please help me out.
Code for Container
Container(
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.28,
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: Color(0xFFF9D276),
borderRadius: BorderRadius.circular(35),
boxShadow: [
BoxShadow(
offset: Offset(0, 4),
color: Color(0xFFF9D276).withOpacity(0.15),
spreadRadius: 4,
blurRadius: 50
)
]
),
)
UPDATE
Answer from #HardikKumar & How I actually want it

You can use from this code for rounded rectangle :)
Container(
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.28,
decoration: BoxDecoration(
color: Colors.green[700],
shape: BoxShape.rectangle,
borderRadius:BorderRadius.all(
Radius.circular(25)
)
),
margin: EdgeInsets.only(right: 8,top: 8),
child: IconButton(
icon: Icon(
Icons.send,
color: Colors.yellow[600],
),
onPressed:() {}
),
)

I'm sorry but I tested your code and the shadow shape appears with the same border as the decoration box
Image
I just edit the color of the shadow to black so i can see it better.
Maybe u want use a button like that , u need to use ButtonTheme to use height and width ,
Final edition:
Use Material , it has elevation property, shapeBorder and BorderRadiusGeometry, u can use an container as parent of Material for the width and height.
Link to Material Widget on Flutter

Problems in the code:
With the blurRadius of 50, you won't see any difference in shadow when you change the spreadRadius or the offset
With the opacity of 0.15, you will barely see any shadow (both in black or white backgound color)
Try this code:
Container(
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.28,
padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: Color(0xFFF9D276),
borderRadius: BorderRadius.circular(35),
boxShadow: [
BoxShadow(
//offset: Offset(0, 4),
color: Color(0xFFF9D276), //edited
spreadRadius: 4,
blurRadius: 10 //edited
)
),
),
How it looks like:
If you need anything else, just let me know.

As far I get some idea from your first image, you can check the code below.
Center(
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(15),
color: Color(0xff000000),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Color(0x73000000),
blurRadius: 5.0,
spreadRadius: 1,
offset: new Offset(-10.0, 0.0),
),
],
),
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.25,
child: Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
child: Container(
width: MediaQuery.of(context).size.width * 0.82,
height: MediaQuery.of(context).size.height * 0.28,
// padding: const EdgeInsets.symmetric(horizontal: 12),
decoration: BoxDecoration(
color: Color(0xFFF9D276),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Color(0xff000000),
blurRadius: 0.0,
spreadRadius: -2,
offset: new Offset(2.0, 0.0),
),
],
borderRadius: BorderRadius.circular(35),
),
),
),
),
)

You can try this
Stack(
children: [
Container(
margin: EdgeInsets.only(left: 2, top: 2),
child: Align(
alignment: Alignment.center,
child: OutlinedButton(
style: OutlinedButton.styleFrom(
primary: Colors.red,
backgroundColor: Colors.gray,
fixedSize: const Size(200, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
side: const BorderSide(width: 3.0, color: ColorsConst.gray9),
),
child: const Text("Text"),
onPressed: () => print('Text'),
),
),
),
Container(
margin: EdgeInsets.only(right: 2, bottom: 2),
child: Align(
alignment: Alignment.center,
child: OutlinedButton(
style: OutlinedButton.styleFrom(
primary: Colors.red,
backgroundColor: Colors.gray,
fixedSize: const Size(200, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
side: const BorderSide(width: 3.0, color: ColorsConst.gray9),
),
child: const Text("Text"),
onPressed: () => print('Text'),
),
),
)
]
)

Related

Shadow Effect only to the left of a card In flutter?

So here is the output image which I wanted to achieve.
and this is the result that I have achieved.
So you can see the shadow effect is in the left and slowly fading towards right.
here is my code for this
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: MediaQuery.of(context).size.height * 0.15,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20)
),
child: Card(
elevation: 5,
),
),
So elevation provides me with shadow but only towards bottom but I want it to be on the left to right fading... any way to achieve this?
Thanks.
Try the following code:
Container(
width: MediaQuery.of(context).size.width * 0.45,
height: MediaQuery.of(context).size.height * 0.15,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 5,
blurRadius: 7,
offset: const Offset(-7.5, 7.5),
),
],
),
child: Card(),
),
Container(
margin: EdgeInsets.all(3),
padding: EdgeInsets.zero,
width: MediaQuery.of(context).size.width * 0.5,
height: MediaQuery.of(context).size.height * 0.15,
decoration: BoxDecoration(
color: Colors.blueAccent,
boxShadow: [
BoxShadow(blurRadius: 8.0),
BoxShadow(color: Colors.white, offset: Offset(0, -16)),
BoxShadow(color: Colors.white, offset: Offset(0, 16)),
BoxShadow(color: Colors.white, offset: Offset(-16, -16)),
BoxShadow(color: Colors.white, offset: Offset(-16, 16)),
],
),
child: Card(color: Colors.white,elevation: 0),
),
You can customize it according to your own code
You can Simply Do This In Card Using RoundedRectangleBorder
Card(
elevation:12.0,
color: Colors.grey[900],
shape: RoundedRectangleBorder(
side: BorderSide(color: Colors.white70, width: 1),
borderRadius: BorderRadius.only(topLeft: Radius.circular(8.0),
bottomLeft: Radius.circular(8.0),
),
),
margin: EdgeInsets.all(20.0),
child: Container(
child: Column(
children: <Widget>[
ListTile(
title: Text(
'example',
style: TextStyle(fontSize: 18, color: Colors.white),
),
),
],
),
),
),
You can set the shadow of the container box with this one.
Container(
margin: EdgeInsets.all(3),
padding: EdgeInsets.zero,
width: MediaQuery.of(context).size.width * 0.45,
height: MediaQuery.of(context).size.height * 0.15,
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
spreadRadius: 5,
blurRadius: 6,
offset: const Offset(-6.0, 6.00),
),
],
borderRadius: BorderRadius.circular(20),
color: Colors.white
),
child: Card(color: Colors.white,elevation: 0),
),

Flutter Stack is not Getting Full Height

I want to overlay container to 1st one container in Stack. But when doing, it works but when i give to height first one it just take this height and second container just stucks and didn't take full height.
I want this but "THIS IS MY TEXT" container should take full height but it won't?
But when i use fit : StackFit.expand. it does work what i want but first one got behind it.
here's my code
double topHeightPadding = MediaQuery.of(context).padding.top;
double heightForStack = (MediaQuery.of(context).size.height - 90);
return Scaffold(
body: Stack(
fit : StackFit.expand,
children: <Widget>[
Container(
height: topHeightPadding + 120,
decoration: const BoxDecoration(
color: Color(0xFF141D38),
),
child: const Center(
child: Text(
"SOMETEXT.COM",
style: TextStyle(color: Colors.white),
textScaleFactor: 1.4,
)),
),
Positioned(
top: topHeightPadding + 90,
left: 0.1,
right: 0.1,
bottom: 0,
child: Container(
width: MediaQuery.of(context).size.width,
height: heightForStack,
decoration: const BoxDecoration(
// color: Colors.red,
color: Color(0xFFFFFFFF),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(35),
topRight: Radius.circular(35)),
boxShadow: [
BoxShadow(
color: Color(0xE6808080),
spreadRadius: 5,
blurRadius: 5,
offset: Offset(0, 2),
),
],
),
child: const Center(
child: Text('THIS IS MY TEXT')
),
))
],
),
);
Any help would be Appreciated.
Answering Own Question : https://stackoverflow.com/help/self-answer
I just got how to do it by Adding one more container with height double.infinity.
Code:
double topHeightPadding = MediaQuery.of(context).padding.top;
double heightForStack = (MediaQuery.of(context).size.height - 90);
return Scaffold(
body: Stack(
// fit : StackFit.expand,
children: <Widget>[
Container(height: double.infinity,),
Container(
height: topHeightPadding + 120,
decoration: const BoxDecoration(
color: Color(0xFF141D38),
),
child: const Center(
child: Text(
"SOMETEXT.COM",
style: TextStyle(color: Colors.white),
textScaleFactor: 1.4,
)),
),
Positioned(
top: topHeightPadding + 90,
left: 0.1,
right: 0.1,
bottom: 0,
child: Container(
width: MediaQuery.of(context).size.width,
height: heightForStack,
decoration: const BoxDecoration(
// color: Colors.red,
color: Color(0xFFFFFFFF),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(35),
topRight: Radius.circular(35)),
boxShadow: [
BoxShadow(
color: Color(0xE6808080),
spreadRadius: 5,
blurRadius: 5,
offset: Offset(0, 2),
),
],
),
child: const Center(
child: Text('THIS IS MY TEXT')
),
))
],
),
);

BoxShadow is drew above other widgets

I'm trying to make glow around items in list. For this I use box-shadow in item's decoration. But it is appeared above other items, not as background. How to draw boxshadow behind sibling items as well.
class ShadowItem extends StatelessWidget {
const ShadowItem({required this.isWithGlow});
final bool isWithGlow;
#override
Widget build(BuildContext context) {
return Container(
height: 100,
decoration: BoxDecoration(
boxShadow: (isWithGlow)
? [
BoxShadow(
color: Colors.blue,
blurRadius: 42.0,
spreadRadius: 0.0,
offset: Offset(
0.0,
12.0,
),
),
]
: [],
),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(19),
),
),
);
}
}
you need to set margins between boxes so the shadow will not overflow on another widget.
I am using flutter_neumorphic: ^3.0.3 Package to solve your problem.
here is the code
import 'package:flutter/material.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
class ShadowItem extends StatelessWidget {
const ShadowItem({required this.isWithGlow});
final bool isWithGlow;
#override
Widget build(BuildContext context) {
return Container(
height: 100,
margin: EdgeInsets.symmetric(vertical: 10, horizontal: 5),
child: Neumorphic(
style: isWithGlow
? NeumorphicStyle(
shadowDarkColor: Colors.blue,
shadowLightColor: Colors.blue,
depth: 10,
shape: NeumorphicShape.convex,
lightSource: LightSource.right)
: NeumorphicStyle(
depth: 2,
shape: NeumorphicShape.convex,
),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(19),
),
),
),
);
}
}
Your need is to show BoxShadow depends on the isWithGlow. so you can set grey shadow background instead of empty shadow [] and set margin for container, it will highlight your Container.
return Container(
height: 100,
margin: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
decoration: BoxDecoration(
boxShadow: (isWithGlow)
? [
BoxShadow(
color: Colors.blue,
blurRadius: 42.0,
spreadRadius: 0.0,
offset: Offset(
0.0,
12.0,
),
),
]
: [
BoxShadow(
color: Colors.grey[400]!,
blurRadius: 42.0,
spreadRadius: 0.0,
offset: Offset(
0.0,
12.0,
),
),
],
),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(19),
),
),
);

Ripple Effect (inkwell) in Containers, positioned with margins in a Stack in Flutter

I'm using the following code in Flutter, hoping to get a ripple effect (InkWell()) in the green and red areas. Yes they are supposed to be buttons, used in car by a copilot while driving on dirt roads.
I can get the ripple effect behind the colored areas by using the Material Class, but not in front of it and it will not respect the bounderies set by the margins of the containers.
Not using types right now, still in early dev stages, any tips on better or shorter code are very welcome
Does anyone have an idea?
context: context,
// barrierColor: Color(0x55888899),
builder: (BuildContext context) {
return Container(
height: 300,
padding: EdgeInsets.only(top: 20),
decoration: BoxDecoration(
color: Color(0xcc232930),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(40),
topRight: Radius.circular(40),
),
),
child: Stack(
children: [
//Background
Container(
decoration: BoxDecoration(
color: Colors.lightBlueAccent,
boxShadow: [
BoxShadow(
color: Colors.black,
offset: new Offset(0.0, 3.0),
blurRadius: 10,
)
],
borderRadius: BorderRadius.circular(40),
),
margin: EdgeInsets.only(left: 15.0, right: 15.0),
padding: EdgeInsets.symmetric(vertical: 18),
height: 245),
Container(
//horizontal line
height: 1,
width: MediaQuery.of(context).size.width / 2 - 15,
color: Colors.white,
margin: EdgeInsets.only(
top: 122, left: MediaQuery.of(context).size.width / 2),
),
Container(
//vertical line
height: 245,
width: 1,
color: Colors.white,
margin: EdgeInsets.only(
left: MediaQuery.of(context).size.width / 2),
),
// Big F
Container(
width: (MediaQuery.of(context).size.width / 2) - 15,
margin: EdgeInsets.only(left: 15),
alignment: Alignment(0.0, 0.0),
child: Text(
"F",
style: TextStyle(
color: Colors.white,
fontSize: 180,
shadows: [
Shadow(
blurRadius: 15,
color: Colors.black,
offset: Offset(0, 0))
],
),
),
),
// Accept button
Container(
width: (MediaQuery.of(context).size.width / 2) - 16,
height: 122,
margin: EdgeInsets.only(
left: MediaQuery.of(context).size.width / 2 + 1,
//top: 100,
),
alignment: Alignment(0.0, 0.0),
decoration: BoxDecoration(
color: Colors.green,
borderRadius:
BorderRadius.only(topRight: Radius.circular(40)),
),
child: InkWell(
onTap: () {
print("Accepted, adding to ControlCard");
},
splashColor: Colors.lightGreen,
enableFeedback: true,
child: Padding(
padding: const EdgeInsets.all(22.0),
child: Text("Accept",
style: TextStyle(
fontSize: 32,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 15,
color: Colors.black,
offset: Offset(0, 0))
],
)),
),
),
),
// Decline button
Container(
width: (MediaQuery.of(context).size.width / 2) - 16,
height: 122,
margin: EdgeInsets.only(
left: MediaQuery.of(context).size.width / 2 + 1,
top: 123,
),
alignment: Alignment(0.0, 0.0),
decoration: BoxDecoration(
color: Colors.red,
borderRadius:
BorderRadius.only(bottomRight: Radius.circular(40)),
),
child: InkWell(
onTap: () {
print("Declined");
},
enableFeedback: true,
child: Padding(
padding: const EdgeInsets.all(22.0),
child: Text("Decline",
style: TextStyle(
fontSize: 32,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 15,
color: Colors.black,
offset: Offset(0, 0))
],
)),
),
),
),
],
),
);
},
);```
You should use the Positioned widget to position the Containers within the Stack. ClipRRect is required to stop the splash from going out of boundaries.
Please see working code below or check it out on Dartpad https://dartpad.dev/d1b2350a64826357a5d417b0f1703334 :
import 'package:flutter/material.dart';
final Color darkBlue = Colors.white;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
height: 300,
padding: EdgeInsets.only(top: 20),
decoration: BoxDecoration(
color: Color(0xcc232930),
borderRadius: BorderRadius.only(
topLeft: Radius.circular(40),
topRight: Radius.circular(40),
),
),
child: Stack(
children: [
//Background
Container(
decoration: BoxDecoration(
color: Colors.lightBlueAccent,
boxShadow: [
BoxShadow(
color: Colors.black,
offset: Offset(0.0, 3.0),
blurRadius: 10,
)
],
borderRadius: BorderRadius.circular(40),
),
margin: EdgeInsets.only(left: 15.0, right: 15.0),
padding: EdgeInsets.symmetric(vertical: 18),
height: 245),
Container(
//horizontal line
height: 1,
width: MediaQuery.of(context).size.width / 2 - 15,
color: Colors.white,
margin: EdgeInsets.only(
top: 122, left: MediaQuery.of(context).size.width / 2),
),
Container(
//vertical line
height: 245,
width: 1,
color: Colors.white,
margin:
EdgeInsets.only(left: MediaQuery.of(context).size.width / 2),
),
// Big F
Container(
width: (MediaQuery.of(context).size.width / 2) - 15,
margin: EdgeInsets.only(left: 15),
alignment: Alignment(0.0, 0.0),
child: Text(
"F",
style: TextStyle(
color: Colors.white,
fontSize: 180,
shadows: [
Shadow(
blurRadius: 15, color: Colors.black, offset: Offset(0, 0))
],
),
),
),
// Accept button
Positioned(
left: MediaQuery.of(context).size.width / 2 + 1,
child: ClipRRect(
borderRadius: BorderRadius.only(topRight: Radius.circular(40)),
child: Material(
color: Colors.green,
child: InkWell(
onTap: () {
print("Accepted, adding to ControlCard");
},
splashColor: Colors.lightGreen,
enableFeedback: true,
child: Container(
width: (MediaQuery.of(context).size.width / 2) - 16,
height: 122,
child: Center(
child: Text(
"Accept",
style: TextStyle(
fontSize: 32,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 15,
color: Colors.black,
offset: Offset(0, 0))
],
),
),
),
),
),
),
),
),
// Decline button
Positioned(
left: MediaQuery.of(context).size.width / 2 + 1,
top: 123,
child: ClipRRect(
borderRadius: BorderRadius.only(bottomRight: Radius.circular(40)),
child: Material(
color: Colors.red,
child: InkWell(
onTap: () {
print("Declined");
},
child: Container(
width: (MediaQuery.of(context).size.width / 2) - 16,
height: 122,
child: Center(
child: Text("Decline",
style: TextStyle(
fontSize: 32,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 15,
color: Colors.black,
offset: Offset(0, 0))
],
)),
),
),
),
),
),
),
],
),
);
}
}

How to create container with box shadow at bottom and all sides

I tried to create a container with box shadow but not able to get same result as I want. This is i achieved but i want to show shadow with rounded edges and one of my horizontal list with bottom shadow and 2nd with all side shadow. I also need low thickness of shadow. My lists have background image and text. Please help me how to achieve this.
My Code
Container(
height: 180.0,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: response.data.length,
itemBuilder: (context, index) {
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {},
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: CachedNetworkImageProvider(
response.data[index].imageUrl,
),
fit: BoxFit.fill)),
margin: EdgeInsets.only(bottom: 6.0, right: 10.0),
width: MediaQuery.of(context).size.width - 100,
child: Container(
width: MediaQuery.of(context).size.width - 100,
margin: EdgeInsets.only(left: 8.0, right: 6.0),
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
BoxShadow(
color: Color(0xff000000).withOpacity(.9),
blurRadius: 10.0,
spreadRadius: 2.0,
offset: Offset(0.0, 180))
],
),
child: Padding(
padding: const EdgeInsets.fromLTRB(
10.0, 35.0, 5.0, 0.0),
child: Text(
response.data[index].name.toUpperCase(),
style: GoogleFonts.roboto(
textStyle: TextStyle(
fontSize: 15,
fontWeight: FontWeight.bold,
color: Color(0xffFFFFFF))),
),
),
),
),
);
}),
)
this might help
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 2,
blurRadius: 3,
offset: Offset(0, 4)),
],
Ref Stackoverflow ref
use offset property of BoxShadow to control how the shadow should be visible and radius property to control thickness of the shadow.