【问题标题】:change text color of Disabled state of ElevatedButton from ElevatedButton.styleFrom()从 ElevatedButton.styleFrom() 更改 ElevatedButton 的禁用状态的文本颜色
【发布时间】:2021-08-30 23:10:29
【问题描述】:

当按钮状态处于禁用状态时,我想更改按钮文本颜色。通过使用 onSurface: MyColors.disable,我可以更改按钮背景禁用状态。但按钮文字没有改变。我该如何解决这个问题。这是我的代码,我试过了

class MyTheme {
  MyTheme._();

  static final ThemeData lightTheme = ThemeData(
    scaffoldBackgroundColor: ShikhoColors.background,
    primaryColor: ShikhoColors.background,
    backgroundColor: ShikhoColors.background,
    brightness: Brightness.light,
    elevatedButtonTheme: ElevatedButtonThemeData(style: lightElevatedButtonStyle),
    textButtonTheme: TextButtonThemeData(style: _lightTextButtonStyle),
    outlinedButtonTheme: OutlinedButtonThemeData(style: _lightOutlinedButtonStyle),
    cardTheme: _lightCardStyle,
  );

  static final ButtonStyle lightElevatedButtonStyle = ElevatedButton.styleFrom(
    primary: ShikhoColors.blue_main,
    // onSurface: ShikhoColors.disable,
    padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 10.0),
    elevation: 0.0,
    shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
    textStyle: TextStyle(color: ShikhoColors.background, fontWeight: FontWeight.bold, fontSize: 16.0, fontFamily: 'HindSiliguri'),
  );

  ...
}

【问题讨论】:

    标签: flutter material-design


    【解决方案1】:

    这不起作用,因为您将颜色设置为 TextStyle,但 TextStyle 定义的颜色未在按钮样式中使用。

    要改变文字颜色,我们需要设置ButtonStyleforegroundColor属性。

    请看这里:elevated_button src

    在您的情况下,您需要设置 ElevatedButton.styleFrom 方法的 onPrimary color 属性。正如提到的here 它用于创建一个MaterialStateProperty ButtonStyle.foregroundColor

    【讨论】:

    • 你能告诉我在哪个地方,我应该使用foregroundColor颜色吗?
    • 使用onPrimary 属性来引用前景色
    【解决方案2】:

    你可以用这个:

    foreground: Paint()..color= Colors.white
    

    以将其更改为白色为例。

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2016-11-20
      • 1970-01-01
      • 2019-12-31
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      相关资源
      最近更新 更多