Flutter点击水波纹效果封装

在flutter中, 普通Widget是没有点击效果的,设置点击事件的时候点着没有感觉。

可以利用Flutter提供的水波纹widget来对控件进行包裹,这样就有点击效果了。

1、使用InkWell实现child点击水波纹效果
class RippleWidget extends StatelessWidget{

  final Function onTap;
  final Widget child;

  RippleWidget({this.child, this.onTap});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      child: child,
      ///点击颜色
      splashColor: Colors.blue,
      onTap: (){
      	///延迟200ms 效果更明显点
        Future.delayed(Duration(milliseconds: 200), onTap);
      },
    );
  }

}
2、使用InkInkWell实现点击控件封装
class RippleButton extends StatelessWidget{

  final Function onTap;///点击事件
  final String title; 
  Decoration decoration = BoxDecoration();
  double radius = 0;
  Color splashColor =  Colors.blue;
  double height = 40;
  double width = 60;
  TextStyle style = Styles.blackTextStyleSp2;

  RippleButton({this.title, this.onTap, this.decoration, this.radius, this.splashColor, this.height, this.width, this.style});

  @override
  Widget build(BuildContext context) {
    return Ink(
      decoration: decoration??BoxDecoration(),
      child: InkWell(
        borderRadius: BorderRadius.all(Radius.circular(radius)),
        splashColor: splashColor,
        child: Container(
          height: height,
          width: width,
          child: Center(child: Text(title, style: style, textAlign: TextAlign.center,),),
        ),
        onTap: onTap,
      ),
    );
  }
}

如果我们的布局有圆角的话,可以使用Inkdecoration给我们点击波纹也添加一层圆角效果。否则控件的点击效果是没有圆角效果的。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值