1 IgnorePointer
GestureDetector(
onTap: () {
debugPrint('红色块外层事件触发');
},
child: IgnorePointer(
ignoring: true,
child: GestureDetector(
onTap: () {
debugPrint('红色块被点击');
},
child: Container(
height: 100,
width: 100,
color: Colors.red,
),
),
),
)
IgnorePointer的效果是会忽略它所包含的所有子节点上的事情。只要点击的是它的子节点,那么所有的事件都不会触发。所以点击红色块,外层事件也不会触发。
2 AbsorbPointer
GestureDetector(
onTap: () {
debugPrint('外部被点击');
},
child: AbsorbPointer(
absorbing: true,
child: GestureDetector(
onTap: () {
debugPrint('红色块被点击');
},
child: Container(
height: 100,
width: 100,
color: Colors.red,
child: GestureDetector(
onTap: () {
debugPrint('紫色块被点击');
},
child: Center(
child: Container(
height: 80,
width: 80,
color: Colors.purple,
),
),
),
),
),
),
)
AbsorbPointer效果是点击它的子节点,子节点上的事件不会触发,但是会触发它的父节点上的事件。