How to apply theme on MaterialButton or RaisedButton?

前端 未结 4 1283
广开言路
广开言路 2021-02-19 15:08

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

4条回答
  •  遇见更好的自我
    2021-02-19 15:43

    One way to do it is to Define buttonTheme in theme in MaterialApp:

    E.g:

    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
          )),
        );
      }
    }
    

    with this all the Buttons defined under this MaterialAppwill Carry this Theme Style.

    Text Color will be the accentColor define in the ThemeData as i have defined textTheme: ButtonTextTheme.accent so it will Pick accentColor

    Button picking Theme Style As Defined in theme

提交回复
热议问题