如何隐藏ElevatedButton的label,实现仅图标居中展示的按钮效果
你当前的实现效果如下:
要实现图标完全居中的按钮,最简便的方案是直接使用普通ElevatedButton构造,将图标作为child传入即可,无需使用.icon命名构造:
ElevatedButton( onPressed: () {}, child: Icon(Icons.arrow_forward), style: ElevatedButton.styleFrom( primary: Colors.red, fixedSize: Size(100, 50), ), )
如果你有特殊需求必须使用ElevatedButton.icon构造,可通过调整内边距、对齐属性,同时将label设置为无尺寸的空白组件实现居中:
ElevatedButton.icon( onPressed: () {}, icon: Icon(Icons.arrow_forward), label: SizedBox.shrink(), style: ElevatedButton.styleFrom( primary: Colors.red, fixedSize: Size(100, 50), padding: EdgeInsets.zero, alignment: Alignment.center, ), )
你原代码的问题在于ElevatedButton.icon默认会在图标和label之间预留固定间距,哪怕你传入的是空Text组件,也会占用布局位置,导致图标整体偏左无法居中。
内容的提问来源于stack exchange,提问作者Jae
相关产品推荐
相关产品推荐

