Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

I'm trying to migrate my FlatButton to TextButton. Since FlatButtons are deprecated since I upgraded my flutter version. I'm currently struggling with adapting the background color.

Old button:

FlatButton(
        height: height,
        onPressed: onPressed,
        shape: baseButtonBorder,
        color: Colors.red,
        child: Text(label, style: TextStyle(color: fontColor, fontWeight: boldLabel ? FontWeight.bold : FontWeight.normal)),
      )`

New Button:

TextButton(
        onPressed: onPressed,
        style: ButtonStyle(backgroundColor: Colors.red), // <-- Does not work
        child: Text(label, style: TextStyle(color: fontColor, fontWeight: boldLabel ? FontWeight.bold : FontWeight.normal)),
      ),

The flat button does not have a color attribute so I tried to use the style attribute and add a ButtonStyle. How ever dart says:

The argument type 'MaterialColor' can't be assigned to the parameter type 'MaterialStateProperty<Color>'.

How can I style my TextButton with the color red like I used to do with my FlatButton? Do I need to create a MaterialStateProperty<Color> with red?

like image 965
LOLWTFasdasd asdad Avatar asked Mar 09 '21 12:03

LOLWTFasdasd asdad


People also ask

How do I change the background color of TextButton in Flutter?

Go to your main.Inside the MaterialApp, find the ThemeData widget. Add the textButtonTheme property. Add the style property and change the color as you would change it for normal TextButton.

How do you style a TextButton in Flutter?

Creating a ButtonStyle for a TextButton can be done using TextButton. styleFrom . For defining a style for a specific button, you can pass ButtonStyle as style parameter in the constructor of TextButton . You can pass ButtonStyle as both ThemeData 's textButtonTheme and TextButton 's style parameters.

How do you change the background color of a material button in Flutter?

to Change Background color of Elevated Button in Flutter Elevated Button has a style Property And style property need ButtonStyle(). ButtonStyle has backgroundColor property which requires MaterialStateProperty. You can simply assign background color by MaterialStateProperty.


3 Answers

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: () {},
),
like image 69
Jouby Avatar answered Oct 18 '22 23:10

Jouby


For people looking for a clearer and easier way to do this, you can use TextButton.styleFrom(). Example:

TextButton(
  child: Text('Example'),
  onPressed: () {},
  style: TextButton.styleFrom(backgroundColor: Colors.red),
)

You can customize almost anything you want in styleFrom. This works for other buttons too, like ElevatedButton and OutlinedButton.

like image 24
Luke Olender Avatar answered Oct 18 '22 23:10

Luke Olender


Try this way,

TextButton(
  onPressed: () {},
  child: Container(
    padding: EdgeInsets.fromLTRB(30, 10, 30, 10),
    color: Colors.red,
    child: Text(""),
  ),
)
like image 24
xahid_rocks Avatar answered Oct 18 '22 21:10

xahid_rocks