gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 07:03:02 24 4
gpt4 key购买 nike

我希望我所有的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com