本文介绍了如何在MaterialButton或RaisedButton上应用主题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我们指出如何为按钮定义基本主题并在每个按钮上使用它吗?我到处都只发现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 MaterialAppwill 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上应用主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 02:13