Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Using Hero Animation with ListView Builder

I am building List (ListView.builder) and Detail Screen. Hero is used to animate some part when widget is selected (using pushedReplacement).

I noticed that when I move from Detail Screen to List screen, if the selected widget is the end/tail/last widget of the ListView, then the animation won't run.

Because ListView.builder is only rendering the first/head viewable element, I think Hero does not know the location of the widget.

So, how do I solve this problem? It's not that critical, but it's bugging me for days.

like image 841
flemadap Avatar asked Aug 09 '19 05:08

flemadap


People also ask

How do you animate a hero in flutter?

Add a Hero widget to the second screen To complete the connection with the first screen, wrap the Image on the second screen with a Hero widget that has the same tag as the Hero in the first screen. After applying the Hero widget to the second screen, the animation between screens just works.

How do you use animation builder in flutter?

Work flow of the Flutter AnimationAnimationController(duration: const Duration(seconds: 2), vsync: this); animation = Tween<double>(begin: 0, end: 300). animate(controller); controller. forward(); Add animation based listener, addListener to change the state of the widget.


2 Answers

Hero widget identifies the elements to animate by its tag. The tag property must be unique in order to make this work. So what you can do is:

  1. Make the tag of each Hero unique. like,

    ListView.builder
    (
       itemCount: litems.length,
       itemBuilder: (BuildContext context, int index) {
       return Hero(
                    tag: "some_name"+index.toString(),
                    child: SomeChild();
                  );
       }
    )
    
  2. Pass the index to detailed screen on click. like,

    Navigator.push(context,
                   MaterialPageRoute(
                   builder: (BuildContext context) => DetailedScreen(index)
                   )
    );
    
  3. on the detailed screen create the tag using the index received. like,

    tag: "some_name"+index.toString()

Hope it will help you.

like image 187
Arjunraj kokkadan Avatar answered Oct 13 '22 16:10

Arjunraj kokkadan


what u need is to provide unique tag to each list-builder element

Hero(
       tag: snapshot.data.documents[index]['category'],
       child: // your child ,
    ),
like image 34
GJJ2019 Avatar answered Oct 13 '22 18:10

GJJ2019