/*红心*/
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;
}
}
Flutter 手势点击,
最新推荐文章于 2022-08-03 10:36:20 发布