首页 > 解决方案 > Dart 中带有 google_maps_flutter 的 GestureDetector

问题描述

我正在使用google_maps_flutter并希望在用户在地图上执行手势时执行操作,无论是缩放/倾斜/移动/旋转。但是我无法在GoogleMap 类中使用onCameraMoveStarted属性,因为它还可以识别非手势用户操作以及编程动画(我的应用程序使用),没有办法(据我所知,请更正我) , 来区分它们。

因此,我想到了使用颤振小部件GestureDetector,将地图包裹在其中,这样我就可以根据 GestureDetector 检测到的手势更改变量,从而间接导致地图发生变化。

最初没有问题,它充当透明层,并且可以正常移动/倾斜/旋转/缩放地图。但是,在添加了通过 onPanStart、onPanUpdate 或 onPanEnd 执行的函数后,都会使地图无法通过手势进行交互。我想这一切都被 GestureDetector 捕获了,但是我有没有办法在将手势传递给孩子的同时异步执行额外的任务?

这是结构,顺便说一句:

 build(context) {
    return Scaffold(
      body: GestureDetector(
        behavior: HitTestBehavior.deferToChild,
        onPanStart: {...}
        child:
          GoogleMap(...),
      ),
      ...
    );
  }

在此先感谢,非常感谢任何帮助。

标签: google-mapsflutterdartuigesturerecognizer

解决方案


我找到了一个可能对你有用的解决方案。

class Test extends DragGestureRecognizer {
  Function _test;

  Test(this._test);

  @override
  void resolve(GestureDisposition disposition) {
    super.resolve(disposition);
    this._test();
  }
}

...

return GoogleMap(
        ...
          gestureRecognizers: Set()
            ..add(Factory<DragGestureRecognizer>(() => Test(() {
               if (_focusEnabled) {
                 setState(() {
                   _focusEnabled = false;
                 });
               }
            })),
        );

这会在与地图的每次交互中运行您的函数。但我没有找到区分事件的方法。


推荐阅读