当前位置:网站首页>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的redo log详解
关于sklearn库的安装
工业级远距离无线传输装置的功能有哪些?
There are a lot of 4T hard drives remaining, prompting "No space left on device" insufficient disk space
The solution to the failure to read channel information when dedecms generates a message in the background
Mysql的undo log详解
Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
Mysql's redo log detailed explanation
Detailed explanation of each module of ansible
u-boot debugging and positioning means
随机推荐
4T硬盘剩余很多提示“No space left on device“磁盘空间不足
AUTOCAD - dimension association
[BJDCTF2020]EasySearch
C#关于set()和get()方法的理解及使用
How to solve complex distribution and ledger problems?
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
动力小帆船制作方法简单,电动小帆船制作方法
The role of DataContext in WPF
Flutter learning three-Flutter basic structure and principle
软件管理rpm
write the story about us
LeetCode:1403. 非递增顺序的最小子序列【贪心】
[8.1] Code Source - [The Second Largest Number Sum] [Stone Game III] [Balanced Binary Tree]
The log causes these pits in the thread block, you have to guard against
淘宝账号如何快速提升到更高等级
u-boot debugging and positioning means
1007 Climb Stairs (贪心 | C思维)
bytebuffer 使用demo
[Nine Lectures on Backpacks - 01 Backpack Problems]
2023年信息与通信工程国际会议(JCICE 2023)