简体   繁体   English

Flutter 2.0 - 按下时如何更改 Textbutton 启动颜色

[英]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.在我以前的 FlatButton 小部件上,我能够在按下时更改启动颜色。 But now I'm using TextButton widget, how can i changed its color the efficient way on the MaterialApp ThemeData or directly on the TextButton widget.但是现在我正在使用 TextButton 小部件,我怎样才能以有效的方式在MaterialApp ThemeData上或直接在TextButton小部件上更改它的颜色。

Currenly this is my TextButton目前这是我的 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但我找不到这个overlayColor

First keep in mind that the primary property on a TextButton sets the colour of its text and icon.首先请记住,TextButton 的主要属性设置其文本和图标的颜色。 It does not change the ripple color.它不会改变波纹颜色。 Secondly in Textbutton there is no direct property to change splash color.其次在 Textbutton 中没有直接的属性来改变启动颜色。 So if you want to change splash color to transparent you can do it like this.因此,如果您想将初始颜色更改为透明,您可以这样做。

TextButton(
  style: ButtonStyle(
    overlayColor: MaterialStateProperty.all(Colors.________),
  ),
)
TextButton(            
 style: ButtonStyle(
  overlayColor: MaterialStateColor.resolveWith((states) => Colors.red),
  ),
 child: ..., 
)

reference to Flutter TextButton splashColor property参考Flutter TextButton splashColor 属性

You can change Splash color like this:您可以像这样更改 Splash 颜色:

            Theme(
              data: ThemeData(
                splashColor: Colors.red,
                highlightColor: Colors.black.withOpacity(.5),
              ),
              child: ListTile(
                  title: Text(
                    "New theme splash",
                    textAlign: TextAlign.center,
                  ),
                  onTap: () {}),
            ),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM