Flutter Inkwell,RaisedButton去除水波纹的点击效果

本文介绍如何在Flutter中去除InkWell和RaisedButton组件的点击视觉效果,包括splash和高亮效果。通过设置相关属性为透明,并调整按钮的海拔属性,可以完全消除点击时的视觉反馈。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

InkWell 、RaisedButton 等组件自带 splashColor 属性和 highlightColor 属性.将这两个属性设置为透明即可去除点击的效果.

InkWell(
	onTap: () {},
	child: Text('InkWell 组件'),
    highlightColor: Colors.transparent, // 透明色
    splashColor: Colors.transparent, // 透明色
),

但是RaisedButton即使设置了 splashColor 属性和 highlightColor 属性,点击时看着还是有效果,我们看两个跟卡片有关的属性,elevation和highlightElevation,看一下源码:

  /// The [button]'s elevation when it is enabled and has not been pressed.
  ///
  /// Returns the button's [MaterialButton.elevation] if it is non-null.
  ///
  /// If button is a [FlatButton] then elevation is 0.0, otherwise it is 2.0.
  double getElevation(MaterialButton button) {
    if (button.elevation != null)
      return button.elevation;
    if (button is FlatButton)
      return 0.0;
    return 2.0;
  }
  /// The [button]'s elevation when it is enabled and has been pressed.
  ///
  /// Returns the button's [MaterialButton.highlightElevation] if it is non-null.
  ///
  /// If button is a [FlatButton] or an [OutlineButton] then the highlight
  /// elevation is 0.0, otherwise the highlight elevation is 8.0.
  double getHighlightElevation(MaterialButton button) {
    if (button.highlightElevation != null)
      return button.highlightElevation;
    if (button is FlatButton)
      return 0.0;
    if (button is OutlineButton)
      return 0.0;
    return 8.0;
  }

源码中透露了如果button是FlatButton,则海拔是0,否则默认为2.0.突出海拔默认为8.
所以RaiseButton去除点击效果代码是:

RaisedButton(
	onPressed: () {},
    child: Text('按钮'),
    splashColor: Colors.transparent,
    highlightColor: Colors.transparent,
    highlightElevation: 0, 
    elevation: 0, 
),
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值