Flutter 拖动示例

class DragWidget extends StatefulWidget {
  const DragWidget({Key? key, required this.title}) : super(key: key);

  final String title;
  @override
  State<DragWidget> createState() {
    return DragState();
  }
}

class DragState extends State<DragWidget> {
  double _top = 0.0; //距顶部的偏移
  double _left = 0.0; //距左边的偏移

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: Stack(
        children: <Widget>[
          Positioned(
            top: _top,
            left: _left,
            child: GestureDetector(
              child: const CircleAvatar(child: Text("A")),
              onPanDown: (DragDownDetails e) {
                print("手指按下:${e.globalPosition}");
              },
              onPanUpdate: (DragUpdateDetails e) {
                setState(() {
                  _left += e.delta.dx;
                  _top += e.delta.dy;
                });
              },
              onPanEnd: (DragEndDetails e){
                print(e.velocity);
              },
            ),
          ),
        ],
      ),
    );
  }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xiaopihair123/article/details/124950555