簡體   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