当前位置:网站首页>Flutter 父子组件如何都能收到点击事件
Flutter 父子组件如何都能收到点击事件
2022-08-05 04:50:00 【bawomingtian123】
在实际项目中,在使用点击事件时,由于需求设计问题,需要子组件和父组件都能收到onTap回调,故会出现类似下面demo中的源码。

按照Flutter 点击事件的设计思路,这样的源码肯定是满足不了需求,在点击组件时,只有子组件会有回调打印,父组件是没有回调的。
那父组件怎么样才能也收到长按事件呢。我们按照以下修改可以满足需求
/// Created by wuliquan on 19.7.22.
/// 测试页面
class SecondPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return SecondPageState();
}
}
class SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
return Center(
child: RawGestureDetector(
gestures: {
AllowMultipleGestureRecognizer: GestureRecognizerFactoryWithHandlers<AllowMultipleGestureRecognizer>(
() => AllowMultipleGestureRecognizer(),
(AllowMultipleGestureRecognizer instance) {
instance.onTap = () => print('tap on parent ');
},
)
},
//Parent Container
child: Container(
width: 100,
height: 100,
color: Colors.blue,
alignment: Alignment.center,
child: GestureDetector(
onLongPress: () {
print('tap on child');
},
child: Container(
width: 50,
height: 50,
color: Colors.orange,
),
),
)),
);
}
}
class AllowMultipleGestureRecognizer extends TapGestureRecognizer {
@override
void rejectGesture(int pointer) {
acceptGesture(pointer);
}
}
使用RawGestureDetector包裹手势识别器AllowMultipleGestureRecognizer
AllowMultipleGestureRecognizer是我们继承TapGestureRecognizer 复写了rejectGesture方法
@override
void rejectGesture(int pointer) {
acceptGesture(pointer);
}重点:
这个方法在手势识别器在手势竞技场竞争失败后,我们手动处理,让它可以继续处理手势,从而我们父组件也能收到回调打印日志
这里简单说明下手势竞技的规则,在分配手势时按照深度优先的策略进行,这里由于我们只是注册了最简单的onTap回调,在底层上报up事件时,会直接返回命中测试组件列表中最后的那个组件,也就是深度最深的那个组件,也就是子组件。
以上面例子说明,在竞技场宣布子组件获胜后,会通知父组件的rejectGesture。由于我们复写了rejectGesture方法,让父手势识别器继续工作。
边栏推荐
- dedecms织梦tag标签不支持大写字母修复
- LeetCode:1403. 非递增顺序的最小子序列【贪心】
- [Surveying] Quick Summary - Excerpt from Gaoshu Gang
- [MRCTF2020] PYWebsite
- Error creating bean with name ‘configDataContextRefresher‘ defined in class path resource
- Detailed explanation of each module of ansible
- [Geek Challenge 2019]FinalSQL
- Homework 8.4 Interprocess Communication Pipes and Signals
- Shell(4) Conditional Control Statement
- Shell(4)条件控制语句
猜你喜欢

AUTOCAD - dimension association
![[BJDCTF2020]EasySearch](/img/60/464de3bcdda876171b9f61ad31bff1.png)
[BJDCTF2020]EasySearch

Day019 Method overriding and introduction of related classes

Paparazzi: Surface Editing by way of Multi-View Image Processing
![[MRCTF2020] PYWebsite](/img/d4/57e8e5ee45b742894679f3f5671516.png)
[MRCTF2020] PYWebsite

Four-digit display header design

how to measure distance from point to face in creo

Qixi Festival code confession

bytebuffer 内部结构

基于Web的商城后台管理系统的设计与实现
随机推荐
ESP32 485光照度
Shell(4)条件控制语句
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
[8.3] Code Source - [meow ~ meow ~ meow~] [tree] [and]
How to wrap markdown - md file
日志导致线程Block的这些坑,你不得不防
[CISCN2019 South China Division]Web11
uboot开启调试打印信息
请写出SparkSQL语句
淘宝账号如何快速提升到更高等级
MySQL中控制导出文件后变为了\N有什么解决方案吗?
Day14 jenkins部署
社区分享|腾讯海外游戏基于JumpServer构建游戏安全运营能力
七夕节赚徽章拉
The solution to the failure to read channel information when dedecms generates a message in the background
开发属于自己的node包
Cron(Crontab)--使用/教程/实例
for..in和for..of的区别
University Physics---Particle Kinematics