繁体   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.

在我以前的 FlatButton 小部件上,我能够在按下时更改启动颜色。 但是现在我正在使用 TextButton 小部件,我怎样才能以有效的方式在MaterialApp ThemeData上或直接在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. 但我找不到这个overlayColor

首先请记住,TextButton 的主要属性设置其文本和图标的颜色。 它不会改变波纹颜色。 其次在 Textbutton 中没有直接的属性来改变启动颜色。 因此,如果您想将初始颜色更改为透明,您可以这样做。

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

参考Flutter TextButton splashColor 属性

您可以像这样更改 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