对于屏幕手指的触摸,使用Listener进行监听,Listener是一个功能Widget,内部具有屏幕事件的响应。
指针事件
-
onPointerDown,// 手指按下时调用
-
onPointerMove,// 手指移动时调用
-
onPointerUp,// 手指抬起时调用
-
onPointerEnter,// 待确定
-
onPointerExit,// 待确定
-
onPointerHover,// 待确定
-
onPointerCancel,// 待确定
-
onPointerSignal,// 待确定
-
behavior = HitTestBehavior.deferToChild,// 待确定
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Listener(
onPointerEnter: (PointerEnterEvent event) => setState(() {
print("onPointerEnter");
}),
onPointerDown: (PointerDownEvent event) => setState(() {
print("onPointerDown");
}),
onPointerMove: (PointerMoveEvent event) => setState(() {
print("onPointerMove");
}),
onPointerUp: (PointerUpEvent event) => setState(() {
print("onPointerUp");
}),
onPointerExit: (PointerExitEvent event) => setState(() {
print("onPointerExit");
}),
onPointerCancel: (PointerCancelEvent event) => setState(() {
print("onPointerCancel");
}),
onPointerHover: (PointerHoverEvent event) => setState(() {
print("onPointerHover");
}),
onPointerSignal: (PointerSignalEvent event) => setState(() {
print("onPointerSignal");
}),
child: Center(
child: Container(
width: 200,
height: 150,
color: Colors.red[200],
alignment: Alignment.center,
child: Text(
"Pointer event,behavior=deferToChild",
style: TextStyle(fontSize: 18),
),
),
),
behavior: HitTestBehavior.deferToChild,
),
Listener(
onPointerEnter: (PointerEnterEvent event) => setState(() {
print("onPointerEnter");
}),
onPointerDown: (PointerDownEvent event) => setState(() {
print("onPointerDown");
}),
onPointerMove: (PointerMoveEvent event) => setState(() {
print("onPointerMove");
}),
onPointerUp: (PointerUpEvent event) => setState(() {
print("onPointerUp");
}),
onPointerExit: (PointerExitEvent event) => setState(() {
print("onPointerExit");
}),
onPointerCancel: (PointerCancelEvent event) => setState(() {
print("onPointerCancel");
}),
onPointerHover: (PointerHoverEvent event) => setState(() {
print("onPointerHover");
}),
onPointerSignal: (PointerSignalEvent event) => setState(() {
print("onPointerSignal");
}),
child: Center(
child: Container(
width: 200,
height: 150,
color: Colors.blue[200],
alignment: Alignment.center,
child: Text(
"Pointer event,behavior=opaque",
style: TextStyle(fontSize: 18),
),
),
),
behavior: HitTestBehavior.opaque,
),
Listener(
onPointerEnter: (PointerEnterEvent event) => setState(() {
print("onPointerEnter");
}),
onPointerDown: (PointerDownEvent event) => setState(() {
print("onPointerDown");
}),
onPointerMove: (PointerMoveEvent event) => setState(() {
print("onPointerMove");
}),
onPointerUp: (PointerUpEvent event) => setState(() {
print("onPointerUp");
}),
onPointerExit: (PointerExitEvent event) => setState(() {
print("onPointerExit");
}),
onPointerCancel: (PointerCancelEvent event) => setState(() {
print("onPointerCancel");
}),
onPointerHover: (PointerHoverEvent event) => setState(() {
print("onPointerHover");
}),
onPointerSignal: (PointerSignalEvent event) => setState(() {
print("onPointerSignal");
}),
child: Center(
child: Container(
width: 200,
height: 150,
color: Colors.green[200],
alignment: Alignment.center,
child: Text(
"Pointer event,behavior=translucent",
style: TextStyle(fontSize: 18),
),
),
),
behavior: HitTestBehavior.translucent,
),
],
),
);
}
}
网友评论