How to add shadow just to an image in Card in Circle Avatar in flutter? - flutter

I am new to flutter and I want to add shadow to an image in Card (shadow just to an image not the text), how can I do that? Below is the snippet of my code, please let me know how can I do that, thank you.
child: Container(
height: 70,
child: Card(
child: ListTile(
onTap: () {},
title: Text(eventList[index].event_name),
leading: CircleAvatar(
backgroundImage: NetworkImage(eventList[index].imageURL),
),
),
),
),

you can wrap the CircleAvatar with another Container and specify the boxShadow property in its decoration, or for more accuracy, just use a DecoratedBox like this:
Container(
height: 70,
child: Card(
child: ListTile(
onTap: () {},
title: Text("eventList[index].event_name"),
leading: DecoratedBox(
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.black.withOpacity(.2),
blurRadius: 5,
offset: const Offset(0, 10),
)
]),
child: CircleAvatar(
backgroundImage: NetworkImage("eventList[index].imageURL"),
),
),
),
),
);

You can simply use a Widget called PhysicalModel where it can easily show shadows. You can play with the properties such as color and elevation for your desired shadow effect.
https://api.flutter.dev/flutter/widgets/PhysicalModel-class.html
leading: PhysicalModel(
color: Colors.grey.withOpacity(0.8),
shape: BoxShape.circle,
elevation: 10.0,
child: const CircleAvatar(
backgroundImage: NetworkImage(
'https://i1.sndcdn.com/artworks-000486414654-mt7qdt-t500x500.jpg'),
),
),

Related

How to add an icon over a CircleAvatar flutter

I wanna know how to add an icon like online icon over a CircleAvatar.
I've wrote this code but dont know what to do...
...
CircleAvatar(
backgroundColor: Color(0xff00A3FF),
radius: 35.0,
backgroundImage:
AssetImage("assets/photos/photo-1.jpg"),
),
SizedBox(height: 7.0),
Text(
"Michael",
style: TextStyle(color: Colors.white,: FontWeight.w700),
)
...
What you need here is a Stack widget. Stack widgets allow you to place widgets on top of widgets (meaning order is crucial).
Here I modified your CircleAvatar to add a little green "online" indicator. Make sure to adjust the size and such to fit your needs.
Stack(
children: [
CircleAvatar(
backgroundColor: Color(0xff00A3FF),
backgroundImage: AssetImage("assets/photos/photo-1.jpg"),
radius: 35.0,
),
Positioned(
right: 0,
bottom: 0,
child: Container(
padding: EdgeInsets.all(7.5),
decoration: BoxDecoration(
border: Border.all(
width: 2,
color: Colors.white
),
borderRadius: BorderRadius.circular(90.0),
color: Colors.green
)
)
)
]
)
You don't need a circleAvatar in a circleAvatar.
just use backgroundImage property of the CircleAvatar. like this;
NetworkImage(userAvatarUrl)
more on that:https://api.flutter.dev/flutter/material/CircleAvatar-class.html
You can achieve what you want with nested circle avatars like this:
CircleAvatar(
radius: 58,
backgroundImage: AssetImage("assets/photos/photo-1.jpg"),
child: Stack(
children: [
Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
radius: 18,
backgroundColor: Colors.white,
child: CircleAvatar(
radius: 16,
backgroundColor: Colors.green
)
)
)
]
)
);
The result is like this:
If you want to add an icon, you can do like this
CircleAvatar(
radius: 58,
backgroundImage: AssetImage("assets/photos/photo-1.jpg"),
child: Stack(
children: [
Align(
alignment: Alignment.bottomRight,
child: CircleAvatar(
radius: 18,
backgroundColor: Colors.white,
child: Icon(Icons.camera) // change this children
)
)
]
)
);
and the result will be:

How do I remove the empty space at the beginning of a card in Flutter?

So I have this card:
and I want to make it so the blue box looks like this:
how would I do this?
my code is this:
child: Card(
elevation: 5.0,
child: ListTile(
onTap: () {},
title: Text(tasks[index].title),
subtitle: Text(tasks[index].description, overflow: TextOverflow.ellipsis, maxLines: 1,),
leading: ClipRRect(
borderRadius: BorderRadius.circular(2.0),
child: SizedBox(
height: 60.0,
width: 10.0,
child: DecoratedBox(
decoration: BoxDecoration(
color: Colors.blue,
),
),
),
)
),
),
Check the DevTools here https://flutter.dev/docs/development/tools/devtools/overview
They are super useful especially for when there is something wrong with the layout of your widgets, you can see exactly what is wrong woth that widget.
Im guessing its the SizedBox width:10.

How to add a Highlight color to a Container having a Image? [duplicate]

This question already has answers here:
InkWell ripple over top of image in GridTile in Flutter
(6 answers)
Closed 2 years ago.
I have a container with its image property. The container is wrapped in a gesture detector. On long Press, a dialogue is open which shows multiple options to deal with an image.
I want to add a highlight colour to the image, So that when user long press and highlight colour is seen.
I tried using inkwell, but nothings happen. Is there any way to achieve this effect?
GestureDetector(
onLongPress: () {
buildPeepShow(context, widget.post);
},
child: Material(
color: Colors.transparent,
child: InkWell(
splashColor: Colors.black.withAlpha(100),
child: Stack(
children: [
Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.3),
spreadRadius: 0.3,
blurRadius: 8.0)
],
color: Colors.yellow,
borderRadius: BorderRadius.circular(16.0),
image: DecorationImage(
image: CachedNetworkImageProvider(
widget.post.downloadURL[0]),
fit: BoxFit.cover)),
),
Visibility(
visible: widget.post.downloadURL.length > 1 ? true : false,
child: Padding(
padding: const EdgeInsets.only(top: 5.0, right: 8.0),
child: Image.asset(
'images/more.png',
scale: 10,
),
),
),
],
),
),
),
);
Wrap your InkWell in a Material widget, then set splash color property on the InkWell to see the splash effect:
Material(
color: App.transparent,
child: InkWell(
splashColor: App.black.withAlpha(100),
child: Container(
child: <YourImageWidgetHere>
),
),
);
Replace your code snippet with this one
return Stack(
children: [
Container(
decoration: BoxDecoration(
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.3),
spreadRadius: 0.3,
blurRadius: 8.0)
],
color: Colors.yellow,
borderRadius: BorderRadius.circular(16.0),
image: DecorationImage(
image: CachedNetworkImageProvider(
widget.post.downloadURL[0]),
fit: BoxFit.cover)),
),
Visibility(
visible: widget.post.downloadURL.length > 1 ? true : false,
child: Material(
child: InkWell(
splashColor: Colors.red, // or the color you want
onLongPress: (){
//show your on long press event
},
child: Padding(
padding: const EdgeInsets.only(top: 5.0, right: 8.0),
child: Image.asset(
'images/more.png',
scale: 10,
),
),
),
),
),
],
);
I am closing my question because I found out its a duplicate of InkWell ripple over top of image in GridTile in Flutter
I thanks to everyone who spent their time on this question

How to display a circle avatar image in flutter?

When using the folloing code I would expect a circle avatar image but getting an oval. I tried different parameters like width and height on the Container but that didn't help.
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: new Icon(Icons.star_border, color: Colors.black),
onPressed: () => {},
),
actions: <Widget>[
Container(
//height: 25.0,
// width: 25.0,
child: CircleAvatar(
backgroundImage: NetworkImage('https://lh3.googleusercontent.com/a-/AAuE7mChgTiAe-N8ibcM3fB_qvGdl2vQ9jvjYv0iOOjB=s96-c'),
)
/*
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
fit: BoxFit.fill,
image: NetworkImage(
'https://lh3.googleusercontent.com/a-/AAuE7mChgTiAe-N8ibcM3fB_qvGdl2vQ9jvjYv0iOOjB=s96-c')),
),*/
),
],
You are receiving an oval shape because you are using the CircleAvatar widget in Appbar widget which has limited height.
Try adding a parameter radius inside CircleAvatar Widget
it will return the circle shape you want for the image.
try changing radius size value, according to your need.
CircleAvatar(
backgroundImage: NetworkImage('https://lh3.googleusercontent.com/a-/AAuE7mChgTiAe-N8ibcM3fB_qvGdl2vQ9jvjYv0iOOjB=s96-c'),
radius: 15.0
)
I've had this issue in the past and have found that wrapping the AvatarCircle in a container with 58 width fixes the Circle radius ratio issue, making it a proper circle shape. One pixel more or less may fit your liking. Give this a try:
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: new Icon(Icons.star_border, color: Colors.black),
onPressed: () => {},
),
actions: <Widget>[
Container(
width: 58.0,
child: CircleAvatar(
backgroundImage: NetworkImage('https://lh3.googleusercontent.com/a-/AAuE7mChgTiAe-N8ibcM3fB_qvGdl2vQ9jvjYv0iOOjB=s96-c'),
)
),
],
i use this code and works with every image size...
CircleAvatar(
child: ClipOval(
child: Image.network(items.logo, fit: BoxFit.fill),
),
backgroundColor: Colors.transparent,
radius: 30,
)
Nothing works for me other than wrapping CircleAvatar with padding of 8.0.
Padding(
padding: const EdgeInsets.all(8.0),
child: CircleAvatar(
...
),
)

Flutter: how can I make a rounded PopupMenuButton's InkWell?

I have a PopupMenuButton inside a FloatingActionButton. But it's InkWell is not rounded, it is standard square shape. How can I achieve that?
Use customBorder property of InkWell:
InkWell(
customBorder: CircleBorder(),
onTap: () {}
child: ...
)
You can use borderRadius property of InkWell to get a rounded Ink Splash.
Material(
color: Colors.lightBlue,
borderRadius: BorderRadius.circular(25.0),
child: InkWell(
splashColor: Colors.blue,
borderRadius: BorderRadius.circular(25.0),
child: Text('Button'),
),
),
To change the InkWell's shape to rounded from standard square shape, Material's borderRadius property is used. Example code is given below -
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.green,
child: Material(
color: Colors.yellow,
borderRadius: BorderRadius.all(Radius.circular(5.0)),
child: InkWell(
child: PopupMenuButton(
//shape is used to change the shape of popup card
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15.0)),
child: Icon(Icons.mode_edit, size: 22.0, color: Colors.red,),
itemBuilder: (context) => [
PopupMenuItem(
child: Text("Child 1"),
),
PopupMenuItem(
child: Text("Child 2"),
),
],
),
),
),
),
I faced a similar issue where the child of the PopupMenuButton would have a square InkWell around it.
In order to make it behave like an IconButton, which naturally uses the rounded InkWell, I simply had to use the icon paramater instead of the child.
icon: Icon(Icons.more_vert),
This is indicated in the documentation for that paramater:
/// If provided, the [icon] is used for this button
/// and the button will behave like an [IconButton].
final Widget icon;
Wrap the Inkwell with Material. Add Border Radius
Material(
borderRadius: BorderRadius.all( // add
Radius.circular(20)
),
child: InkWell(
child: Ink(
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
child: Text(
"Kayıt Ol",
style: TextStyle(
color: cKutuRengi,
),
),
),
),
)
Here's how to make the tap effect look right
Material(
borderRadius: BorderRadius.all(
Radius.circular(20)
),
child: InkWell(
customBorder:RoundedRectangleBorder( // add
borderRadius: BorderRadius.all(
Radius.circular(20)
)
),
onTap: () {
debugPrint("on tap");
},
child: Ink(
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 10),
child: Text(
"Kayıt Ol",
style: TextStyle(
color: cKutuRengi,
),
),
),
),
)
Most of the answers here are not using PopupMenuButton like the question specified. If you're simply using an icon child then you can use the icon property rather than child as already explained above, but if you want rounded corners on some other child, you wrap it with a Material, and wrap that with a ClipRRect See this Stackoverflow