如何让LegacyFlatButton颜色与Scaffold背景色一致且去除外边框
解决方案
要实现LegacyFlatButton无外边框、和Scaffold背景视觉完全融合的效果,只需要额外配置几个样式属性即可:
- 配置
shape属性的side为BorderSide.none,完全移除按钮外边框 - 设置
backgroundColor为透明,或者直接读取当前主题的Scaffold背景色,保证和页面背景完全匹配 - 可选配置点击态的高亮、水波纹颜色为透明,完全消除按钮的视觉存在感
修改后的完整代码如下:
LegacyFlatButton.icon( onPressed: () {}, icon: Icon(Icons.edit_location), label: Text('Edit Location'), color: Colors.blue, // 移除外边框 shape: const RoundedRectangleBorder(side: BorderSide.none), // 背景设为透明和Scaffold融合 backgroundColor: Colors.transparent, // 可选:移除点击时的水波纹、高亮效果 splashColor: Colors.transparent, highlightColor: Colors.transparent, ),
如果你使用的是Flutter 2.0及以上版本,
LegacyFlatButton已经被官方标记为废弃,更推荐使用TextButton组件实现相同效果,代码示例如下:
TextButton.icon( onPressed: () {}, icon: Icon(Icons.edit_location), label: Text('Edit Location'), style: TextButton.styleFrom( foregroundColor: Colors.blue, // 移除边框 side: BorderSide.none, // 背景透明 backgroundColor: Colors.transparent, // 完全取消点击水波纹效果 splashFactory: NoSplash.splashFactory, ), )
内容的提问来源于stack exchange,提问作者Kisah Tegar Putra Abdi
相关产品推荐
相关产品推荐

