【问题标题】:change to MaterialButton's colour using ThemeData使用 ThemeData 更改 MaterialButton 的颜色
【发布时间】:2022-01-08 20:53:48
【问题描述】:

我的应用中有一个自定义按钮,可以使用 GetMaterialApp 内部的 ThemeData 对其进行操作,但我无法更改其颜色。我必须在这里更改它的颜色,因为我的应用有几个主题可以切换。

我的自定义按钮是:

MaterialButton(
      onPressed: () {
        ...
      },
      child: Icon(
        icon,
        color: Get.theme.iconTheme.color,
      ),
    );

而 ThemeData 是:

ThemeData customThemeDataLight = ThemeData(
  ...
  buttonTheme: const ButtonThemeData(
    height: 60,
    buttonColor: cpDarkBlue,
    shape: OutlineInputBorder(
      borderSide: BorderSide(width: 0.5),
      borderRadius: BorderRadius.only(
        topRight: Radius.circular(15.0),
        bottomRight: Radius.circular(15.0),
      ),
    ),
  ),
  ...
);

我的 main.dart 文件是:

return GetMaterialApp(
      ...
      theme: customThemeDataLight,
      darkTheme: customThemeDataDark,
      themeMode: ThemeMode.system,
      ...
    );

【问题讨论】:

  • 为了让您的生活更轻松,您可以将提供程序包实现为状态管理解决方案,以实现像 this 这样的暗光主题。
  • 通常小部件可以像这样访问 ThemeData:Theme.of(context).primaryColor;
  • 谢谢@JahnE。你给了我一个想法。顺便说一句,GetX 处理主题和提供者。

标签: flutter flutter-layout flutter-getx


【解决方案1】:

MaterialButton 不直接访问 themeData。所以,我让它手动访问了 themeData 并且它起作用了。

例如:

MaterialButton(
      color: Get.theme.primaryColor,
      onPressed: () {
        ...
      },
      child: Icon(
        icon,
        color: Get.theme.iconTheme.color,
      ),
    );

这解决了我的问题。

【讨论】:

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