小编Dav*_*tin的帖子

Flutter ThemeData:按钮文本颜色优先考虑 colorScheme,而不是 ElevatedButtonTheme textStyle

我目前正在尝试为我的材质应用程序定义 ThemeData,并且我需要 ElevatedButtonThemeData 中的 ButtonStyle 来控制应用程序中所有按钮的外观。到目前为止,一切正常,但由于某种原因,我的按钮 TextStyle 的“颜色”字段被主题 ColorScheme 中的“onPrimary”字段覆盖。

textStyle 中的其他一切都工作正常,例如,如果我更改 TextStyle 中的 fontSize,则字体大小会在整个应用程序中更新,但更改颜色不会执行任何操作。此外,backgroundColor 对于 ButtonStyle 也适用。

我知道我可以将所有按钮包装在主题小部件中,但如果可能的话我想避免这种情况。

这是最终使用的颜色

      theme: ThemeData(
        brightness: Brightness.light,
        colorScheme: const ColorScheme(
          brightness: Brightness.light,
          primary: Colors.white,

          ///////////////////////////////////////
          onPrimary: Colors.red,
          //this is the color that is used
       ),

Run Code Online (Sandbox Code Playgroud)

这是我想使用的颜色

elevatedButtonTheme: ElevatedButtonThemeData(
          //this themedata controls themedata for elevated buttons
          style: ButtonStyle(
            //for some reason the MarterialStateProperty must be called to explicitly define types for buttons
            //ie: "MaterialStateProperty.all<Color>(const Color(0xFF50D2C2))" just allows "const Color(0xFF50D2C2)" to be used
            backgroundColor: MaterialStateProperty.all<Color>(const …
Run Code Online (Sandbox Code Playgroud)

color-scheme themes material-design flutter flutter-layout

2
推荐指数
1
解决办法
2661
查看次数