InkWell

InkWell 组件在用户点击时出现“水波纹”效果,定义如下:

  1. const InkWell({
  2. Key key,
  3. Widget child,
  4. GestureTapCallback onTap, //点击,如果不设置无法出现“水波纹”效果
  5. GestureTapCallback onDoubleTap, //双击
  6. GestureLongPressCallback onLongPress, //长按
  7. GestureTapDownCallback onTapDown,
  8. GestureTapCancelCallback onTapCancel,
  9. ValueChanged<bool> onHighlightChanged,
  10. ValueChanged<bool> onHover,
  11. MouseCursor mouseCursor,
  12. Color focusColor,
  13. Color hoverColor,
  14. Color highlightColor, //高亮颜色(按住时显示的颜色)
  15. MaterialStateProperty<Color> overlayColor,
  16. Color splashColor, //水波纹颜色(设置了highlightColor属性后 splashColor将不起效果)
  17. InteractiveInkFeatureFactory splashFactory,
  18. double radius, //水波纹的半径
  19. BorderRadius borderRadius, //圆角,需与Ink组件组合使用
  20. ShapeBorder customBorder,
  21. bool enableFeedback = true,
  22. bool excludeFromSemantics = false,
  23. FocusNode focusNode,
  24. bool canRequestFocus = true,
  25. ValueChanged<bool> onFocusChange,
  26. bool autofocus = false,
  27. })

示例1:简单应用

fdtdttyddasdyrt.gif

  1. InkWell(
  2. onTap: () {},
  3. child: Container(width: 100, height: 50, child: Text('点击')),
  4. ),

产生问题?
如果在InkWell的上下都出现的颜色的设置,如上中的Container中如果加入了color:Colors.white,或者是Container中的其他widget设置了color属性,这时候InkWell的水波纹效果会无效。

示例2:widget 设置水波纹点击效果 并设置widget背景

fdtdttyddasdyrt.gif

  1. //此处用Container包裹,设置背景色,不会出现水波纹效果
  2. Material(
  3. color: Colors.red, // 设置背景颜色 默认矩形
  4. child: InkWell(
  5. // 不要在这里设置背景色,for则会遮挡水波纹效果,如果设置的话尽量设置Material下面的color来实现背景色
  6. child: Container(width: 200, height: 70, child: Text('按钮')),
  7. onTap: () {},
  8. ),
  9. ),

示例3:添加边距和圆角边框

fdtdttyddasdyrt.gif

  1. InkWell(
  2. onTap: () {},
  3. child: Container(
  4. width: 200,
  5. height: 70,
  6. decoration: BoxDecoration(
  7. border: Border.all(color: Colors.blue),
  8. borderRadius: BorderRadius.circular(35),
  9. ),
  10. child: Text('按钮'),
  11. ),
  12. ),

产生问题?
发现“水波纹”超出的了圆角边框,如何解决这个问题呢?Ink隆重登场。

Ink

Ink的官方解释:

A convenience widget for drawing images and other decorations on [Material] widgets, so that [InkWell] and [InkResponse] splashes will render over them.

简单翻译:Ink控件用于在[Material]控件上绘制图像和其他装饰,以便[InkWell]、[InkResponse]控件的“水波纹”效果在其上面显示。定义如下:

  1. Ink({
  2. Key key,
  3. this.padding,
  4. Color color,
  5. Decoration decoration,
  6. this.width,
  7. this.height,
  8. this.child,
  9. })

设置背景色(效果等同于示例2)

Ink、InkWell - 图4

  1. Ink(
  2. color: Colors.red,
  3. child: InkWell(
  4. onTap: () {},
  5. child: Container(width: 200, height: 70, child: Text('按钮')),
  6. ),
  7. ),

设置圆角(修复示例3问题)

fdtdttyddasdyrt.gif

  1. Ink(
  2. decoration: BoxDecoration(
  3. color: Colors.red,
  4. border: Border.all(color: Colors.blue),
  5. borderRadius: BorderRadius.circular(35),
  6. ),
  7. child: InkWell(
  8. //圆角设置,给水波纹也设置同样的圆角(如果这里不设置就会出现矩形的水波纹效果)
  9. borderRadius: BorderRadius.circular(35),
  10. onTap: () {},
  11. child: Container(width: 200, height: 70, child: Text('按钮')),
  12. ),
  13. ),

InkResponse

InkResponse组件也可实现InkWell组件相同的功能,因为InkWell继承于InkResponse,相比之下,InkResponse可以选择控制水波纹以及高亮底色的形状和剪裁效果。

设置自定义水波纹颜色点击效果

fdtdttyddasdyrt.gif fdtdttyddasdyrt.gif

左侧是 containedInkWell 为true的效果;右侧是为false的效果

  1. Ink(
  2. decoration: BoxDecoration(
  3. color: Colors.red,
  4. borderRadius: BorderRadius.circular(35),
  5. ),
  6. child: InkResponse(
  7. borderRadius: BorderRadius.circular(35),
  8. //highlightColor: Colors.yellowAccent,
  9. //水波纹的颜色
  10. splashColor: Colors.green,
  11. //点击或者toch控件高亮的shape形状(rectangle 矩形;circle 圆形)
  12. highlightShape: BoxShape.rectangle,
  13. //InkResponse内部的radius这个需要注意的是,我们需要半径大于控件的宽,如果radius过小,显示的水波纹就是一个很小的圆,
  14. radius: 300.0, //水波纹的半径
  15. //true表示要剪裁水波纹响应的界面 false不剪裁 如果控件是圆角不剪裁的话水波纹是矩形
  16. containedInkWell: true,
  17. onTap: () {},
  18. child: Container(
  19. // 不能在InkResponse的child容器内部设置装饰器颜色,否则会遮盖住水波纹颜色的,containedInkWell设置为false就能看到是否是遮盖了。
  20. width: 200,
  21. height: 70,
  22. child: Text('按钮'),
  23. ),
  24. ),
  25. ),

设置高亮颜色点击效果

fdtdttyddasdyrt.gif

  1. Ink(
  2. decoration: BoxDecoration(
  3. color: Colors.red,
  4. borderRadius: BorderRadius.circular(35),
  5. ),
  6. child: InkResponse(
  7. borderRadius: BorderRadius.circular(35),
  8. //点击或者toch控件高亮时显示的控件在控件上层,水波纹下层
  9. highlightColor: Colors.yellowAccent,
  10. //水波纹的颜色(设置了highlightColor属性后 splashColor将不起效果)
  11. splashColor: Colors.green,
  12. //点击或者toch控件高亮的shape形状(rectangle 矩形;circle 圆形)
  13. highlightShape: BoxShape.rectangle,
  14. radius: 0.0,
  15. containedInkWell: true,
  16. onTap: () {},
  17. child: Container(width: 200, height: 70, child: Text('按钮')),
  18. ),
  19. ),