flutter elevated button background color code example

Example 1: elevated button flutter color

ElevatedButton(
  style: ButtonStyle(
    backgroundColor: MaterialStateProperty.resolveWith<Color>(
      (Set<MaterialState> states) {
        if (states.contains(MaterialState.pressed))
          return Colors.green;
        return null; // Use the component's default.
      },
    ),
  ),
)

Example 2: raised button background color doesn't changeflutter

RaisedButton(
              onPressed: null,
              child: Text('Get in'), // change it to sign-in
              color: Colors.blue,
              disabledColor: Colors.blue,//add this to your code
            )

Example 3: elevated Button Theme background color in flutter

style: ElevatedButton.styleFrom(
              primary: Colors.purple,
            ),

Example 4: add background color to button flutte

bool isButtonPressed = false;

RaisedButton(
            color: isButtonPressed ? Colors.green : Colors.red,
            onPressed: () {
              setState(() {
                isButtonPressed =!isButtonPressed;
              });
            },
          ),

Example 5: raisedbutton background color

List<bool> _list = [true, false, true, false];


Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(title: Text("Title")),
    body: ListView(children: _buildButtons()),
  );
}

List<Widget> _buildButtons() {
  List<Widget> listButtons = List.generate(_list.length, (i) {
    return RaisedButton(
      color: _list[i] ? Colors.green : Colors.red,
      onPressed: () {
        setState(() {
          _list[i] = !_list[i];
        });
      },
      child: Text("Button #${i}"),
    );
  });
  return listButtons;
}

Tags:

Dart Example