Flutter如何引起一个新的手势

Flutter How to cause a new gesture

我有一个可滑动的列表视图 我想要一个长按手势来激活一个可滑动的动作。 我可以收到一条长按消息,但我不知道该怎么做才能使滑动动作起作用。 这是我需要编辑的自定义 Tile Widget。

这是我滑动时的样子。
我希望在释放长按时发生同样的事情

代码如下:

class BookmarkListTile extends StatelessWidget {
  // final BookmarkPageViewModel bookmarkViewmodel;
  // final int index;

  const BookmarkListTile(
      {Key? key, required this.bookmark, this.onTap, this.onDelete})
      : super(key: key);
  final Bookmark bookmark;
  final Function(Bookmark bookmark)? onDelete;
  final Function(Bookmark bookmark)? onTap;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onLongPressUp: () {
          print("longpress");
          // add code here to cause the Slidable Action to happen

        },
        child: Slidable(
          actionPane: const SlidableDrawerActionPane(),
          secondaryActions: [
            IconSlideAction(
              icon: Icons.delete,
              color: Colors.red,
              onTap: () {
                if (onDelete != null) onDelete!(bookmark);
              },
            )
          ],
          child: ListTile(
            onTap: () {
              if (onTap != null) onTap!(bookmark);
            },
            title: Text(bookmark.note),
            subtitle: Text(PaliScript.getScriptOf(
                language:
                    context.read<ScriptLanguageProvider>().currentLanguage,
                romanText: bookmark.bookName!)),
            trailing: SizedBox(
              width: 100,
              child: Row(
                children: [
                  Text('${AppLocalizations.of(context)!.page} -'),
                  Expanded(
                      child: Text(
                    '${bookmark.pageNumber}',
                    textAlign: TextAlign.end,
                  )),
                ],
              ),
            ),
          ),
        ));
  }
}

试试这个:

Widget build(BuildContext context) {
    return Slidable(
          actionPane: const SlidableDrawerActionPane(),
          secondaryActions: [
            IconSlideAction(
              icon: Icons.delete,
              color: Colors.red,
              onTap: () {
                if (onDelete != null) onDelete!(bookmark);
              },
            )
          ],
          child: Builder(builder: (context) =>
             GestureDetector(
        behavior: HitTestBehavior.translucent,
        onLongPress: () {
         openSlidable(context);
        },
       child: ListTile(
            onTap: () {
              if (onTap != null) onTap!(bookmark);
            },
            title: Text(bookmark.note),
            subtitle: Text(PaliScript.getScriptOf(
                language:
                    context.read<ScriptLanguageProvider>().currentLanguage,
                romanText: bookmark.bookName!)),
            trailing: SizedBox(
              width: 100,
              child: Row(
                children: [
                  Text('${AppLocalizations.of(context)!.page} -'),
                  Expanded(
                      child: Text(
                    '${bookmark.pageNumber}',
                    textAlign: TextAlign.end,
                  )),
                ],
              ),
          ),
        ))));
  }


 void openSlidable(BuildContext context) {
    final slidable = Slidable.of(context);
    final isClosed = slidable.renderingMode == SlidableRenderingMode.none;
    if (isClosed) {
      Future.delayed(Duration.zero, () {
        if (slidable.mounted) {
                    slidable.open(actionType: SlideActionType.secondary);

        }
      });
    }
  }

这将自动打开您的滑动件(如果它已关闭)。 Slidable 的 Child 应该包含在 Builder 中才能工作。