当前位置:网站首页>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方法,让父手势识别器继续工作。
边栏推荐
- 特征预处理
- Qixi Festival code confession
- Detailed explanation of each module of ansible
- C++ core programming
- Day019 方法重写与相关类的介绍
- 多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
- [8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
- 【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
- flink读取mongodb数据源
- NPDP证书含金量高吗?跟PMP相比?
猜你喜欢

多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层

App快速开发建设心得:小程序+自定义插件的重要性

二叉树基本性质+oj题解析
![[BSidesCF 2019]Kookie](/img/29/19e7c244feb86b37ab32a53aa11f25.png)
[BSidesCF 2019]Kookie

特征预处理

What is the function of industrial-grade remote wireless transmission device?

Develop your own node package

There are a lot of 4T hard drives remaining, prompting "No space left on device" insufficient disk space

不看后悔,appium自动化环境完美搭建

Error creating bean with name ‘configDataContextRefresher‘ defined in class path resource
随机推荐
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
The solution to the failure to read channel information when dedecms generates a message in the background
flink reads mongodb data source
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
[Geek Challenge 2019]FinalSQL
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
AUTOCAD——标注关联
【cesium】加载并定位 3D Tileset
Event parse tree Drain3 usage and explanation
What is ASEMI photovoltaic diode, the role of photovoltaic diode
【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
4T硬盘剩余很多提示“No space left on device“磁盘空间不足
About the installation of sklearn library
【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】
【无标题】
关于sklearn库的安装
Mini Program_Dynamic setting of tabBar theme skin
Qixi Festival earn badges
Homework 8.4 Interprocess Communication Pipes and Signals