I have this UI: enter image description here When user taps on right side we go to the next story, left we go to previous and holding down anywhere should stop the automatic loading on top. I am currently using GestureDetectors and using the three callbacks

  • onTap转到下一篇或上一篇文章
  • 如果为onTapDown,则暂停加载程序
  • onTapUp以恢复加载器

问题是,onTap次呼叫onTapUp次,所以当我试图暂停时,例如通过按住右侧,它会在我举起手指后立即转到下一个故事.

推荐答案

Let's try the below solution, I tested it and it works quite well in your case.

下面是一个自定义的GestureDetector小部件,它允许我们设置onLongPressStartonLongPressEnd的持续时间

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

class CustomGestureLongPress extends StatelessWidget {
  final Widget? child;
  final Duration duration;
  final GestureLongPressStartCallback onLongPressStart;
  final GestureLongPressEndCallback onLongPressEnd;
  final VoidCallback? onLongPress;

  const CustomGestureLongPress({
    super.key,
    this.child,
    required this.duration,
    required this.onLongPressStart,
    required this.onLongPressEnd,
    this.onLongPress,
  });

  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
      behavior: HitTestBehavior.translucent,
      gestures: <Type, GestureRecognizerFactory>{
        LongPressGestureRecognizer: GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
              () => LongPressGestureRecognizer(duration: duration), (instance) {
                instance.onLongPress = () {
                  print("Parent onLongPress");
                  onLongPress?.call();
                };
                instance.onLongPressStart = (details) {
                  print("Parent onLongPressStart");
                  onLongPressStart.call(details);
                };
                instance.onLongPressEnd = (details) {
                  print("Parent onLongPressEnd");
                  onLongPressEnd.call(details);
                };
              },
        ),
      },
      child: child,
    );
  }
}`

以及自定义小部件的使用

CustomGestureLongPress(
            duration: const Duration(milliseconds: 200),
            onLongPressStart: (_) {
              //TODO Pause the loader
            },
            onLongPressEnd: (_) {
              //TODO Resume the loader
            },
            child: Stack(
                children: [
                  GestureDetector(
                    onTap: () {
                      //TODO Go to previous story
                    },
                    child: Align(
                      alignment: Alignment.centerLeft,
                      child: Container(
                        width: 50.w,
                        color: Colors.transparent,
                      ),
                    )
                  ),
                  GestureDetector(
                    onTap: () {
                      //TODO Go to next story
                    },
                    child: Align(
                      alignment: Alignment.centerRight,
                      child: Container(
                        width: 50.w,
                        color: Colors.transparent,
                      ),
                    )
                  )
                ]
            ),
          )

Flutter相关问答推荐

在Flutter中创建具有固定行的压缩水平列表

如何在Ffltter 3.16中设置标准的提升按钮主题?

在VSCode中运行应用程序而不进行调试时,控制台仍显示为调试模式

如何从文本比例因子迁移到文本比例因子

火焰Flutter 在多个向量之间插补2

StateNotifer正在riverpod中提供初始状态值(bool)

在 Flutter 中使用条件语句设置 ImageProvider Object 类型的背景图像

Flutter Riverpod 没有捕获 List 的嵌套值变化

点击后,将按钮的 colored颜色 禁用/更改一段时间

如何在Flutter中显示ListView Builder区域外的列表数据?

我怎样才能消除我的 Column 子元素之间的差距?

当我将主题更改为深色然后返回浅色模式时,Getx 会Flutter 更改主题,它不会加载我的自定义主题

为什么轮播加载图片很慢?

Flutter Websockets MacOS:相同的代码在调试模式下有效,但在发布模式下无效:(操作系统错误:提供了 node 名或服务名..)

如何验证非表单字段?

Flutter RawMaterialButton 常量相对大小

在 Flutter 中,您什么时候应该更喜欢Widget继承而不是组合?

将一个 Flutter 应用程序集成到另一个 Flutter 应用程序中

如何在 flutter 中更改日期 Select 器 colored颜色 ?

是否可以在 Flutter 中使用 ToggleButtons 在页面 PageView 之间切换?