首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤动更改主题中所有凸起按钮的文本颜色

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

Stack Overflow用户
提问于 2019-06-10 03:57:28
回答 2查看 2.1K关注 0票数 2

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

EN

回答 2

Stack Overflow用户

发布于 2019-06-10 06:03:48

如果您查看一下MaterialButton,您将看到它使用了ButtonThemeData中的getTextColor()方法,并且该方法使用枚举ButtonTextTheme来定义文本颜色。枚举条目是normalaccentprimary。您可以仅根据这些颜色为您的RaisedButton设置全局文本颜色。

要实现这一点:

代码语言:javascript
运行
复制
ThemeData theme = Theme.of(context);

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

如果你想设置一个与normalaccentprimary不匹配的自定义颜色,你得到的最好的选择就是创建一个带有这种颜色的自定义Widget,这样你就不需要在每个RaisedButton中单独设置它。

看看这个:

代码语言:javascript
运行
复制
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,
  );
}
票数 1
EN

Stack Overflow用户

发布于 2020-08-29 05:25:49

对于那些使用轻量主题的应用程序,但是想要在按钮上显示白色文本的人来说,你可以在你的应用程序的入口点(即声明你的MaterialApp的地方)调整buttonTheme。类似于:

代码语言:javascript
运行
复制
return MaterialApp(
  theme: ThemeData.light().copyWith(
    buttonTheme: ThemeData.dark().buttonTheme
  ),
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56517959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档