Flutter 手势点击,

/*红心*/
import 'package:flutter/material.dart';

class GestureRecognizerWork extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      theme: ThemeData.light(),
      home: Container(
          color: Colors.white,
          child: new Scaffold(
            body: HomeTest(),
          )),
    );
  }
}

class HomeTest extends StatefulWidget {
  @override
  _HomeTestState createState() => new _HomeTestState();
}

class _HomeTestState extends State<HomeTest> {
  List<Offset> gestureDown = new List();

  double _left = 0;

  double _top = 0;


  _add(Offset position) {
    setState(() {
      gestureDown.add(position);
    });
  }

  List<Widget> _getList() {
    List<Positioned> list = new List();
    for (int i = 0; i < gestureDown.length; i++) {
      var down = gestureDown[i];
      list.add(Positioned(
          top: down.dy - 90,
          left: down.dx - 40,
          child: Container(color: Color(0xffff0000), child:
          CustomPaint(painter: MyHeart(),)
            ,)));
    }
    return list;
  }


  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(title: Text("gestureRecognize"),),
      body: Stack(
        children: <Widget>[
          GestureDetector(
            onPanDown: (DragDownDetails details) {
              RenderBox box = context.findRenderObject();
              Offset offset = box.globalToLocal(details.globalPosition);
              _add(offset);
              print("offset--${offset.toString()}");
            },
            onPanUpdate: (DragUpdateDetails details) {
//              RenderBox box = context.findRenderObject();
//              Offset offset = box.globalToLocal(details.globalPosition);
//              setState(() {
//                _left = offset.dx;
//                _top = offset.dy;
//              });
            },
          ),
          Stack(children: _getList(),),

          Positioned(
            top:_top,
            left:_left,

            child: FloatingActionButton(
                onPressed: () {
                  setState(() {
                    gestureDown.clear();
                  });
                }),
          )

        ],

      ),
    );
  }
}

class MyHeart extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint pa = new Paint()
      ..color = Colors.red
      ..strokeWidth = 10
      ..isAntiAlias = true //是否抗锯齿
      ..style = PaintingStyle.fill //画笔样式:填充
        ;


    Path path = new Path();
    var width = 50;
    var height = 80;
    path.moveTo(width / 2, height / 4);
    path.cubicTo((width * 6) / 7, height / 9, (width * 13) / 13,
        (height * 2) / 5, width / 2, (height * 7) / 12);
    canvas.drawPath(path, pa);

    Path path2 = new Path();
    path2.moveTo(width / 2, height / 4);
    path2.cubicTo(width / 7, height / 9, width / 21, (height * 2) / 5,
        width / 2, (height * 7) / 12);
    canvas.drawPath(path2, pa);
  }

  //flutter刷新后是否重新绘制
  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值