0

I was trying to center the icon in a circular background but it failed even if I use a center widget as child unless increase container size.


    Container(
                  height: 22,
                  width: 22,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: Color(0xffF7825C),
                  ),
                  child: Center(
                    child: Icon(
                      Icons.add,
                      color: Colors.white,
                    ),
                  ),
                ) 

A.K.J.94
  • 492
  • 6
  • 14

5 Answers5

4

Try this:

             Container(
              alignment: Alignment.center,
              height: 22,
              width: 22,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Color(0xffF7825C),
              ),
              child: Icon(
                  Icons.add,
                  color: Colors.white,
                  size: 22 
                ),
            ) 
BosS
  • 447
  • 2
  • 9
1

You need to set the size of your icon with size attribute, so your Icon widget should look like this

Icon(
    Icons.add,
    color: Colors.white,
    size: 22
)
Yunus Karaaslan
  • 184
  • 1
  • 3
  • 12
  • 1
    at last I solved it by changing it to a CircleAvatar widget so it align center with out any hassle, – A.K.J.94 Apr 19 '21 at 06:32
1

You can also use RawMaterialButton, You can set it like this

RawMaterialButton(
              onPressed: () {},
              fillColor: Color(0xffF7825C),
              child: Icon(
                Icons.add,
                size: 22.0,
                color: Colors.white,
              ),
              shape: CircleBorder(),
            )
Mittal Varsani
  • 5,601
  • 2
  • 15
  • 27
0

Issue can be solve by giving size to Icon

Container(
    width: 40,
    height: 20,                  
    child: Icon(
        Icons.arrow_right_alt_outlined,
        color: Colors.white,
        size: 20,),
    ),

the above thing works because now we have the container which can contain whole icons size ( 40x20(container) can contain 20(icon)) but if this icons size gets bigger than container dimension there we meshed up the centering of icon inside container

hope this explanation works!

-2

use the following :

Container(
              height: 22,
              width: 22,
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Color(0xffF7825C),
              ),
              alignment: Alignment.center,
                child: Icon(
                  Icons.add,
                  color: Colors.white,
                ),
              
            ) 
Jay Dangar
  • 3,271
  • 1
  • 16
  • 35