skip to Main Content

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: () {},
          ),
        ),
      ),
    );
  }
}

that’s how my code and app look like at the moment

2

Answers


  1. Because container is the parent widget and icon is child so You need to use alignment: Alignment.center, for container as well

    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,
                alignment: Alignment.center,
              decoration: BoxDecoration(
                color: tdRed,
                borderRadius: BorderRadius.circular(5),
              ),
              child: IconButton(
                alignment: Alignment.center,
                color: Colors.white,
                iconSize: 18,
                icon: Icon(Icons.delete),
                onPressed: () {},
              ),
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
  2. The 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

    The padding around the button’s icon. The entire padded icon will react to input gestures.
    This property can be null. If null, it defaults to 8.0 padding on all sides.

    To resolve the issue, you can set padding to 0.0 in the IconButton class.

    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,
                padding: const EdgeInsets.all(0.0),
                color: Colors.white,
                iconSize: 18,
                icon: Icon(Icons.delete),
                onPressed: () {},
              ),
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search