当前位置:网站首页>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方法,让父手势识别器继续工作。
边栏推荐
- The log causes these pits in the thread block, you have to guard against
- 【8.3】代码源 - 【喵 ~ 喵 ~ 喵~】【树】【与】
- 【informix】解决启动报错大全,以及解决办法
- [MRCTF2020]Ezpop(详解)
- 4T硬盘剩余很多提示“No space left on device“磁盘空间不足
- 8.04 Day35-----MVC三层架构
- About the installation of sklearn library
- Learning and finishing of probability theory 8: Geometric and hypergeometric distributions
- Qixi Festival code confession
- Analyses the mainstream across technology solutions
猜你喜欢

Detailed explanation of each module of ansible
![[MRCTF2020] Ezpop (detailed)](/img/19/920877ca36d1eda8d118637388ab05.png)
[MRCTF2020] Ezpop (detailed)

AUTOCAD——标注关联

C++ core programming

狗仔队:表面编辑多视点图像处理

bytebuffer 内部结构

炎炎夏日教你利用小米智能家居配件+树莓派4接入Apple HomeKit

Please write the SparkSQL statement

token, jwt, oauth2, session parsing

upload upload pictures to Tencent cloud, how to upload pictures
随机推荐
Event parse tree Drain3 usage and explanation
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
Shell(4) Conditional Control Statement
延迟加载js方式async与defer区别
基于Web的商城后台管理系统的设计与实现
密码学系列之:PEM和PKCS7,PKCS8,PKCS12
Why did you start preparing for the soft exam just after the PMP exam?
动力小帆船制作方法简单,电动小帆船制作方法
Day14 jenkins deployment
1068 Find More Coins
Mini Program_Dynamic setting of tabBar theme skin
How to wrap markdown - md file
bytebuffer 内部结构
What is the function of industrial-grade remote wireless transmission device?
Excel画图
请写出SparkSQL语句
Error creating bean with name ‘configDataContextRefresher‘ defined in class path resource
dedecms织梦tag标签不支持大写字母修复
MySQL中控制导出文件后变为了\N有什么解决方案吗?
【转】什么是etcd