Flutter - how to have a widget respond to touch and flick? - flutter

Currently in the design phase of an app. One of the goals we have is to be able to touch a certain widget, and on tap and hold, have the widget then follow the user's finger where they drag.
Then, if the user releases gently, the widget snaps back to the original location.
However, if the user flicks the widget, we want the widget to fly across the screen, reacting correctly to the user's flick.
Is there anything built-in that can handle this? Also, if this needs to be explained more to make sense, happy to elaborate.
Thanks!

I think for such cases you can use GestureDetector widget. It provides several useful functions. See here:
https://api.flutter.dev/flutter/widgets/GestureDetector-class.html
You can use onPanStart of GestureDetector and then get the offset and use it to move the widget accross the screen.

Related

Flutter - make Text() glide if its too long

So basically I want to make a widget that checks how much space is available and if there is not enough space to wait a few seconds and then slowly glide and stop at the start position. This process should repeat forever and optimally don't use too much performance. If did not get a clear idea of how I want the widget to behave, just look at the Spotify song names.
You can use an AnimationController with a custom animation which moves a Positioned widget containing the Text widget to make your own ScrollingText widget. There's another question where this exact problem gets covered in more detail. You can just copy-paste the code from there and you should be good.

How to animate fullscreen photo transition on swipe, like in Telegram?

When you swipe vertically, image is dragged towards the swipe, with fading out animation of black background.
And after release, it smoothly returns to it's previous position on the screen, like Hero animation does.
How is it possible to recreate such an effect using Flutter? The same scenario, in fullscreen photo view.
photo_view package is desired for fullscreen, so it shouldn't interfere with zooming.
What you need is actually an out-of-the-box widget, and it's surprisingly easy to use. It's called Hero. Basically, you wrap the widget you want to animate like that in a Hero widget, with a specific tag string, and, when you navigate to another screen, you wrap the destination widget in another Hero with the same tag. An effect like the one you shared can be achieved by wrapping two widgets with the same photo with Heroes in different PageRoutes.
Check out this Flutter widget of the week video to get you started, and this Flutter.dev article for a more detailed explanation on Hero widgets.
Edit: I see you are looking for a more specific image-viewer behavior. Then, I suggest you use the photo_view package, which includes many functionalities to visualize images, including the hero transition with swipe-dow-to-dismiss behaviors, pinching to zoom, etc.
I found the solution.
To create such an animation, you should use extended_image package, which has SlideOutPage widget for creation of such transitions.

How do I detect more than one gesture on a single widget in flutter?

I want to listen for tap events on a widget and take one action for that, and a hold event and take a different action for that. It looks like flutter gesture detector can only detect a single gesture? This seems like it would be hugely limiting for mobile development though, so I figured there must be a way to detect two different gestures on one widget. How would I do that?
if you see the api docs, you will see there are detectors like doubletap, longpress etc
https://api.flutter.dev/flutter/widgets/GestureDetector-class.html

How to get a widget to move across the screen every X seconds?

Basically I'm trying to build a game to learn flutter more.
Right now I'm just trying to get a Text widget to move from the top of the phone screen, towards the bottom of the phone screen... I'm building a Space Invaders type of game with just Text.
From what I've read by googling the problem, should I use Flame? Or can everything be done by just using the base Flutter framework (collision detection, moving widgets, etc...)? Thanks.
You can use Draggable class for dragging the item.
A simple way to solve this problem is to have the moving Text widget a child of the Stack() widget. Actually, the child of the Stack() widget is a Positioned() widget, which has left and top properties, which you can update periodically with a Timer.periodic function to move the widget down by incrementing top property. So, your Text widget is a child of the Positioned() widget, that you use for, well, positioning.
I have a working example of this in my slowly_moving_widgets_field project, where I have a bunch of arbitrary widgets moving about the screen every X seconds as you desire.

Flutter. Throw widget after dragging

I have been searching around for days in the jungle of widgets, trying to figure out which widget suits my purpose best.
What I have done so far: I have a Positioned widget with a listener, that I can drag around in a Stack with onPointerMove.
What I want to achieve is the following:
If I move my finger a bit faster and at some point let go, I would like to have the widget to continue with the given speed (with some friction) and direction (x,y) until it reaches some point.
Im a iOS developer and did this in Swift, but have I reached a point where I met the limit of Flutter?
Flutter has a Draggable widget which contains velocity in the onDragEnd event. You might use this and some basic animation to fling the widget using physics. Check out the Cookbook on the Flutter API site.