Taba
Taba

Reputation: 4336

How to set the duration of onLongPress

I know onLongPress would trigger after a certain period of time (like 500 ms or so). But what I want to do is to trigger some action when user presses the button for like 3 seconds. Actually I want to set the duration for onLongPress.

ElevatedButton(
  onPressed: () => print('ok I\'m just fine'),
  onLongPress: () => print('Trigger me when user presses me for like 3 seconds'),
  style: ElevatedButton.styleFrom(
  primary: Colors.red,
  elevation: 4,
),

Upvotes: 7

Views: 8245

Answers (5)

Ernest Collection
Ernest Collection

Reputation: 2292

The solutions above didn't work for me.

Another problem is that every time I need to add this functionality, I've ended adding a lot of stuff like timers, etc.

So I built a widget to do that:

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class ExtraLongDetector extends StatelessWidget {
  final Widget? child;
  final Duration duration;
  final VoidCallback onLongPress;

  const ExtraLongDetector({
    super.key,
    this.child,
    required this.duration,
    required this.onLongPress,
  });

  @override
  Widget build(BuildContext context) {
    return RawGestureDetector(
      gestures: <Type, GestureRecognizerFactory>{
        LongPressGestureRecognizer: GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
          () => LongPressGestureRecognizer(duration: duration),
          (instance) => instance.onLongPress = onLongPress,
        ),
      },
      child: child,
    );
  }
}

Then I use like this:

return Scaffold(
      body: ExtraLongDetector(
        duration: Duration(seconds: 10),
        onLongPress: () {
          ... do something only if I left the button pressed 10 seconds
        },
        child: GestureDetector(  // I've added another detector to detect taps (optional) 
          onTap: () {
            ... on tap do something
          },
          child: Container(),
        ),
      ),
    );

Upvotes: 3

R. Muro
R. Muro

Reputation: 1

GestureDetector(
      onTapDown: (_) { //Detect when you click the element
        _timer = Timer(
          const Duration(seconds: 5),
          () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => const ListOrder(),
              ),
            );
          },
        );
        print('tapping');
      },
      onTapUp: (_) { // Detect and cancel when you lift the click
        _timer!.cancel();
        print('cancel');
      },
      child: const Icon(Icons.person_search),
),

Upvotes: 0

jayesh saha
jayesh saha

Reputation: 351

How I did it:

onLongPress: () {
                Timer(Duration(milliseconds: (longPressIncrementDuration > 500) ? longPressIncrementDuration - 500 : 0), //enter function here//);

// I subtract 500 ms from required time limit as longpress on flutter activates after 500ms
              },

Upvotes: 1

t3nsa
t3nsa

Reputation: 880

I made a package today where you can set the duration on GestureDetector. if you wan you can try it out https://pub.dev/packages/custom_long_tap

Upvotes: 0

Rajni Gujarati
Rajni Gujarati

Reputation: 2937

You can solve your problem this way, use onPanCancel and onPanDown of GestureDetector with timer.

class _MyHomePageState extends State<MyHomePage> {
  Timer _timer;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: GestureDetector(
        onPanCancel: () => _timer?.cancel(),
        onPanDown: (_) => {
          _timer = Timer(Duration(seconds: 3), () { // time duration
            // your function here
          })
        },
      ),
    );
  }
}

let me know if it work for you.

Upvotes: 1

Related Questions