颤动图标不在容器中居中

问题描述

我试图使图标在圆形背景中居中,但即使我使用中心小部件作为子项,它也失败了,除非增加容器大小。


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

解决方法

试试这个:

             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 
                ),) 
,

您需要使用 size 属性设置图标的大小,因此您的图标小部件应如下所示

Icon(
    Icons.add,size: 22
)
,

你也可以用RawMaterialButton,你可以这样设置

RawMaterialButton(
              onPressed: () {},fillColor: Color(0xffF7825C),child: Icon(
                Icons.add,size: 22.0,shape: CircleBorder(),)
,

使用以下内容:

Container(
              height: 22,alignment: Alignment.center,)