Flutter FlatButton已弃用,如何使用TextButton或ElevatedButton替换现有实现?
原有FlatButton默认是无背景填充、仅保留点击水波纹交互的按钮样式,用TextButton调整样式即可1:1还原原有效果,替换后的完整代码如下:
Center( child: TextButton( // 取消TextButton默认内边距,避免和原有Container的margin叠加导致布局偏移 style: TextButton.styleFrom( padding: EdgeInsets.zero, tapTargetSize: MaterialTapTargetSize.shrinkWrap, ), onPressed: () { }, child: Container( margin:EdgeInsets.only(top: 25), child: image != null ? Image.file(image,width:140,height:192,fit:BoxFit.fill) : Container( width: 240, height: 200, child: Icon(Icons.camera_front, size: 100, color:Colors.grey,) ), ), ), )
如果需要对齐原FlatButton按下时的遮罩效果,可在TextButton.styleFrom中额外添加foregroundColor配置:
foregroundColor: MaterialStateColor.resolveWith((states) => states.contains(MaterialState.pressed) ? Colors.grey.withOpacity(0.5) : Colors.transparent )
- 只有当你需要按钮自带背景填充、阴影效果时,才考虑替换为
ElevatedButton,你当前的业务场景用TextButton是最优解。
内容的提问来源于stack exchange,提问作者aaryan
相关产品推荐
相关产品推荐

