我希望我的所有RaisedButton小部件都有不同的textColor,如何在MaterialApp ThemeData中仅更改此小部件
发布于 2019-06-10 06:03:48
如果您查看一下MaterialButton,您将看到它使用了ButtonThemeData中的getTextColor()方法,并且该方法使用枚举ButtonTextTheme来定义文本颜色。枚举条目是normal、accent和primary。您可以仅根据这些颜色为您的RaisedButton设置全局文本颜色。
要实现这一点:
ThemeData theme = Theme.of(context);
return MaterialApp(
...
theme: theme.copyWith(
buttonTheme: theme.buttonTheme.copyWith(
textTheme: ButtonTextTheme.accent,
),
),
);如果你想设置一个与normal、accent或primary不匹配的自定义颜色,你得到的最好的选择就是创建一个带有这种颜色的自定义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,
);
}发布于 2020-08-29 05:25:49
对于那些使用轻量主题的应用程序,但是想要在按钮上显示白色文本的人来说,你可以在你的应用程序的入口点(即声明你的MaterialApp的地方)调整buttonTheme。类似于:
return MaterialApp(
theme: ThemeData.light().copyWith(
buttonTheme: ThemeData.dark().buttonTheme
),
);https://stackoverflow.com/questions/56517959
复制相似问题