首页 > 解决方案 > 如何更改 ElevatedButton 和 OutlinedButton 的禁用颜色

问题描述

ElevatedButton和小部件中没有这样的属性OutlinedButton可以像常规一样更改禁用的颜色RaisedButton

ElevatedButton(
  onPressed: null,
  disabledColor: Colors.brown, // Error
}

标签: flutter

解决方案


onSurface如果您只想更改禁用的颜色,请使用属性(此属性在 中也可用OutlinedButton)。

ElevatedButton(
  onPressed: null,
  style: ElevatedButton.styleFrom(
    onSurface: Colors.brown,
  ),
  child: Text('ElevatedButton'),
)

如需更多自定义,请使用ButtonStyle

ElevatedButton(
  onPressed: null,
  style: ButtonStyle(
    backgroundColor: MaterialStateProperty.resolveWith<Color>((states) {
      if (states.contains(MaterialState.disabled)) {
        return Colors.brown; // Disabled color
      }
      return Colors.blue; // Regular color
    }),
  ),
  child: Text('ElevatedButton'),
)

要将其应用于ElevatedButton您的应用程序中的所有内容:

MaterialApp(
  theme: ThemeData(
    elevatedButtonTheme: ElevatedButtonThemeData(
      style: ElevatedButton.styleFrom(
        onSurface: Colors.brown
      ),
    ),
  ),
)

推荐阅读