How to apply theme on MaterialButton or RaisedButton?

前端 未结 4 1267
广开言路
广开言路 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:29

    Looks like you also need to provide textColor to your button. How about creating your Custom Button?

    class MyButton extends StatelessWidget {
      final String text;
      final Color textColor;
      final Color buttonColor;
      final Function() onPressed;
      MyButton({
        @required this.text,
        this.buttonColor = const Color(0xFF000000) /** Default buttonColor */,
        @required this.onPressed,
        this.textColor = const Color(0xFFDDDDDD) /** Default textColor */,
      });
      @override
      Widget build(BuildContext context) {
        return MaterialButton(
          color: buttonColor,
          onPressed: onPressed,
          child: Text(text,
              style: TextStyle(
                color: textColor,
                fontSize: 20.0,
              )),
        );
      }
    }
    

    You can define your button color like the one in the answer given above/below too.

    [UPDATE] As per request from the comments, this is how you pass a function for onPressed

    class Home extends StatelessWidget {
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
              title: Text("Button Theme"),
              backgroundColor: Colors.green),
          body: Center(
            child: MyButton( //My custom button 
            text: "Hit me",
            onPressed: () { print("Ouch! Easy pal!! :p ") },
            textColor = const Color(SOME CUSTOM COLOUR)
          )),
        );
      }
    }
    

提交回复
热议问题