如何在 Flutter 中禁用默认的 Widget 飞溅效果? [英] How to disable default Widget splash effect in Flutter?

查看:19
本文介绍了如何在 Flutter 中禁用默认的 Widget 飞溅效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁用 Widget 上的默认飞溅/波纹/墨迹效果?有时效果是不需要的,例如在以下 TextField 情况下:

How can I disable the default splash/ripple/ink effect on a Widget? Sometimes the effect is unwanted, such as in the following TextField case:

推荐答案

根据上面@hunter 的建议,我发现通过在我的主题中同时设置 highlightColorsplashColorColors.transparent 去除了波纹.

Per @hunter's suggestion above, I found that by setting both highlightColor and splashColor in my theme to Colors.transparent removed the ripple.

我确实担心设置 highlightColor 可能会产生一些连锁反应,但我还没有注意到.

I do hold some concerns that setting highlightColor might have some knock-on effects, but I haven't noticed any yet.

虽然我最初的答案有很多赞成票,但我学得越多,我就越意识到这确实不是正确的方法.正如下面一些人所指出的,更好的解决方案是使用 splashFactory.例如,下面的代码显示它是直接通过样式设置的,或者您也可以在主题中设置它:

While my original answer has loads of up-votes, the more I learn, the more I've realised that it really isn't the right way to do it. As several people have pointed out below, a better solution is to use the splashFactory. For example, the code below shows it being set directly via the style, or you can set it in your theme too:

ElevatedButton(
  onPressed: onPressed,
  style: ElevatedButton.styleFrom(
    splashFactory: NoSplash.splashFactory,
  ),
  child: child,
);

这篇关于如何在 Flutter 中禁用默认的 Widget 飞溅效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆