How can I make a shadow with a material design card? - flutter

This is the result that I would like to have :

Make a custom card
///custom cards
Widget card(String image) {
return Container(
child: Image.asset(
image,
fit: BoxFit.cover,
),
decoration: BoxDecoration(
border: Border.all(color: Colors.blue, width: 2.0),
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(5.0),
),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Colors.blue,
blurRadius: 3.0,
offset: new Offset(0.0, 3.0),
),
],
),
margin: EdgeInsets.all(5.0),
height: 150.0,
width: 100.0,
);
}
Box Shadow is what you need. I hope this will help.

Two ways I know to make a card with shadow. one with the built-in Card Widget and other Using the Container Widget
1.Using Card Widget
SizedBox.expand(
child: Card(
margin: EdgeInsets.all(10),
elevation: 3.0,// this field changes the shadow of the card 1.0 is default
shape: RoundedRectangleBorder(
side: BorderSide(width: 0.2),
borderRadius: BorderRadius.circular(20)),
),
)
Using Container Widget
Container(
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
border: Border.all(width: 0.2),
boxShadow: [
BoxShadow(
blurRadius: 2.0,
spreadRadius: 0.4,
offset: Offset(0.1, 0.5)),
],
color: Colors.white),
)
modify BlurRadius and offset to alter the shadow of the container

You can use container with boxshadow. you can use below class to archive card effect in container.
class CustomCard extends StatelessWidget {
const CustomCard(
{Key? key, required this.child, this.height, this.width, this.redius})
: super(key: key);
final Widget child;
final double? height;
final double? width;
final double? redius;
#override
Widget build(BuildContext context) {
return Container(
child: child,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(
Radius.circular(redius ?? 5),
),
boxShadow: <BoxShadow>[
BoxShadow(
color: Color.fromRGBO(230, 230, 230, 0.9),
blurRadius: redius ?? 5,
offset: Offset(0.0, 4.0),
),
],
),
margin: EdgeInsets.all(5.0),
height: height ?? 150.0,
width: width ?? 100.0,
);
}
}

Related

Flutter: How can I add a box shadow to this widget?

I am trying to add a box shadow over a widget that displays an image. Whenever I include the box-shadow within the parent container, the shadow is only shown on the container which is stacked behind the child image. How can I have the shadow be displayed for the child image?
import 'package:flutter/material.dart';
import 'package:flutter_course_app_4/pages/nft_post.dart';
class NFTIconTemplate extends StatelessWidget {
final String NFTImg;
NFTIconTemplate({required this.NFTImg});
#override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
child: InkWell(
child: Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black54,
offset: Offset(4.0, 4.0),
blurRadius: 15.0,
spreadRadius: 1.0),
],
color: Colors.grey[800],
),
child: Column(
children: [
Image.network(
NFTImg,
width: 120,
height: 160,
fit: BoxFit.cover,
),
],
),
),
onTap: () {
print('Clicked NFT Icon');
Navigator.push(
context, MaterialPageRoute(builder: (context) => nftPost()));
},
),
),
);
}
}
Try this code to get extra shade to your code,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
bottomLeft: Radius.circular(12),
bottomRight: Radius.circular(12)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.6),
spreadRadius: 8,
blurRadius: 6,
offset: Offset(0, 4),
),
],
),

Stack with custom shadow and positioned widget outside the bounderies

I'm trying to achieve something like this in flutter:
This is my attempt to do that
class ContainerWidget extends StatelessWidget {
final Widget child;
final double width, height;
late List<Color>? gradiantColors;
ContainerWidget({
Key? key,
required this.child,
required this.height,
required this.width,
this.gradiantColors,
}) : super(key: key);
#override
Widget build(BuildContext context) {
var border = BorderRadius.only(
topRight: Radius.circular(70),
topLeft: Radius.circular(8),
bottomLeft: Radius.circular(8),
bottomRight: Radius.circular(8),
);
return Container(
decoration: BoxDecoration(
color: gradiantColors == null ? Colors.white : null,
borderRadius: border,
boxShadow: [
BoxShadow(
color: Colors.grey.shade300,
offset: Offset(0, 1),
blurRadius: 30,
spreadRadius: 5,
),
],
gradient: gradiantColors != null
? LinearGradient(
colors: gradiantColors!,
begin: Alignment.topLeft,
end: Alignment.bottomRight,
)
: null,
),
width: width,
height: height,
child: Stack(
children: [
Positioned(
top: -40,
left: -20,
child: Container(
decoration: BoxDecoration(
color: Colors.red.shade100.withOpacity(0.2),
shape: BoxShape.circle,
),
width: 100,
height: 100,
),
),
Positioned(
child: Icon(
Icons.abc_rounded,
size: 40,
),
),
child
],
),
);
}
}
this is the output
However, I've two problems:
The circle on top leak out over the rounded corner, I tried to solve that with the ClipRRect, but it clipped also the shadow of the container.
Icon (or the image) cannot be placed above the top portion of the container.
Any ideas ?
Like this (put this inside your widget class):
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Stack(
clipBehavior: Clip.none,
children: [
Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(blurRadius: 20,
color: Colors.orange.withOpacity(0.5),
offset: const Offset(0.0, 8.0)
)
],
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(100),
bottomLeft: Radius.circular(20),
bottomRight: Radius.circular(20)
)
),
child: ClipRRect(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(100),
bottomLeft: Radius.circular(20),
bottomRight: Radius.circular(20)
),
child: Stack(
children: [
Container(
width: 200,
height: 300,
color: Colors.orange
),
Positioned(
top: -50,
left: -40,
child: ClipOval(
child: Container(
width: 150,
height: 150,
color: Colors.white.withOpacity(0.25)
)
)
)
]
)
)
),
Positioned(
top: -40,
left: 20,
child: Container(
width: 80,
height: 80,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(25),
boxShadow: [
BoxShadow(blurRadius: 10, color: Colors.black.withOpacity(0.2), offset: const Offset(0.0, 5.0))
]
)
)
)
]
)
)
);
}
which then ends up looking like this:
The top white Container is just a placeholder for what your image could look like. I'd suggest then using a PNG already with the shadow integrated if you'll be using pictures like that.
Let me know if that's what you're trying to accomplish and adjust to your needs.

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

PageView Container won't constrain to a max width or height flutter

I can't seem to figure out why my container keeps being full screen. I'm trying to make the containers have a max width so when its viewed on a desktop the containers look like cards kinda like they would if it was viewed on a phone screen.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
int currentPage = 0;
class MyApp extends StatelessWidget {
final PageController ctrl = PageController(viewportFraction: 0.8);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => PageView(
scrollDirection: Axis.vertical,
controller: ctrl,
children: [
Container(
width: MediaQuery.of(context).size.width * .80,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.green,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 500,
offset: Offset(10, 10))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.blue,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 500,
offset: Offset(10, 10))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.orange,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 500,
offset: Offset(10, 10))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.red,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 500,
offset: Offset(10, 10))
]),
),
],
),
),
),
);
}
}
Here is an image of what the container is doing.
Problem
And here is what I want it to look like when viewed on a desktop
What I'm Trying To Do
I Also tried adding constants like this and that didn't work either.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
int currentPage = 0;
class MyApp extends StatelessWidget {
final PageController ctrl = PageController(viewportFraction: 0.8);
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Builder(
builder: (context) => PageView(
scrollDirection: Axis.vertical,
controller: ctrl,
children: [
Container(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height /4,
maxWidth: MediaQuery.of(context).size.width * 0.8,),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.green,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 500,
offset: Offset(10, 10))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.blue,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 500,
offset: Offset(10, 10))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.orange,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 500,
offset: Offset(10, 10))
]),
),
Container(
margin: EdgeInsets.only(bottom: 50, right: 30, left: 30),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(20)),
color: Colors.red,
boxShadow: [
BoxShadow(
color: Colors.grey[500],
blurRadius: 500,
offset: Offset(10, 10))
]),
),
],
),
),
),
);
}
}
Could someone help me do this?
Thank you!
Put PageView Inside a container and give constraints to that container.

Adding a border to 3/4 sides in flutter

I'm trying to add a border to 3/4 sides on a custom button (the bottom has no border). However, the code produces no border anywhere. If I give all the sides a border, it works, but does not work if I do 3/4 sides. Any help is greatly appreciated. This is the code:
class CustomButton extends StatelessWidget{
CustomButton(this.img, this.title, this.connectivity, this.link);
final img;
final title;
final connectivity;
final link;
#override
Widget build(BuildContext context){
return GestureDetector(
onTap: (){
Navigator.of(context).push(MaterialPageRoute(builder: (context) => UrlViewers(link)));
print("Link: $link");
},
child: Column(
children: [
Container(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 2),
height: 200,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
border: Border(
left: BorderSide(color: Colors.black, width: 1.0),
top: BorderSide(color: Colors.black, width: 1.0),
right: BorderSide(color: Colors.black, width: 1.0),
),
image: DecorationImage(image: NetworkImage(img), fit: BoxFit.cover)
),
),
Container(
padding: EdgeInsets.all(2),
decoration: BoxDecoration(
color: Colors.white24,
borderRadius: BorderRadius.circular(10),
),
child: Text(title, style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold,)),
)
],
),
);
}
}
I have had the same issue earlier, apparently there is no simple way to add borders to a container while controlling its radius..
if you just remove the borderRadius parameter from the BoxDecoration it should work just fine.