flutter 嵌套的GestureDetector OnTap函数

mjqavswn  于 2023-05-23  发布在  Flutter
关注(0)|答案(3)|浏览(180)

我已经嵌套了GestureDetetor,但问题是只有子GestureDetectoronTap被执行。我不希望覆盖子进程的onTap,而是希望父进程和子进程的onTap都能执行。下面是我的代码:

GestureDetector(
        onTap: () {
            todo1(); 
        },
        child: GestureDetector(
            onTap: () {
                todo2();
            },
            child: Text("Nested Gesture")))

如何将其更改为同时调用todo1()todo2() onTap?
编辑:子控件是一个可重用的自定义小部件,它有自己的实现,但现在正被父控件使用,父控件除了子控件外,还拥有自己的实现

ztyzrc3y

ztyzrc3y1#

我做了一个快速自定义手势识别器-它取消手势只有当用户已经移动太远,从初始点击点。
使用示例:

UniversalTapHandler(
  onTap: () {
    print("Tap 1");
  },
  child: UniversalTapHandler(
    onTap: () {
      print("Tap 2");
    },
    child: Text("Nested Gesture"),
  )
)

源代码:

class UniversalTapHandler extends RawGestureDetector {
  UniversalTapHandler({
    @required GestureTapCallback onTap,
    @required Widget child,
  }):
    super(
      gestures: <Type, GestureRecognizerFactory>{
        _UniversalPointerHandler: GestureRecognizerFactoryWithHandlers<_UniversalPointerHandler>(
          () => _UniversalPointerHandler(onTap: onTap),
          (_) {},
        ),
      },
      child: child,
    );
}

class _UniversalPointerHandler extends OneSequenceGestureRecognizer {
  _UniversalPointerHandler({
    @required this.onTap,
  }): super();

  final GestureTapCallback onTap;

  final _maxDistance = 18; // as in official recognizer by default
  Offset _startPosition;

  void _reset() {
    _startPosition = null;
  }

  @override
  void addPointer(PointerDownEvent event) {
    _startPosition = event.position;
    startTrackingPointer(event.pointer);
    resolve(GestureDisposition.accepted);
  }

  @override
  void handleEvent(PointerEvent event) {
    if (event is PointerUpEvent) {
      stopTrackingPointer(event.pointer);
      if (_startPosition != null) {
        onTap();
      }
    }
    if (event is PointerMoveEvent && _startPosition != null) {
      if ((event.position - _startPosition).distance > _maxDistance) {
        rejectGesture(event.pointer);
        _reset();
      }
    }
    if (event is PointerCancelEvent || event is PointerExitEvent || event is PointerRemovedEvent) {
      _reset();
    }
  }

  @override
  void resolve(GestureDisposition disposition) {
    if (disposition == GestureDisposition.rejected) {
      _reset();
    }
    super.resolve(disposition);
  }

  @override
  void didStopTrackingLastPointer(int pointer) {}

  @override
  String get debugDescription => "_UniversalPointerHandler: Custom Gesture Recognizer";
}

更新别忘了导入:

import 'package:flutter/gestures.dart';
e4eetjau

e4eetjau2#

你可以在嵌套的onTap回调中调用todo1():

GestureDetector(
        onTap: () {
            todo1(); 
        },
        child: GestureDetector(
            onTap: () {
                todo2();
                todo1();
            },
            child: Text("Nested Gesture")))
g6baxovj

g6baxovj3#

如果你想同时执行父对象和子对象的onTap方法,你可以使用下面的代码片段:

GestureDetector(
  onTap: () {
    // parent onTap method
  },
  child: GestureDetector(
    onTap: () {
      // child onTap method
    },
    behavior: HitTestBehavior.translucent,
    child: Container(),
  ),
);

behavior属性设置为HitTestBehavior.translucent,这允许父部件和子部件都接收点击。

相关问题