当前位置:网站首页>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.
边栏推荐
- MySQL中控制导出文件后变为了\N有什么解决方案吗?
- Mysql的undo log详解
- 基于Web的商城后台管理系统的设计与实现
- Is the NPDP certificate high in gold content?Compared to PMP?
- dedecms error The each() function is deprecated
- Day14 jenkins deployment
- 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?
- [MRCTF2020] PYWebsite
- 1007 Climb Stairs (greedy | C thinking)
- How to identify false evidence and evidence?
猜你喜欢
随机推荐
[BJDCTF2020]EasySearch
ansible各个模块详解
8.04 Day35-----MVC three-tier architecture
机器学习概述
概率论的学习和整理8: 几何分布和超几何分布
Develop your own node package
Feature preprocessing
The role of DataContext in WPF
Flutter学习-开篇
ESP32 485光照度
狗仔队:表面编辑多视点图像处理
[MRCTF2020]Ezpop(详解)
upload upload pictures to Tencent cloud, how to upload pictures
小程序_动态设置tabBar主题皮肤
[极客大挑战 2019]FinalSQL
uboot开启调试打印信息
Mysql的undo log详解
【8.3】代码源 - 【喵 ~ 喵 ~ 喵~】【树】【与】
使用IDEA连接TDengine服务器
What is ASEMI photovoltaic diode, the role of photovoltaic diode