当前位置:   article > 正文

Flutter 自定义点击组件(水波纹点击效果)_flutter 点击效果

flutter 点击效果

Flutter中的 Material Design 风格中,点击按钮通常会出现水波纹效果。如果想自定义这种效果,可以使用 InkWell 和 Ink 组件。

  1. InkWell:一个具有水波纹效果和触摸事件处理功能的组件。它可以包裹其他组件来实现自定义的水波纹效果。

  2. Ink:用于在 InkWell 内部生成水波纹效果的组件。

但是使用inkWell自定义水波纹组件时,子组件设置背景色或背景图片后,会导致水波纹效果消失。这里使用Stack组件包裹,inkWell放在最上面。样式均由子组件确定!

  1. class InkWellView extends StatelessWidget {
  2. final Widget child;
  3. final void Function()? onPressed;
  4. final BorderRadius borderRadius;
  5. final Color splashColor;
  6. final Color highlightColor;
  7. final Color backColor = Colors.transparent;
  8. final double? width, height;
  9. InkWellView({
  10. required this.child,
  11. this.onPressed,
  12. this.borderRadius = const BorderRadius.all(Radius.circular(0)),
  13. this.splashColor = Colors.grey,
  14. this.highlightColor = Colors.grey,
  15. this.width,
  16. this.height,
  17. });
  18. @override
  19. Widget build(BuildContext context) {
  20. return Container(
  21. width: width,
  22. height: height,
  23. decoration: BoxDecoration(
  24. borderRadius: borderRadius,
  25. ),
  26. child: Stack(
  27. children: [
  28. child,
  29. Positioned.fill(
  30. child: Material(
  31. type: MaterialType.transparency,
  32. borderRadius: borderRadius,
  33. child: Ink(
  34. color: backColor,
  35. child: InkWell(
  36. splashColor: splashColor.withAlpha(200),
  37. highlightColor: Colors.transparent,
  38. borderRadius: borderRadius,
  39. onTap: onPressed,
  40. ),
  41. ),
  42. ),
  43. )
  44. ],
  45. ),
  46. );
  47. }
  48. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/882708
推荐阅读
相关标签
  

闽ICP备14008679号