꿈꾸는 시스템 디자이너

Flutter Example - GestureDetector | InkWell 본문

Tutorial/Flutter with App

Flutter Example - GestureDetector | InkWell

독행소년 2019. 9. 30. 14:12
import 'package:flutter/material.dart';

GestureDetectorDemoState pageState;

class GestureDetectorDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    pageState = GestureDetectorDemoState();
    return pageState;
  }
}

class GestureDetectorDemoState extends State<GestureDetectorDemo> {
  final scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: scaffoldKey,
      appBar: AppBar(title: Text("GestureDetector & InkWell")),
      body: ListView(
        children: <Widget>[
          myTitle("InkWell"),
          InkWell(
            child: Container(
              height: 50,
              color: Colors.orange,
              alignment: Alignment(0, 0),
              margin: const EdgeInsets.all(10),
              child: Text(
                "Container with Text in InkWell\nTap me!!",
                textAlign: TextAlign.center,
                style: TextStyle(color: Colors.white),
              ),
            ),
            onTap: () {
              showSnackBar("InkWell - onTap");
            },
            onDoubleTap: () {
              showSnackBar("InkWell - onDoubleTap");
            },
            onTapDown: (value) {
              showSnackBar("InkWell - onTapDown");
            },
            onTapCancel: () {
              showSnackBar("InkWell - onTapCancel");
            },
            onLongPress: () {
              showSnackBar("InkWell - onLongPress");
            },
          ),
          myTitle("Gesture Detector"),
          GestureDetector(
            child: Container(
              height: 50,
              color: Colors.blue,
              alignment: Alignment(0, 0),
              margin: const EdgeInsets.all(10),
              child: Text(
                "Container with Text in GestureDetector\nTap me!!",
                textAlign: TextAlign.center,
                style: TextStyle(color: Colors.white),
              ),
            ),
            onTap: () {
              showSnackBar("GestureDetector - onTap");
            },
            onDoubleTap: () {
              showSnackBar("GestureDetector - onDoubleTap");
            },
            onTapDown: (value) {
              showSnackBar("GestureDetector - onTapDown");
            },
            onTapCancel: () {
              showSnackBar("GestureDetector - onTapCancel");
            },
            onLongPress: () {
              showSnackBar("GestureDetector - onLongPress");
            },
          )
        ],
      ),
    );
  }

  myTitle(String title) {
    return Column(
      children: <Widget>[
        Container(
          alignment: Alignment(-1, 0),
          padding: const EdgeInsets.only(left: 10, top: 10, bottom: 5),
          child: Text(
            "* $title",
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
        ),
        Divider(
          color: Colors.grey,
          height: 0,
          indent: 10,
          endIndent: 10,
        )
      ],
    );
  }

  showSnackBar(String message) {
    scaffoldKey.currentState
      ..hideCurrentSnackBar()
      ..showSnackBar(
        SnackBar(
          content: Text(message),
          backgroundColor: Colors.blue,
          action: SnackBarAction(
            label: "Done",
            textColor: Colors.white,
            onPressed: () {},
          ),
        ),
      );
  }
}

 

 

▶ Go to Table of Contents | 강의 목차로 이동


※ This example is also available in the Flutter Code Examples app. | 본 예제는 Flutter Code Examples 앱에서도 제공됩니다.

 

Flutter Code Examples - Google Play 앱

Are you a beginner at Flutter? Check out the various features of Flutter through the demo. Source code for all demos is also provided.

play.google.com

Comments