본문 바로가기

# 02/Flutter

[Flutter] GestureDetector behavior

반응형

/// How this gesture detector should behave during hit testing.

///

/// This defaults to [HitTestBehavior.deferToChild] if [child] is not null and

/// [HitTestBehavior.translucent] if child is null.


child 가 null 이 아닌 경우 기본 값은 [HitTestBehavior.deferToChild]


child 가 null 인 경우 기본 값은 [HitTestBehavior.translucent]




/// How to behave during hit tests.
enum HitTestBehavior {
/// Targets that defer to their children receive events within their bounds
/// only if one of their children is hit by the hit test.
deferToChild,

/// Opaque targets can be hit by hit tests, causing them to both receive
/// events within their bounds and prevent targets visually behind them from
/// also receiving events.
opaque,

/// Translucent targets both receive events within their bounds and permit
/// targets visually behind them to also receive events.
translucent,
}


deferToChild 

-> 자식을 갖고있는 대상은 자식 중 하나가 hit test에 해당하는 경우에만 해당 범위 내에서 이벤트를 받는다.


opaque 

-> 불투명 한 대상은 hit test에 의해 hit 될 수 있으므로 둘 다 해당 범위 내에서 이벤트를 수신하고 시각적으로 뒤에 있는 대상도 이벤트를 수신하지 못하도록 한다.


translucent 

-> 반투명 타겟은 경계 내에서 이벤트를 수신하고 시각적으로 뒤에 있는 타겟도 이벤트를 수신하도록 허용한다.




import 'package:flutter/material.dart';

class GestureDetectorTest extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Gesture Detector Test')
),
body: Center(
child: GestureDetector(
// behavior: HitTestBehavior.deferToChild,
// behavior: HitTestBehavior.opaque,
// behavior: HitTestBehavior.translucent,
onTap: () {
print('----------------------first GD on tap');
},
child: Container(
width: 200,
height: 200,
color: Colors.amber,
child: GestureDetector(
// behavior: HitTestBehavior.deferToChild,
// behavior: HitTestBehavior.opaque,
// behavior: HitTestBehavior.translucent,
onTap: () {
print('----------------------second GD on tap');
},
),
),
),
),
);
}
}


첫 번째 GestureDetector 경우 child를 갖고 있으므로 behavior 기본 값은 HitTestBehavior.deferToChild 이고


두 번째 GestureDetector 경우 child를 갖고 있지 않으므로 behavior 기본 값은 HitTestBehavior.translucent 이다.



1. ----------------------first GD on tap 이 프린트 되는 경우


둘 다 HitTestBehavior.deferToChild

first GD HitTestBehavior.opaque, second GD HitTestBehavior.deferToChild

first GD HitTestBehavior.translucent, second GD HitTestBehavior.deferToChild





2. ----------------------second GD on tap 이 프린트 되는 경우


first GD HitTestBehavior.deferToChild, second GD HitTestBehavior.opaque

first GD HitTestBehavior.deferToChild, second GD HitTestBehavior.translucent

first GD HitTestBehavior.opaque, second GD HitTestBehavior.opaque

first GD HitTestBehavior.opaque, second GD HitTestBehavior.translucent

first GD HitTestBehavior.translucent, second GD HitTestBehavior.opaque

first GD HitTestBehavior.translucent, second GD HitTestBehavior.translucent




반응형

'# 02 > Flutter' 카테고리의 다른 글

[Flutter] 깊은 복사 코드  (0) 2020.11.22
[Flutter] 깊은 복사  (0) 2020.11.22
[Flutter] InheritedWidget  (0) 2020.10.05
[Flutter] StatelessWidget 에서 시작 시 함수를 호출하는 방법  (0) 2020.09.28
[Flutter] final & const  (1) 2020.09.28