简体   繁体   中英

draw a widget on a custompaint with GestureDetector

i want to draw a line at center of the screen when i touch the screen but is not working. i dont know why is does not work but it can print "tapdown"

Code below:**

@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));
              },
            ),
          ]),
        );
      }

Code Create Line:

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() need to be at the end of func:

    GestureDetector(
      onTapDown: (e) {
        print("tapdown");
        CustomPaint(
            painter: ExampleLine(xaxis: size.width, yaxis: size.height));
        setState(() {});
      },
    ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM