android - 检查在 Flutter 中按下按钮多长时间

标签 android flutter mobile-application

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

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

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

Gestures()
          ..isTap((isTapped) => setState(() => pressed = isTapped))
          ..onLongPress(() { print("long pressed"); })

提前谢谢你。

最佳答案

您可以使用 dart:async

中的 Timer 来做到这一点
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');
  }
  
}

关于android - 检查在 Flutter 中按下按钮多长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63445281/

相关文章:

flutter - 如何在GridView中每6个不同的项目渲染一个图像?

flutter - 项目构建器在futurbuilder Flutter中没有被触发

php - 实现移动应用程序 Web 服务的标准方式

javascript - 即使我不从外部链接调用,React-native 链接 getinitialurl() 也能正常工作

java - SharedPreferences 无法正常工作

android - 测试装备移动设备

android - Dart OAuth 1.0 - 无效签名 - 提供的签名不匹配

android - 阻止所有关闭安卓应用程序的方式

android - 切换选项卡时重置 fragment 后退堆栈

java - AsyncTask 卡住其线程和 UI 线程