一、指针事件Pointer

Pointer 代表的是人机界面交互的原始数据。一共有四种指针事件:

PointerDownEvent 指针在特定位置与屏幕接触PointerMoveEvent 指针从屏幕的一个位置移动到另外一个位置PointerUpEvent 指针与屏幕停止接触PointerCancelEvent 指针因为一些特殊情况被取消
Pointer的原理是什么呢?

在指针落下时,框架做了一个 hit test 的操作,确定与屏幕发生接触的位置上有哪些Widget以及分发给最内部的组件去响应;
事件会沿着最内部的组件向组件树的根冒泡分发;
并且不存在用于取消或者停止指针事件进一步分发的机制;

二、手势识别GestureDetector

  1. GestureDetector({
  2. Key? key,
  3. this.child,
  4. this.onTapDown,
  5. this.onTapUp,
  6. this.onTap,
  7. this.onTapCancel,
  8. this.onSecondaryTap,
  9. this.onSecondaryTapDown,
  10. this.onSecondaryTapUp,
  11. this.onSecondaryTapCancel,
  12. this.onTertiaryTapDown,
  13. this.onTertiaryTapUp,
  14. this.onTertiaryTapCancel,
  15. this.onDoubleTapDown,
  16. this.onDoubleTap,
  17. this.onDoubleTapCancel,
  18. this.onLongPressDown,
  19. this.onLongPressCancel,
  20. this.onLongPress,
  21. this.onLongPressStart,
  22. this.onLongPressMoveUpdate,
  23. this.onLongPressUp,
  24. this.onLongPressEnd,
  25. this.onSecondaryLongPressDown,
  26. this.onSecondaryLongPressCancel,
  27. this.onSecondaryLongPress,
  28. this.onSecondaryLongPressStart,
  29. this.onSecondaryLongPressMoveUpdate,
  30. this.onSecondaryLongPressUp,
  31. this.onSecondaryLongPressEnd,
  32. this.onTertiaryLongPressDown,
  33. this.onTertiaryLongPressCancel,
  34. this.onTertiaryLongPress,
  35. this.onTertiaryLongPressStart,
  36. this.onTertiaryLongPressMoveUpdate,
  37. this.onTertiaryLongPressUp,
  38. this.onTertiaryLongPressEnd,
  39. this.onVerticalDragDown,
  40. this.onVerticalDragStart,
  41. this.onVerticalDragUpdate,
  42. this.onVerticalDragEnd,
  43. this.onVerticalDragCancel,
  44. this.onHorizontalDragDown,
  45. this.onHorizontalDragStart,
  46. this.onHorizontalDragUpdate,
  47. this.onHorizontalDragEnd,
  48. this.onHorizontalDragCancel,
  49. this.onForcePressStart,
  50. this.onForcePressPeak,
  51. this.onForcePressUpdate,
  52. this.onForcePressEnd,
  53. this.onPanDown,
  54. this.onPanStart,
  55. this.onPanUpdate,
  56. this.onPanEnd,
  57. this.onPanCancel,
  58. this.onScaleStart,
  59. this.onScaleUpdate,
  60. this.onScaleEnd,
  61. this.behavior,
  62. this.excludeFromSemantics = false,
  63. this.dragStartBehavior = DragStartBehavior.start,
  64. })

三、跨组件事件EventBus

在组件之间如果有事件需要传递,一方面可以一层层来传递,另一方面我们也可以使用一个EventBus工具来完成。
其实EventBus是一种非常常见的跨组件通信的方式:

EventBus相当于是一种订阅者模式,通过一个全局的对象来管理;这个EventBus我们可以自己实现,也可以使用第三方的EventBus;
这里我们直接选择第三方的EventBus:
dependencies:
event_bus: ^1.1.1

第一:我们需要定义一个希望在组件之间传递的对象:

我们可以称之为一个时间对象,也可以是我们平时开发中用的模型对象(model)
class UserInfo {
String nickname;
int level;

UserInfo(this.nickname, this.level);
}

第二:创建一个全局的EventBus对象
final eventBus = EventBus();

第三:在某个Widget中,发出事件:
class HYButton extends StatelessWidget {
@override
Widget build(BuildContext context) {
return RaisedButton(
child: Text(“HYButton”),
onPressed: () {
final info = UserInfo(“why”, 18);
eventBus.fire(info);
},
);
}
}

第四:在某个Widget中,监听事件
class HYText extends StatefulWidget {
@override
_HYTextState createState() => _HYTextState();
}

class _HYTextState extends State {
String message = “Hello Coderwhy”;

@override
void initState() {
super.initState();

  1. eventBus.on<UserInfo>().listen((data) {<br /> setState(() {<br /> message = "${data.nickname}-${data.level}";<br /> });<br /> });<br /> }

@override
Widget build(BuildContext context) {
return Text(message, style: TextStyle(fontSize: 30),);
}
}