dart Flutter 2.0 -如何更改按下时的TextButton飞溅颜色?

deyfvvtc  于 5个月前  发布在  Flutter
关注(0)|答案(5)|浏览(110)
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

xfyts7mz

xfyts7mz1#

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

字符串
引用Flutter TextButton splashColor property

4ngedf3f

4ngedf3f2#

您可以像这样更改飞溅颜色:

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

字符串

zc0qhyus

zc0qhyus3#

我更喜欢NearHuscarl's Anser。但是在flutter v3.1.0之后,你可以实现像

TextButton(
  style: TextButton.styleFrom(
    backgroundColor: Colors.blueGrey,
    foregroundColor: Colors.white
  ),
  child: const Center(child: Text('OK')),
  onPressed: () {},
)

字符串

eeq64g8w

eeq64g8w4#

首先要记住,TextButton上的primary属性设置了文本和图标的颜色。它不会改变涟漪颜色。其次,在TextButton中没有直接的属性来改变飞溅颜色。所以如果你想将飞溅颜色改为透明,你可以这样做。

TextButton(
  style: ButtonStyle(
    overlayColor: MaterialStateProperty.all(Colors.________),
  ),
)

字符串

p4rjhz4m

p4rjhz4m5#

您可以使用辅助方法TextButton.styleFrom()快速设置TextButton的启动颜色。请注意,这实际上将根据前景色设置前景色和启动颜色,这在大多数情况下都是您想要的:

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

字符串

Live Demo

相关问题