onDoubleTap 会降低 GestureDetector 的性能而不生成事件
预期结果:
如果不双击,我预计性能不会有差异。
实际结果:
我添加了双击事件,并注意到仅通过添加它,应用程序的性能就会大大降低。 另一方面,如果我评论该行,那么性能将恢复稳定。
如果我造成了任何性能问题,双击就会出现。 但只要加上性能急剧下降的事件线。
代码
void _startOperation(BuildContext ctx) {
_timer = Timer(const Duration(milliseconds: 150), () {
isLongPressed = true;
if (!widget.cell.isShowed) {
Provider.of<GameModelProvider>(ctx, listen: false).setFlag(widget.cell);
HapticFeedback.mediumImpact();
}
});
}
Widget build(BuildContext context) {
return GestureDetector(
onDoubleTap: () => Provider.of<GameModelProvider>(context, listen: false)
.speedOpenCell(widget.cell),
onTapDown: (_) {
_startOperation(context);
},
onTapUp: (_) {
if (!isLongPressed) {
Provider.of<GameModelProvider>(context, listen: false)
.computeCell(widget.cell);
} else {
isLongPressed = false;
}
_timer.cancel();
},
child: Container(
width: widget.cellWidth,
height: widget.cellHeight,
decoration: BoxDecoration(
color: widget.cell.isShowed
? Colors.grey.shade300
: Colors.grey.shade400,
borderRadius: BorderRadius.circular(5.0),
),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: Center(
child: getContent(),
),
),
),
);
}
您可以在此处获取所有代码:https://github.com/EliaTolin/infinity_sweeper< /a>
Expected results:
I don't expect a difference in performance if I don't double tap.
Actual results:
I added the double tap event and noticed that by simply adding it, the performance of the application is greatly diminished.
If, on the other hand, I comment the line, then the performance returns stable.
If I had created any performance problems I would expect them if I double tap.
But just add the event line that the performances drop drastically.
Code
void _startOperation(BuildContext ctx) {
_timer = Timer(const Duration(milliseconds: 150), () {
isLongPressed = true;
if (!widget.cell.isShowed) {
Provider.of<GameModelProvider>(ctx, listen: false).setFlag(widget.cell);
HapticFeedback.mediumImpact();
}
});
}
Widget build(BuildContext context) {
return GestureDetector(
onDoubleTap: () => Provider.of<GameModelProvider>(context, listen: false)
.speedOpenCell(widget.cell),
onTapDown: (_) {
_startOperation(context);
},
onTapUp: (_) {
if (!isLongPressed) {
Provider.of<GameModelProvider>(context, listen: false)
.computeCell(widget.cell);
} else {
isLongPressed = false;
}
_timer.cancel();
},
child: Container(
width: widget.cellWidth,
height: widget.cellHeight,
decoration: BoxDecoration(
color: widget.cell.isShowed
? Colors.grey.shade300
: Colors.grey.shade400,
borderRadius: BorderRadius.circular(5.0),
),
child: Padding(
padding: const EdgeInsets.all(3.0),
child: Center(
child: getContent(),
),
),
),
);
}
You get all code here : https://github.com/EliaTolin/infinity_sweeper
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是说性能发生了变化,只是存在延迟,因为 flutter 框架现在必须等待(300 毫秒)才能确定手势实际上是双击还是单击。
It’s not that the performance has changed there is just latency because the flutter framework now has to wait (300ms) to determine if the gesture is actually a double tap versus a single tap.