如何使用GestureDetect在Flutter中点击CustomPaint路径?

时间:2019-07-18 17:59:28

标签: flutter dart path gesturedetector custom-painting

我是一个非常不熟悉的人,正在尝试弄清楚如何在CustomPaint路径上检测到手势。由于某些原因,我可以单击许多其他内容,但不能单击路径...如何使它起作用?到目前为止,我的代码如下。

import 'package:flutter/material.dart';

void main() => runApp(MbiraShapes());

class MbiraShapes extends StatefulWidget {
    @override
  _MbiraShapesState createState() => _MbiraShapesState();
}

class _MbiraShapesState extends State<MbiraShapes>{

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Mbira Shapes',
        home: PathExample());
  }
}

class PathExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return CustomPaint(
      painter: KeyPathPainter(),
    );
  }
}

class KeyPathPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint lineDrawer = Paint()
      ..color = Colors.blue
      ..strokeWidth = 8.0;

    Path path = Path()
// Moves to starting point
      ..moveTo(50, 50)
      //draw lines passing through xc, yc to end at x,y
      ..quadraticBezierTo(77, 370, 50, 750)
      ..quadraticBezierTo(100, 775, 150, 750)
      ..quadraticBezierTo(110, 440, 75, 50);
    //close shape from last point
    path.close();
    canvas.drawPath(path, lineDrawer);

    Path path2 = Path()
// Moves to starting point
      ..moveTo(250, 50)
      //draw lines passing through xc, yc to end at x,y
      ..quadraticBezierTo(280, 350, 270, 675)
      ..quadraticBezierTo(290, 750, 350, 750)
      ..quadraticBezierTo(365, 710, 345, 600)
      ..quadraticBezierTo(320, 450, 270, 50);
    //close shape from last point
    path2.close();
    canvas.drawPath(path2, lineDrawer);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => true;
}

我使用GestureDetector尝试了以下代码段,但它不起作用。 我尝试了一个侦听器,但对onPointerDown没有响应,仅对onPointerMove没有响应,但是我需要轻按动作,而不需要移动动作。

@override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
  
        title: Text("widget.title)"),
      ),
      
      body: Center(
        child: GestureDetector(
          child: CustomPaint(
            painter: KeyPathPainter(),
            child: Container()),
          onTap: () {
            print("tapped the key");
          },
        ),
      ),
    );
  }

我只想轻按一个键并获得响应,最后会发出声音,但现在,我只是想弄清楚如何使onTap正常工作。

3 个答案:

答案 0 :(得分:1)

用手势检测器包裹油漆即可完成工作。

 GestureDetector(
    onTapDown: (details) {
         print("${details.globalPosition.dx}");
         print("${details.globalPosition.dy}");
     },
  ),

答案 1 :(得分:1)

您必须重写hitTest()类的KeyPathPainter方法:

@override
bool hitTest(Offset position) {
    // _path - is the same one we built in paint() method;
    return _path.contains(position);
}

这将使GestureDetector可以检测路径内部而不是整个CustomPaint容器中的点击。尽管_path必须关闭,因为(至少据我所知)无法测试曲线的点击率。

答案 2 :(得分:0)

我是通过谷歌搜索我遇到的一个问题而来的。我的问题是我只是将Painter包装在一个手势检测器中,例如:

GestureDetector(
      child: CustomPaint(
        painter: CustomPainter(),
      ),

      onTapDown: _handleTapDown,
);

在哪里需要解决方案的地方,我需要有一个Container的颜色,包裹着Custom Painter,就像这样:

return GestureDetector(
      child: Container(
        width: 300,
        height: 300,
        color: Colors.white,
        child: CustomPaint(
          painter: CustomPainter(),
        ),
      ),

      onTapDown: _handleTapDown,
);