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

5

10
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

Chaotic answered 6/4, 2021 at 8:14 Comment(2)
you can use setState to change color when pressedCray
@AkshayNayka , its the Splash color what i mean like when you pressed a button theres an accent colors slowly filling up the button. Its not the Button Color.Chaotic
G
5
TextButton(            
 style: ButtonStyle(
  overlayColor: MaterialStateColor.resolveWith((states) => Colors.red),
  ),
 child: ..., 
)

reference to Flutter TextButton splashColor property

Grumble answered 6/4, 2021 at 9:4 Comment(0)
N
13

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.________),
  ),
)
Nerve answered 6/4, 2021 at 8:40 Comment(0)
W
6

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

Welton answered 7/9, 2021 at 14:8 Comment(1)
Thank bro, this way is faster & shorter than the way use ButtonStyle() like others recommendedShayla
G
5
TextButton(            
 style: ButtonStyle(
  overlayColor: MaterialStateColor.resolveWith((states) => Colors.red),
  ),
 child: ..., 
)

reference to Flutter TextButton splashColor property

Grumble answered 6/4, 2021 at 9:4 Comment(0)
R
0

You can change Splash color like this:

            Theme(
              data: ThemeData(
                splashColor: Colors.red,
                highlightColor: Colors.black.withOpacity(.5),
              ),
              child: ListTile(
                  title: Text(
                    "New theme splash",
                    textAlign: TextAlign.center,
                  ),
                  onTap: () {}),
            ),
Rector answered 11/6, 2021 at 8:50 Comment(0)
S
0

I prefer NearHuscarl's Anser. but after flutter v3.1.0 you can implement like

TextButton(
  style: TextButton.styleFrom(
    backgroundColor: Colors.blueGrey,
    foregroundColor: Colors.white
  ),
  child: const Center(child: Text('OK')),
  onPressed: () {},
)
Stour answered 21/12, 2023 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.