![](/img/trans.png)
[英]Flutter GestureDetector with CustomPaint not working
[英]draw a widget on a custompaint with GestureDetector
当我触摸屏幕但不工作时,我想在屏幕中心画一条线。 我不知道为什么不起作用,但它可以打印“tapdown”
以下代码:**
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
body: Stack(children: [
Positioned(
left: size.width / 2,
top: size.height / 2,
child: CustomPaint(
painter: Over(),
),
),
GestureDetector(
onTapDown: (e) {
print("tapdown");
setState(() {});
CustomPaint(
painter: ExampleLine(xaxis: size.width, yaxis: size.height));
},
),
]),
);
}
代码创建行:
class ExampleLine extends CustomPainter {
double xaxis, yaxis;
ExampleLine({required this.xaxis, required this.yaxis});
@override
void paint(Canvas canvas, Size size) {
Paint line = Paint()
..color = Colors.red
..style = PaintingStyle.stroke
..strokeWidth = 1;
Path pline = Path();
pline.moveTo(xaxis / 2, yaxis / 2);
pline.lineTo(0, 0);
canvas.drawPath(pline, line);
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false;
}
}
setState() 需要在 func 的末尾:
GestureDetector(
onTapDown: (e) {
print("tapdown");
CustomPaint(
painter: ExampleLine(xaxis: size.width, yaxis: size.height));
setState(() {});
},
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.