Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter 2.0 - How to change TextButton splash color when pressed?

FlatButton is deprecated and shouldn't be used. Used TextButton instead.

On my previous FlatButton widget, I was able to changed the splash color when on pressed. But now I'm using TextButton widget, how can I change its color the efficient way on the MaterialApp ThemeData or directly on the TextButton widget.

Currenly this is my TextButton

TextButton(
  style: TextButton.styleFrom(
    primary: Colors.red,
    textStyle: TextStyle(
      color: Colors.black45,
      fontFamily: "Courier Prime",
    ),
    backgroundColor: Colors.transparent,
  ),
  onPressed: () {},
  child: Text(
    "Student",
    style: TextStyle(fontWeight: FontWeight.bold),
  ),
),
overlayColor is used to indicate that the button is focused, hovered, or pressed.

But I cant find this overlayColor

like image 576
Raine Dale Holgado Avatar asked Apr 06 '21 08:04

Raine Dale Holgado


2 Answers

First keep in mind that the primary property on a TextButton sets the colour of its text and icon. It does not change the ripple color. Secondly in Textbutton there is no direct property to change splash color. So if you want to change splash color to transparent you can do it like this.

TextButton(
  style: ButtonStyle(
    overlayColor: MaterialStateProperty.all(Colors.________),
  ),
)
like image 134
Arslan Kaleem Avatar answered Nov 15 '22 09:11

Arslan Kaleem


You can quickly set the splash color of a TextButton using the helper method TextButton.styleFrom(). Note that this will actually set both the foreground color and splash color based on the foreground color, which in most of the cases is what you want:

TextButton(
  style: TextButton.styleFrom(primary: Colors.red),
  child: const Text('Text Button'),
  onPressed: () {},
)

Live Demo

like image 24
NearHuscarl Avatar answered Nov 15 '22 07:11

NearHuscarl