AnimatedList auto scrolling to last item and back

459 views Asked by At

I've implemented an Animated list with SlideTransition like this

@override
Widget build(BuildContext context) {
  return Expanded(
    child: Container(
      child: ListView(
        children: [
          // Other widgets
          animatedList(),
        ],
      ),
    ),
  );
}

Widget animatedList() {
  return AnimatedList(
    shrinkWrap: true,
    key: _myKeyList,
    initialItemCount: _myItemsList.length,
    itemBuilder: (context, index, animation) {
      return SlideTransition(
        position: animation.drive(_offset),
        child: _buildMyItemTile[index],
      );
    },
  );
}

where _offset variable is a Tween animation. Each item of list is inserted and animated with a delay of 500 milliseconds.

Now, when all items are added to AnimatedList, i would like that AnimatedList content scroll automatically from first item to last (and back) continuously for show all its content. How can i do?

1

There are 1 answers

0
André On

Just do it add a controller

final controll = ScrollController();

then add this controller in the AnimatedList

AnimatedList(
   controller: controll,
    ...
    )

Make a function to call the last position or the first 0 for first position and 1 for last position or true false, I don't know, use your imagination

void getLastItem(int 0){
 var position = int == 0
 ? controll.position.minScrollExtent
 : controll.position.maxScrollExtent;
 controll.animateTo(
      position,
      duration: const Duration(milliseconds: 300),
      curve: Curves.easeInCubic,
    )
}

I hope I helped you.