当前位置:网站首页>How can Flutter parent and child components receive click events
How can Flutter parent and child components receive click events
2022-08-05 04:52:00 【bawomingtian123】
In the actual project, when using the click event, due to the requirement design problem, both the child component and the parent component need to receive the onTap callback, so the source code similar to the demo below will appear.

According to the design idea of Flutter's click event, such a source code will definitely not meet the requirements. When a component is clicked, only the child component will have a callback print, and the parent component will not have a callback.
How can the parent component also receive the long press event?We can meet the needs according to the following modifications
/// Created by wuliquan on 19.7.22./// Test pageclass SecondPage extends StatefulWidget {@overrideState createState() {return SecondPageState();}}class SecondPageState extends State {@overrideWidget build(BuildContext context) {return Center(child: RawGestureDetector(gestures: {AllowMultipleGestureRecognizer: GestureRecognizerFactoryWithHandlers(() => AllowMultipleGestureRecognizer(),(AllowMultipleGestureRecognizer instance) {instance.onTap = () => print('tap on parent ');},)},//Parent Containerchild: 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 {@overridevoid rejectGesture(int pointer) {acceptGesture(pointer);}} Use RawGestureDetector to wrap gesture recognizer AllowMultipleGestureRecognizer
AllowMultipleGestureRecognizer is our inheritance TapGestureRecognizerOverride the rejectGesture method
@overridevoid rejectGesture(int pointer) {acceptGesture(pointer);}Focus:
This method is handled manually after the gesture recognizer fails to compete in the gesture arena, so that it can continue to process the gesture, so that our parent component can also receive a callback printJournal
Here is a brief description of the rules of gesture competition. When assigning gestures, follow the depth-first strategy. Since we only registered the simplest onTap callback, it will be reported at the bottom.When the up event occurs, it will directly return the last component in the list of hit test components, that is, the component with the deepest depth, that is, the child component.
In the above example, after the arena declares the victory of the child component, the rejectGesture of the parent component will be notified.Since we overridden the rejectGesture method, the parent gesture recognizer continues to work.
边栏推荐
- 服务器磁盘阵列
- bytebuffer internal structure
- 【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
- upload上传图片到腾讯云,如何上传图片
- In the hot summer, teach you to use Xiaomi smart home accessories + Raspberry Pi 4 to connect to Apple HomeKit
- 特征预处理
- 软件管理rpm
- 淘宝账号如何快速提升到更高等级
- Flutter learning three-Flutter basic structure and principle
- 二叉树基本性质+oj题解析
猜你喜欢

u-boot debugging and positioning means
![[CISCN2019 South China Division]Web11](/img/15/843334fec0a5cc8cfaba92aab938db.png)
[CISCN2019 South China Division]Web11

MySQL基础(一)---基础认知及操作
![[Surveying] Quick Summary - Excerpt from Gaoshu Gang](/img/35/e5c5349b8d4ccf9203c432a9aaee7b.png)
[Surveying] Quick Summary - Excerpt from Gaoshu Gang
![[MRCTF2020]Ezpop(详解)](/img/19/920877ca36d1eda8d118637388ab05.png)
[MRCTF2020]Ezpop(详解)

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

Day019 Method overriding and introduction of related classes
How to identify false evidence and evidence?

ESP32 485光照度

【cesium】元素高亮显示
随机推荐
[SWPU2019]Web1
Dephi逆向工具Dede导出函数名MAP导入到IDA中
【学习笔记之菜Dog学C】动态内存管理之经典笔试题
Mysql的redo log详解
Flutter学习-开篇
二叉树基本性质+oj题解析
使用IDEA连接TDengine服务器
基于Web的商城后台管理系统的设计与实现
A 35-year-old software testing engineer with a monthly salary of less than 2W, resigns and is afraid of not finding a job, what should he do?
Feature preprocessing
LAB 信号量实现细节
C language - vernacular to understand the original code, inverse code and complement code
The solution to the failure to read channel information when dedecms generates a message in the background
The log causes these pits in the thread block, you have to guard against
Flutter learning three-Flutter basic structure and principle
dedecms error The each() function is deprecated
Mysql的undo log详解
JeeSite New Report
【学生毕业设计】基于web学生信息管理系统网站的设计与实现(13个页面)
Machine Learning Overview