GestureDetector
특정 위젯에 대해 제스쳐 이벤트를 처리할 수 있도록 해준다.
제스쳐란 탭(Tap), 더블탭(Double Tap), 오래 누르기(Long Press), 드래그(Drag), 확대/축소(Scaling) 등을 말한다.
버튼처럼 기본적으로 Tap 이벤트를 처리하지 못하는 위젯들도 GestureDetector 로 감싸주면 제스쳐 이벤트를 처리할 수 있다.
Code Template
import 'package:flutter/material.dart';
/// Flutter code sample for [GestureDetector].
void main() => runApp(const GestureDetectorExampleApp());
class GestureDetectorExampleApp extends StatelessWidget {
const GestureDetectorExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: GestureDetectorExample(),
);
}
}
class GestureDetectorExample extends StatefulWidget {
const GestureDetectorExample({super.key});
@override
State<GestureDetectorExample> createState() => _GestureDetectorExampleState();
}
class _GestureDetectorExampleState extends State<GestureDetectorExample> {
bool _lightIsOn = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
Icons.lightbulb_outline,
color: _lightIsOn ? Colors.yellow.shade600 : Colors.black,
size: 60,
),
),
GestureDetector(
onTap: () {
setState(() {
// Toggle light when tapped.
_lightIsOn = !_lightIsOn;
});
},
child: Container(
color: Colors.yellow.shade600,
padding: const EdgeInsets.all(8),
// Change button text when light changes state.
child: Text(_lightIsOn ? 'TURN LIGHT OFF' : 'TURN LIGHT ON'),
),
),
],
),
),
);
}
}
실행 결과

참고
https://api.flutter.dev/flutter/widgets/GestureDetector-class.html
https://docs.flutter.dev/ui/interactivity/gestures
'개발 > Flutter' 카테고리의 다른 글
[Flutter] Hive (0) | 2024.08.13 |
---|---|
[Flutter] SharedPreferences (0) | 2024.08.11 |
[Flutter] LinearProgressIndicator (0) | 2024.07.30 |
[Flutter] Navigation (0) | 2024.07.29 |
[Flutter] Dialog (0) | 2024.07.23 |
GestureDetector
특정 위젯에 대해 제스쳐 이벤트를 처리할 수 있도록 해준다.
제스쳐란 탭(Tap), 더블탭(Double Tap), 오래 누르기(Long Press), 드래그(Drag), 확대/축소(Scaling) 등을 말한다.
버튼처럼 기본적으로 Tap 이벤트를 처리하지 못하는 위젯들도 GestureDetector 로 감싸주면 제스쳐 이벤트를 처리할 수 있다.
Code Template
import 'package:flutter/material.dart';
/// Flutter code sample for [GestureDetector].
void main() => runApp(const GestureDetectorExampleApp());
class GestureDetectorExampleApp extends StatelessWidget {
const GestureDetectorExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: GestureDetectorExample(),
);
}
}
class GestureDetectorExample extends StatefulWidget {
const GestureDetectorExample({super.key});
@override
State<GestureDetectorExample> createState() => _GestureDetectorExampleState();
}
class _GestureDetectorExampleState extends State<GestureDetectorExample> {
bool _lightIsOn = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
Icons.lightbulb_outline,
color: _lightIsOn ? Colors.yellow.shade600 : Colors.black,
size: 60,
),
),
GestureDetector(
onTap: () {
setState(() {
// Toggle light when tapped.
_lightIsOn = !_lightIsOn;
});
},
child: Container(
color: Colors.yellow.shade600,
padding: const EdgeInsets.all(8),
// Change button text when light changes state.
child: Text(_lightIsOn ? 'TURN LIGHT OFF' : 'TURN LIGHT ON'),
),
),
],
),
),
);
}
}
실행 결과

참고
https://api.flutter.dev/flutter/widgets/GestureDetector-class.html
https://docs.flutter.dev/ui/interactivity/gestures
'개발 > Flutter' 카테고리의 다른 글
[Flutter] Hive (0) | 2024.08.13 |
---|---|
[Flutter] SharedPreferences (0) | 2024.08.11 |
[Flutter] LinearProgressIndicator (0) | 2024.07.30 |
[Flutter] Navigation (0) | 2024.07.29 |
[Flutter] Dialog (0) | 2024.07.23 |