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

gra*_*452 7 path custom-painting gesturedetector dart flutter

我对 flutter 非常陌生,正在尝试找出如何在 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;
}
Run Code Online (Sandbox Code Playgroud)

我用 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");
          },
        ),
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

我只想点击一个键并得到响应,最终会发出声音,但现在,我只是想弄清楚如何让 onTap 工作。

小智 13

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

\n\n
@override\nbool hitTest(Offset position) {\n    // _path - is the same one we built in paint() method;\n    return _path.contains(position);\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这将允许GestureDetector检测路径内的点击而不是整个CustomPaint容器。尽管_path必须关闭,因为至少据我所知,\xe2\x80\x94 无法测试曲线上的命中。

\n


Ser*_*nal 1

用手势检测器包裹油漆就可以了。

 GestureDetector(
    onTapDown: (details) {
         print("${details.globalPosition.dx}");
         print("${details.globalPosition.dy}");
     },
  ),
Run Code Online (Sandbox Code Playgroud)