android - Flutter - 多个手势,无需抬起手指

标签 android ios dart flutter gesture

我正在尝试创建以下效果:当用户在空白屏幕上长按时,会出现一个矩形。在不抬起手指的情况下,我希望用户能够拖动矩形的边缘之一(例如,垂直)。

我能够分别实现这些效果(长按、释放、拖动),但我需要在不抬起手指的情况下拥有它们。

目前,我的代码如下所示:

 @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onPanStart: startDrag,
      onPanUpdate: onDrag,
      onPanEnd: endDrag,
      child: CustomPaint(
        painter: BoxPainter(
          color: BOX_COLOR,
          boxPosition: boxPosition,
          boxPositionOnStart: boxPositionOnStart ?? boxPosition,
          touchPoint: point,
        ),
        child: Container(),
      ),
    );
  }

这个实现了边缘的拖动,基于this tutorial .

为了使元素在长按时出现,我使用了 Opacity 小部件。

@override
  Widget build(BuildContext context) {
    return new GestureDetector(
      onLongPress: () {
        setState(() {
          this.opacity = 1.0;
        });
      },
      child: new Container(
        width: width,
        height: height,
        child: new Opacity(
          opacity: opacity,
          child: PhysicsBox(
            boxPosition: 0.5,
          ),
        ),
      ),
    );
  }

最佳答案

如果有人仍然感兴趣,我可以使用 DelayedMultiDragGestureRecognizer 类实现所需的行为。

代码如下所示:

  void onDrag(details) {
    // Called on drag update
  }

  void onEndDrag(details) {
    // Called on drag end
  }

  @override
  Widget build(BuildContext context) {
    return new RawGestureDetector(
      gestures: <Type, GestureRecognizerFactory>{
        DelayedMultiDragGestureRecognizer:
            new GestureRecognizerFactoryWithHandlers<
                DelayedMultiDragGestureRecognizer>(
          () => new DelayedMultiDragGestureRecognizer(),
          (DelayedMultiDragGestureRecognizer instance) {
            instance
              ..onStart = (Offset offset) {
                /* More code here */
                return new ItemDrag(onDrag, endDrag);
              };
          },
        ),
      },
    );
  }

ItemDrag 是 Fl​​utter Drag 类的扩展类:

class ItemDrag extends Drag {
  final GestureDragUpdateCallback onUpdate;
  final GestureDragEndCallback onEnd;

  ItemDrag(this.onUpdate, this.onEnd);

  @override
  void update(DragUpdateDetails details) {
    super.update(details);
    onUpdate(details);
  }

  @override
  void end(DragEndDetails details) {
    super.end(details);
    onEnd(details);
  }
}

关于android - Flutter - 多个手势,无需抬起手指,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824208/

相关文章:

android - Lollipop : Had trouble showing a shadow under the overflow menu

android - 可见时刷新 fragment 中的适配器

ios - 快速使用选择器不起作用

flutter - 在ListView.builder() flutter中动态创建单选按钮Group

android - 屏蔽通知栏

android - RippleDrawable 不能用作 ViewGroup 的可绘制对象

ios - 在哪里存储创建的PDF文件?

ios - iOS什么时候移除不活跃的约束

postgresql - 从 Aqueduct 应用程序获取上次修改日期时间的最轻量级方法是什么?

plugins - device_calendar 0.1.0 插件,用于日历访问的 flutter 限制权限