Flutter-手势识别GestureDetector

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('监听'),
        ),
        body: GestureDetectorTestRoute(),
      ),
    );
  }
}

class GestureDetectorTestRoute extends StatefulWidget {
  @override
  _GestureDetectorTestRouteState createState() => _GestureDetectorTestRouteState();
}

class _GestureDetectorTestRouteState extends State {
  String _operation = "no gesture detected!";

  @override
  Widget build(BuildContext context) {
    return Center(
      child: GestureDetector(
        onTap: () => updateText("tap"),
        onDoubleTap: () => updateText("doubletap"),
        onLongPress: () => updateText("longpress"),
        child: Container(
          alignment: Alignment.center,
          color: Colors.blue,
          width: 200,
          height: 100,
          child: Text(_operation, style: TextStyle(color: Colors.white),),
        ),
      ),
    );
  }

  void updateText(String text) {
    setState(() {
      _operation = text;
    });
  }

}

你可能感兴趣的:(Flutter-手势识别GestureDetector)