【Flutter】onTap点击延迟问题

2025-03-30  本文已影响0人  热爱技术的小卢同学

在使用bitsdojo_window库实现桌面端开发的过程中,遇到标题栏中按钮点击有延迟的问题,现记录一下相关情况和解决办法

排查过程:检查了按钮的父级代码,发现MoveWindow组件中的UI代码,存在一个GestureDetector,这个GestureDetector中存在一个双击回调,这也就是产生此次问题的罪魁祸首,代码如下

class MoveWindow extends StatelessWidget {
  final Widget? child;
  final VoidCallback? onDoubleTap;
  MoveWindow({Key? key, this.child, this.onDoubleTap}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    if (child == null) return _MoveWindow(onDoubleTap: this.onDoubleTap);
    return _MoveWindow(
      onDoubleTap: this.onDoubleTap,
      child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [Expanded(child: this.child!)]),
    );
  }
}

class _MoveWindow extends StatelessWidget {
  _MoveWindow({Key? key, this.child, this.onDoubleTap}) : super(key: key);
  final Widget? child;
  final VoidCallback? onDoubleTap;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onPanStart: (details) {
          appWindow.startDragging();
        },
        onDoubleTap: this.onDoubleTap ?? () => appWindow.maximizeOrRestore(),//需要去除onDoubleTap,用其他方式实现双击最大化
        child: this.child ?? Container());
  }
}

总结,可能存在的原因有:
1.手势中存在onDoubleTap
2.子组件没有加背景(网上其他人说的,还没验证)

上一篇 下一篇

猜你喜欢

热点阅读