如何增加onLongPress的时间限制?

Pra*_*nav 6 flutter

我对扑动相当陌生。我知道当用户触摸屏幕超过 500 毫秒时会触发长按。有没有办法将其更改为大约 2 秒?

在我的代码中,我使用 inkresponse 从 fab 获取两个不同的函数。点击时会转到第一页,长按时会转到第二页。我希望长按时间更长,并且可能添加动画来指示按钮被按下。

这是我写的代码-

floatingActionButtonLocation: 
  FloatingActionButtonLocation.endFloat,
      floatingActionButton: InkResponse(
        splashColor: Colors.tealAccent,
             onLongPress: () {
          Navigator.of(context).push(new MaterialPageRoute(
          builder: (BuildContext context) => new firstPage(),));
          },
        child: new Container(
          width: 57.0,
          height: 57.0,

          child: Align(
          alignment: Alignment(1, 1.05),
           child: FloatingActionButton(
            onPressed: () {
                Navigator.of(context).push(new MaterialPageRoute(
                builder: (BuildContext context) => new secondpage(),
              ));},
        ),),
      ),),
Run Code Online (Sandbox Code Playgroud)

Cop*_*oad 4

您可以使用onPanDownand onPanCancelofGestureDetector来实现该行为。为了简单起见,我删除了InkResponse. 您可以随意将其GestureDetector包裹起来InkWell

class _MyPageState extends State<MyPage> {
  Timer _timer;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: GestureDetector(
        onPanCancel: () => _timer?.cancel(),
        onPanDown: (_) => {
          _timer = Timer(Duration(seconds: 1), () {
            // Your function goes here
          })
        },
        child: FloatingActionButton(onPressed: () {}),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)