Flutter之手势监听

常用的三个手势监听

 OnTap:点击

OnDoubleTap:双击

OnLongPress:长按

import 'package:flutter/material.dart';

class Getsturepage extends StatefulWidget {
  @override
  _GetsturepageState createState() => _GetsturepageState();
}

class _GetsturepageState extends State<Getsturepage> {
  String pinfo = '';
  double movex = 0.0, movey = 0.0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('如何检测用户手势和监听事件'),
          leading: GestureDetector(
            onTap: () {
              Navigator.pop(context);
            },
            child: Icon(Icons.arrow_back),
          ),
        ),
        body: FractionallySizedBox(
          widthFactor: 1,
          child: Stack(
            children: <Widget>[
              Column(children: <Widget>[
                GestureDetector(//手势监听器
                  onTap: () => _printinfo('点击'),
                  onDoubleTap: () => _printinfo('双击'),
                  onLongPress: () => _printinfo('长按'),//根据字面意思都挺好理解的
                  child: Container(
                    padding: EdgeInsets.all(60),
                    decoration: BoxDecoration(color: Colors.blueAccent),
                    child: Text(
                      '点我',
                      style: TextStyle(fontSize: 36, color: Colors.white),
                    ),
                  ),
                ),
                Text(pinfo),
              ]),
              Positioned(
                //跟着手指滑动的小球
                left: movex,
                top: movey,
                child: GestureDetector(
                  onPanUpdate: (e) => _doMove(e),
                  child: Container(
                    width: 70.0,
                    height: 70.0,
                    decoration: BoxDecoration(
                      color: Colors.amber,
                      borderRadius: BorderRadius.circular(35.0),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  _printinfo(String info) {
    setState(() {
      pinfo += ', $info';
    });
  }

  _doMove(DragUpdateDetails e) {
    setState(() {
     movex +=e.delta.dx;
     movey +=e.delta.dy; //像极了微分
    });
    print(e);
  }
}
发布了99 篇原创文章 · 获赞 21 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43721423/article/details/99610448