如何在MaterialButton或RaisedButton上应用主题?
问题内容:
有人可以帮助指出我们如何为按钮定义基本主题并将其用于每个按钮吗?我到处都只发现textTheme
但没有buttonTheme
示例?
甚至buttonTheme
我们如何定义文本颜色?因为在按钮本身上,我们可以直接像color: Colors.blue
问题答案:
做到这一点的方法之一是定义buttonTheme
在theme
中MaterialApp
:
例如:
void main() {
runApp(MaterialApp(
home: Home(),
theme: ThemeData(
accentColor: Colors.redAccent,
buttonTheme: ButtonThemeData(
buttonColor: Colors.blueAccent,
shape: RoundedRectangleBorder(),
textTheme: ButtonTextTheme.accent,
....
)),
));
}
class Home extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Button Theme"),
backgroundColor: Colors.green),
body: Center(
child: RaisedButton( //Button Color is as define in theme
onPressed: () {},
child: Text("Send"), //Text Color as define in theme
)),
);
}
}
与此相关的所有在此下定义的按钮MaterialApp
都将采用此主题样式。
文字颜色将是我accentColor
定义的中的ThemeData
定义,textTheme: ButtonTextTheme.accent
因此它将选择accentColor
按中定义的按钮选择主题样式 theme