Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Hero-like transition in PageView

I have an image in one page of the PageView. I want to animate this image to the next page when i go to it, sort of like when you use Hero animations in navigator page transitions. Any ideas how this can be achieved?

like image 403
DeToxer Avatar asked Apr 09 '20 13:04

DeToxer


People also ask

How to create a hero transition animation in flutter?

The first one is tag which is used as identifier. When moving to another page, Flutter will create the transition animation if each page contains a Hero with the same tag value. You are also required to pass child which is the Widget where the hero animation will be applied on. Creating a Hero is very simple.

How to use Pageview widget in flutter?

The PageView widget allows the user to transition between different screens in their flutter application. All you need to set it up are a PageViewController and a PageView. scrollDirection: It sets the axis of scrolling ( Vertical or horizontal ).

What is a hero widget in flutter?

The hero refers to the widget that flies between screens. Create a hero animation using Flutter’s Hero widget. Fly the hero from one screen to another. Animate the transformation of a hero’s shape from circular to rectangular while flying it from one screen to another.

How to get the widget of a second page hero?

To do so, you need to pass a function as placeholderBuilder. The passed function is responsible to return the placeholder widget. If the placeholderBuilder is only defined on the second page's Hero, the widget returned by HeroPlaceholderBuilder function is only displayed on the second page Hero 's child.


2 Answers

In order to achieve an animation similar to the hero animation. Basically what we will implement is this, you will need 5 elements:

  1. The initial global coordinates
  2. The final coordinates
  3. Initial widget size
  4. Final widget size
  5. The widget you want to animate

Now in order to create this animation that spans the 2 screens we will need to use the overlay that is present in flutter. link

  1. Add to the Overlay the widget that is to be animated (Not the widget present on the screen, a new instance).
  2. Position this overlay entry to be in the starting coordinate of the animation (The original widget's position).
  3. Hide the original widget.
  4. Create an animation that changes the x and y coordinates to the create the custom motion, this can be any curve, the simplest being a simple tween between the starting coordinate and the end coordinate.
  5. Another animation that animates size change between the start and end size. This animation can be combined with the previous one. (One tween animation but the values are calculated manually)
  6. After the animation is done, remove the overlay entry, and show the new widget. (This can be done as a timed operation or as a call back operation)

This is the most flexible between all of the available options since you have access to the widget in all of its states. However you must be very careful when using this method as it might cause a slowdown if not properly implemented.

like image 173
Mohammad Kurjieh Avatar answered Oct 14 '22 02:10

Mohammad Kurjieh


This packages does what you want https://pub.dev/packages/coast

It is essentially taking the same approach as Flutter’s Hero and HeroController: Detect when a transition is started, search through the element trees of the source and target pages for the widgets marked for animation (heroes, crabs), pair them up based on their tags, and finally create an entry in an Overlay whose position and size is the interpolation between those on the source and target pages.

like image 40
spkersten Avatar answered Oct 14 '22 02:10

spkersten