How can I change the background color of a textbutton in flutter?

backgroundColor property is MaterialStateProperty<Color?> type. You can check in Flutter documentation.

So you have to use MaterialStateProperty class to apply color. A quick example :

TextButton(
    child: Text('test'),
    style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Colors.red)),
    onPressed: () {},
),

2022 Update

Since version 3.0.1, we can use MaterialStatePropertyAll as const.

So it’s a better approach, to use it :

TextButton(
    child: Text('test'),
    style: const ButtonStyle(
        backgroundColor: MaterialStatePropertyAll(Colors.red),
    ),
    onPressed: () {},
),

Leave a Comment

tech