Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter Retrigger Staggered Animation on ListView

I am using flutter_staggered_animations in my app for my listView. It is working quite nice when starting the app.

Problem:

I want the animation to be triggered if I change the child-widget of the listView or even just the itemCount. So what I need is a rebuild of the staggeredList.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But how can I do that? I tried simply changing the child or itemCount with setState. But that is triggering an animation…

Couldn’t find anything on this. Let me know if you need more info!

I use pretty much the exact code from the example:

@override
Widget build(BuildContext context) {
  return Scaffold(
    body: AnimationLimiter(
      child: ListView.builder(
        itemCount: 100,
        itemBuilder: (BuildContext context, int index) {
          return AnimationConfiguration.staggeredList(
            position: index,
            duration: const Duration(milliseconds: 375),
            child: SlideAnimation(
              verticalOffset: 50.0,
              child: FadeInAnimation(
                child: YourListChild(),
              ),
            ),
          );
        },
      ),
    ),
  );
}

>Solution :

You can provide a new key on AnimationLimiter, and it will recreate the AnimationLimiter,

AnimationLimiter(
  key: ValueKey("$itemCount"),
  child: ListView.builder(
class STA extends StatefulWidget {
  const STA({super.key});

  @override
  State<STA> createState() => _STAState();
}

class _STAState extends State<STA> {
  int itemCount = 5;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          itemCount++;
          setState(() {});
        },
      ),
      body: AnimationLimiter(
        key: ValueKey("$itemCount"),
        child: ListView.builder(
          itemCount: itemCount,
          itemBuilder: (BuildContext context, int index) {
            return AnimationConfiguration.staggeredList(
              position: index,
              duration: const Duration(milliseconds: 375),
              child: SlideAnimation(
                verticalOffset: 50.0,
                child: FadeInAnimation(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Container(
                      height: 50,
                      color: index.isEven ? Colors.amber : Colors.purple,
                    ),
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading