μ μ€μ²λ₯Ό κ°μ§νλ μμ ―μ λλ€.
NULLμ΄ μλ μ½λ°±μ ν΄λΉνλ μ μ€μ²λ₯Ό μΈμν©λλ€.
μ΄ μμ ―μ μμμ΄ μμΌλ©΄ ν¬κΈ°λ ν΄λΉ μμμ λ°λ₯΄κ³ λ§μ½ μμμ΄ μμκ²½μ° λΆλͺ¨μ λ§μΆ°μ ν¬κΈ°κ° μ‘°μ λ©λλ€.
https://api.flutter.dev/flutter/widgets/GestureDetector-class.html
μ¬μ©λ²μ κ°λ¨ν©λλ€.
μ¬μ©νλ €λ Gesture μμ±μ μ ννκ³ μ΄λ€ λμμ ν μ§ μ μΈνλ©΄ λ©λλ€.
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
home: HomeScreen(),
),
);
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
double _top = 0;
double _left = 0;
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned(
top: _top,
left: _left,
child: GestureDetector(
onTap: () => print('Tapeed!'),
onPanUpdate: (details) => setState(
() {
_top += details.delta.dy;
_left += details.delta.dx;
},
),
child: Container(
height: 100,
width: 100,
color: Colors.red,
),
),
),
],
);
}
}
μμ κ°μ΄ StatefulWidgetμ μ΄μ©νμ¬ λ²νΌμ ν΄λ¦μ μνλ₯Ό λ³νμν¬μ μμ΅λλ€.
import 'package:flutter/material.dart';
void main() => runApp(
MaterialApp(
home: HomeScreen(),
),
);
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
title: _title,
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({super.key});
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
bool _lightIsOn = false;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
alignment: FractionalOffset.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
Icons.lightbulb_outline,
color: _lightIsOn ? Colors.yellow.shade600 : Colors.black,
size: 60,
),
),
GestureDetector(
onTap: () => setState(
() {
_lightIsOn = !_lightIsOn;
},
),
child: Container(
color: Colors.yellow.shade600,
padding: EdgeInsets.all(8),
child: Text(_lightIsOn ? 'TURN LIGHT OFF' : 'TURN LIGHT ON'),
),
),
],
),
),
);
}
}