본문 바로가기
프로그래밍/Flutter-Dart

[Flutter] GestureDetector

by 채연2 2021. 3. 24.

 

 

 

 

 

최종 코드

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _GestureDetectorExample createState() => _GestureDetectorExample();
}

class _GestureDetectorExample extends State<MyApp> {
  late String msg;
  late String direction;

  double _scaleFactor = 1.0;
  double _baseScaleFactor = 1.0;

  @override
  void initState() {
    msg = 'gesture';
    direction = '';
  }

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Test',
      home: Center(
        child: GestureDetector(
          onTap: () {
            setState(() {
              msg = 'onTap';
            });
          },
          onLongPress: () {
            setState(() {
              msg = 'onLongPress';
            });
          },
          onDoubleTap: () {
            setState(() {
              msg = 'onDoubleTap';
            });
          },
          onScaleStart: (details) {
            _baseScaleFactor = _scaleFactor;
          },
          onScaleUpdate: (details) {
            setState(() {
              _scaleFactor = _baseScaleFactor * details.scale;
            });
          },
          child: Expanded(
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                Container(
                  width: 500,
                  height: 300,
                  color: Colors.orange,
                ),
                Text(
                  msg,
                  style: TextStyle(color: Colors.amber, fontSize: 30),
                  textScaleFactor: _scaleFactor,
                ),
                GestureDetector(
                  onVerticalDragEnd: (DragEndDetails details) {
                    setState(() {
                      String updown = details.velocity.pixelsPerSecond.dy < 0
                          ? 'up'
                          : 'down';
                      direction = updown;
                    });
                  },
                  onHorizontalDragEnd: (DragEndDetails details) {
                    setState(() {
                      String updown = details.velocity.pixelsPerSecond.dx < 0
                          ? 'left'
                          : 'right';
                      direction = updown;
                    });
                  },
                  child: Expanded(
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                        Container(
                          width: 300,
                          height: 300,
                          color: Colors.green,
                        ),
                        Text(
                          direction,
                          style: TextStyle(color: Colors.blue, fontSize: 20),
                        ),
                      ])),
                )
              ])),
        ),
      ),
    );
  }
}

 

 

 

결과

 

 

 

 

 

320x100

'프로그래밍 > Flutter-Dart' 카테고리의 다른 글

[Flutter] get package (2)  (0) 2021.03.25
[Flutter] get package (1)  (0) 2021.03.24
[Flutter] Bluetooth  (0) 2021.03.24
[Flutter] Provider (2)  (0) 2021.03.24
[Flutter] Provider (1)  (0) 2021.03.24

댓글