I have a circular image inside circleAvatar on clicking which I want to open an alert dialog box. My question is how to add clicking event inside CircleAvatar?
This is my code :
Column(children: <Widget>[
SizedBox(height: 32.0),
CircleAvatar(
radius: 55.0,
backgroundImage: ExactAssetImage('assets/cat.jpg'),
),
Container(
height: 20.0,
),
Text('01:29:30'),
Text(
'Avik Kumar Mandal',
style: TextStyle(fontSize: 20),
),
Expected: onclick event on the image which is inside Circular avatar P.s it is a profile picture on clicking which a dialog box should appear.
This property applies a background image to the CircleAvatar widget. child: The child property takes the widget to be placed below the CircleAvatar widget inside the widget tree or the widget to be displayed inside the circle. foregroundColor: This property holds the Color class (final) as the parameter value.
You can wrap your circular avatar with a gesture detector
Column(children: <Widget>[
SizedBox(height: 32.0),
GestureDetector(
onTap: () {
//do what you want here
},
child: CircleAvatar(
radius: 55.0,
backgroundImage: ExactAssetImage('assets/cat.jpg'),
),
),
Container(
height: 20.0,
),
Text('01:29:30'),
Text(
'Avik Kumar Mandal',
style: TextStyle(fontSize: 20),
),
Or
InkWell(
onTap: () => print("image clicked"),
child: AnyWidget()
),
Now generalize my answer.
If you want to set OnClickListener
to some widget that onTap
is not implemented by default, then you should wrap the widget with GestureDetector
or InkWeel
You can use GestureDetector
but you will lose ripple effect on image click, so a better solution would be to use InkWell
. Wrap your CircleAvatar
in InkWell
InkWell(
onTap: () => print("image clicked"),
child: CircleAvatar(
backgroundImage: ExactAssetImage('assets/cat.jpg'),
radius: 55,
),
),
Another way is to wrap it in an IconButton, which has the benefit of a circular ripple effect.
IconButton(
icon: CircleAvatar(),
onPressed: () {},
)
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