Search code examples
flutterswipe-gestureraw-data

Raw touch/swipe data in Flutter? Compared to Java w/ Android Studio?


I am very new to flutter development, and I have to make a fairly quick decision on whether or not it is the right platform for my internship project.

I have to create an interface which requires all directional swipes to navigate to different menus (I'm thinking of doing nested horizontal and vertical scrolling, which I have had trouble with in Android Studio) - but more importantly, I have to save the raw data from the touching/tapping/swiping. I can't just save "left swipe" or "right swipe", I also have to know pressure, velocity, location, exact path, etc.

Is this feasible in flutter? How does flutter handle this raw data as opposed to Android studio? Does flutter only determine the approximate direction of the swipe and that's it?

I have tried searching for answers all day, but I must be missing some key word, because I have been unable to find the answer so far.


Solution

  • GestureDetector is a very extensive Widget in this regard. It has all the capabilities you are searching for. A simpler version of it, which also has Material design built in, is InkWell, but this might be lacking some of the functionality you are searching for.

    With a GestureDetector wrapped about your Widget you will be able to catch all hit events (you can even specify HitTestBehavior (with the behavior parameter).

    For your custom interactions there are plenty of callbacks implemented. I linked you to the constructor, which contains a bunch of useful parameters, like onTapDown/Up, onVertical/HorizontalDragStart/Update/End.

    This is not even everything, but using those you can programatically define your behavior. Let me explain the concept with a small example:

    Offset start;
    
    void verticalDragStart(DragStartDetails details) {
      // process the start by working with the details
      start = details.globalPosition;
      // ...
    }
    
    void verticalDragUpdate(DragUpdateDetails details) {
      // apply your logic
      Offset delta = details.delta;
      // ...
    }
    
    // use DragEnd, also for horizontal, pan etc.
    
    @override
    Widget build(BuildContext context) => GestureDectector(
      onVerticalDragStart: verticalDragStart,
      // ...
    );
    

    I hope that you can imagine all the possibilties this enables. You can also combine different callbacks. Just take a look at the parameters in the documentation and experiment with what fits for you.

    I think that this is the raw data you asked for.