Tomek Polański
1 min readMay 26, 2020

--

Hey Marwin,

there are two approaches to your issue:

1) check out what is causing the lagginess in your GestureDetector

GestureDetector is quite performant so probably the issue is with your widget - setState forces your entire widget to rebuild and if that widget is too complex, then it will lag.

You can use setState, for example, to update your widget when typing as it only happens way less frequently then animations (16ms on 60Hz). Still, if you want to have smooth swipe animation there is the better way:

2) For animations, you should be using AnimationController. It has a useful method `fling`. It creates an animation that is this swipe/fling animation. Then using that controller you should use it with SlideTransition.

To check how it is done check Dismissible widget (https://api.flutter.dev/flutter/widgets/Dismissible-class.html) and check it's source code (https://github.com/flutter/flutter/blob/c6fe7bb9e13dc630d04a0553ae74c9bdfd913688/packages/flutter/lib/src/widgets/dismissible.dart).

That widget

- first gets the velocity of the drag/swipe from GestureDetector (https://github.com/flutter/flutter/blob/c6fe7bb9e13dc630d04a0553ae74c9bdfd913688/packages/flutter/lib/src/widgets/dismissible.dart#L579)

- passes the velocity to the AnimationController (https://github.com/flutter/flutter/blob/c6fe7bb9e13dc630d04a0553ae74c9bdfd913688/packages/flutter/lib/src/widgets/dismissible.dart#L428)

- creates Tween with that controller (https://github.com/flutter/flutter/blob/c6fe7bb9e13dc630d04a0553ae74c9bdfd913688/packages/flutter/lib/src/widgets/dismissible.dart#L369)

- and finally uses SlideTransition with it (https://github.com/flutter/flutter/blob/c6fe7bb9e13dc630d04a0553ae74c9bdfd913688/packages/flutter/lib/src/widgets/dismissible.dart#L552)

That's really high overview and you would need to understand the code but if you want to learn and understand how to create a smooth animation, that's a really good place to start.

Cheers,

Tomek

--

--

Tomek Polański
Tomek Polański

Written by Tomek Polański

Passionate mobile developer. One thing I like more than learning new things: sharing them

Responses (1)