问题描述
有人可以帮助我们指出如何为按钮定义基本主题并在每个按钮上使用它吗?我到处都只发现textTheme
而不是buttonTheme
示例?
Can someone help to point how do we define base theme for button and use it on every button? Everywhere I look only found textTheme
but not buttonTheme
example?
即使在buttonTheme
上,我们如何定义文本颜色?因为在按钮本身上,我们可以像color: Colors.blue
Even on buttonTheme
how do we define text colors? Because on the button itself we can do it directly like color: Colors.blue
推荐答案
一种方法是在MaterialApp
中的theme
中定义buttonTheme
:
One way to do it is to Define buttonTheme
in theme
in 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
下定义的所有按钮都将带有此主题样式.
with this all the Buttons defined under this MaterialApp
will Carry this Theme Style.
文本颜色将是ThemeData
中的accentColor
定义,因此它将选择accentColor
Text Color will be the accentColor
define in the ThemeData
as i have defined textTheme: ButtonTextTheme.accent
so it will Pick accentColor
按theme
这篇关于如何在MaterialButton或RaisedButton上应用主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!