How to increase the time limit for onLongPress?

Viewed 967

I am fairly new to flutter. I know that the long press is fired when the user touches the screen for more than 500 milliseconds. Is there a way to change it to maybe like 2 seconds?

In my code I used inkresponse to get two different functions from the fab. On tap it goes to 1st page and on long press it goes to the 2nd page. I wanted the long press to be fairly longer and maybe add animation to indicate that the button is being pressed.

Here is the code that I have written-

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(),
              ));},
        ),),
      ),),
2 Answers

You can use onPanDown and onPanCancel of GestureDetector to achieve that behavior. For simplicity, I removed InkResponse. You're free to wrap the GestureDetector in 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: () {}),
      ),
    );
  }
}

Using RawGestureDetector you have more customisation options:

RawGestureDetector(
  gestures: <Type, GestureRecognizerFactory>{
    LongPressGestureRecognizer: GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
      () => LongPressGestureRecognizer(
        debugOwner: this,
        duration: Duration(seconds: 2),
      ),
      (LongPressGestureRecognizer instance) {
        instance.onLongPress = () => print("Pressed long");
      },
    ),
  },
  child: Container(
    height: 400,
    width: 400,
    color: Color(0xffff0000),
  ),
)
Related