Flutter 3.3.0中ElevatedButton的onSurface已弃用,替代属性该选哪一个或两者?
在Flutter 3.3.0版本中,ElevatedButton的onSurface属性被弃用后,只需要使用disabledForegroundColor来替代它,disabledBackgroundColor是独立属性,无需用来替换onSurface。
核心说明
- 原
onSurface专门负责设置按钮禁用状态下的前景元素(文字、图标)颜色; disabledForegroundColor是官方指定的替代属性,完全承接原onSurface的功能;disabledBackgroundColor用于控制按钮禁用时的背景填充色,这是原onSurface从未覆盖的功能,仅在需要调整禁用背景时才需配置。
代码对比
弃用的旧写法
ElevatedButton( onPressed: null, style: ElevatedButton.styleFrom( onSurface: Colors.grey, // 该属性已弃用 ), child: Text('禁用按钮'), )
替代的新写法
ElevatedButton( onPressed: null, style: ElevatedButton.styleFrom( disabledForegroundColor: Colors.grey, // 替代原onSurface // 若需调整禁用时背景色,可添加以下配置(非必须) // disabledBackgroundColor: Colors.grey[200], ), child: Text('禁用按钮'), )
内容的提问来源于stack exchange,提问作者Gurpreet Ganger
相关产品推荐
相关产品推荐

