Flutter检测用户手势与处理事件

114 阅读1分钟

GestureDetector

import 'package:flutter/material.dart';
​
class GesturePage extends StatefulWidget {
  const GesturePage({Key? key}) : super(key: key);
​
  @override
  State<GesturePage> createState() => _GesturePageState();
}
​
class _GesturePageState extends State<GesturePage> {
  String resMsg = '';
  double moveX = 0, moveY = 0;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '手势与事件',
      theme: ThemeData(
          primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('手势与事件'),
          leading: GestureDetector(
            onTap: (){
              Navigator.pop(context);
            },
            child: Icon(Icons.arrow_back),
          ),
        ),
        body: Stack(
          children: <Widget>[
            Column(
              children: <Widget>[
                GestureDetector(
                  onTap: () => _printMsg('点击'),
                  onDoubleTap: () => _printMsg('双击'),
                  onLongPress: () => _printMsg('长按'),
                  onTapCancel: () => _printMsg('取消'),
                  onTapUp: (e) => _printMsg('松开'),
                  onTapDown: (e) => _printMsg('按下'),
                  child: Container(
                    padding: EdgeInsets.all(60),
                    decoration: BoxDecoration(color: Colors.blueAccent),
                    child: Text('点我', style: TextStyle(fontSize: 36, color: Colors.white),),
                  ),
                ),
                Text(resMsg)
              ],
            ),
            Positioned(
              left: moveX,
                top: moveY,
                child: GestureDetector(
                  onPanUpdate: (e){
                    _domove(e);
                  },
                  child: Container(
                    width: 72,
                    height: 72,
                    decoration: BoxDecoration(color: Colors.amber, borderRadius: BorderRadius.circular(36)),
                  ),
                )
            )
          ],
        ),
      )
    );
  }
​
  _printMsg(String msg) {
    setState(() {
      resMsg += ' ,$msg';
    });
  }
​
  void _domove(DragUpdateDetails e) {
    setState(() {
      moveY += e.delta.dy;
      moveX += e.delta.dx;
    });
  }
}
​

\