i am trying to center a delete icon to a trailing container that’s within a list tile
i tried using the alignment option for the icon button but it didn’t work, i also tried to use the center widget for the icon Button too, also didn’t work, what did i do wrong?
Widget build(BuildContext context) {
return Container(
child: ListTile(
onTap: () {},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
contentPadding: EdgeInsets.symmetric(horizontal: 20, vertical: 20),
tileColor: Colors.white,
leading: Icon(Icons.check_box, color: tdBlue),
title: Text(
'Check Mail',
style: TextStyle(
fontSize: 16,
color: tdblack,
decoration: TextDecoration.lineThrough),
),
trailing: Container(
padding: EdgeInsets.all(0),
margin: EdgeInsets.symmetric(vertical: 12),
width: 25,
height: 25,
decoration: BoxDecoration(
color: tdRed,
borderRadius: BorderRadius.circular(5),
),
child: IconButton(
alignment: Alignment.center,
color: Colors.white,
iconSize: 18,
icon: Icon(Icons.delete),
onPressed: () {},
),
),
),
);
}
}
2
Answers
Because container is the parent widget and icon is child so You need to use
alignment: Alignment.center,
for container as wellThe IconButton class has a padding parameter which defaults to 8.0 if not explicitly defined. This padding affects the Icon widget of the IconButton class, hence the reason why it looks shifted. IconButton’s padding property
To resolve the issue, you can set padding to 0.0 in the IconButton class.