Flutter 更改主题中所有凸起按钮的文本颜色

标签 flutter

我希望我所有的 RaisedButton 小部件都有不同的 textColor,如何在 MaterialApp ThemeData 中仅更改它?

最佳答案

如果你看一下MaterialButton , 你会看到它使用方法 getTextColor()来自 ButtonThemeData,此方法考虑枚举 ButtonTextTheme 来定义文本颜色。枚举条目是normalaccentprimary。您可以仅根据这些颜色为 RaisedButton 设置全局文本颜色。

实现它:

ThemeData theme = Theme.of(context);

return MaterialApp(
  ...
  theme: theme.copyWith(
    buttonTheme: theme.buttonTheme.copyWith(
      textTheme: ButtonTextTheme.accent,
    ),
  ),
);

如果你想设置一个与normalaccentprimary 不匹配的自定义颜色,最好的选择是创建具有此颜色的自定义 Widget,因此您无需在每个 RaisedButton 中单独设置它。

检查一下:

class ButtonWithCustomTextColor extends RaisedButton {
  ButtonWithCustomTextColor({
    Key key,
    @required VoidCallback onPressed,
    ValueChanged<bool> onHighlightChanged,
    ButtonTextTheme textTheme,
    // Place your custom color here
    Color textColor = Colors.blue,
    Color disabledTextColor,
    Color color,
    Color disabledColor,
    Color highlightColor,
    Color splashColor,
    Brightness colorBrightness,
    double elevation,
    double highlightElevation,
    double disabledElevation,
    EdgeInsetsGeometry padding,
    ShapeBorder shape,
    Clip clipBehavior = Clip.none,
    MaterialTapTargetSize materialTapTargetSize,
    Duration animationDuration,
    Widget child,
  }) : super(
    key: key,
    onPressed: onPressed,
    onHighlightChanged: onHighlightChanged,
    textTheme: textTheme,
    textColor: textColor,
    disabledTextColor: disabledTextColor,
    color: color,
    disabledColor: disabledColor,
    highlightColor: highlightColor,
    splashColor: splashColor,
    colorBrightness: colorBrightness,
    elevation: elevation,
    highlightElevation: highlightElevation,
    disabledElevation: disabledElevation,
    padding: padding,
    shape: shape,
    clipBehavior: clipBehavior,
    materialTapTargetSize: materialTapTargetSize,
    animationDuration: animationDuration,
    child: child,
  );
}

关于Flutter 更改主题中所有凸起按钮的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56517959/

相关文章:

dart - 在为其他小部件保持一定高度的同时保持整个高度

flutter - dio_cookie_manager插件的CookieManager功能与flutter_inappwebview Cookiemanager冲突

将 Container 作为子项的 Flutter InkWell 未显示效果

flutter - 向下滚动Listview项时,强制屏幕小部件的顶部保持显示, flutter

flutter - 如何在flutter_swiper中动态禁用滑动按钮?

java - Flutter stomp 客户端无法与 spring stomp 客户端连接

flutter - 有没有办法获取 Dart 中触摸事件的压力?

android - flutter 应用程序 : A problem occurred configuring root project 'android'

flutter 陷入解决android的依赖关系

ios - 无法在iOS模拟器中选择图片