检查 Flutter 中按钮按下了多长时间

Abe*_*ang 2 android mobile-application flutter

实际上,我目前正在使用 Division 来处理按钮的手势。但我仍在寻找一种有或没有除法的方法。我希望长按按钮在 1000 毫秒而不是 500 毫秒后启动。

我在 SO 和其他网站上进行了搜索,但似乎仍然找不到这个确切问题的解决方案。

这就是它与 Division 的合作方式。我尝试使用 while 和 Future.delayed 但似乎无法使其正常工作。但如果不在 Division 的 Gesture() 中也完全没问题。因为我只是想让它正常工作,无论它是否使用除法。

Gestures()
          ..isTap((isTapped) => setState(() => pressed = isTapped))
          ..onLongPress(() { print("long pressed"); })
Run Code Online (Sandbox Code Playgroud)

先感谢您。

Jig*_*tel 7

您可以执行此操作的一种方法是使用Timerfromdart:async

class _MyWidgetState extends State<MyWidget> {
  
  Timer timer;
  
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (_) {
        timer = Timer(Duration(milliseconds: 1000), onLongerPress);
      },
      onTapUp: (_){
        timer.cancel();
      },
      child: Container(child: Center(child: Text('Click here')), width: 200, height: 50, color: Colors.green),
    );
  }
  
  void onLongerPress() {
    print('onLongerPress called');
  }
  
}
Run Code Online (Sandbox Code Playgroud)