Categories
Development

Listview go to next item after timer expires

I want to change the index of my parent widget which is a Listview.builder after the timer expires. Basically, it would go to the next item of the list.

here is the code from my parent widget:

EDIT I posted the wrong code. Here is the right code for the parent

Widget build(BuildContext context) {
    return Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        color: Colors.black,
        child: Swiper(
          index: widget.inititalIndex,
          itemBuilder: (BuildContext c, int i) {
            return StoriesPerUser(
              storiesList: widget.storiesList,
              selectedIndex: i,
            );
          },
          itemCount: widget.storiesList.length,
          loop: false,
          duration: 1000,
        ));
  }

I am passing the whole List to the ViewStoryModal and the index of the item that the user has selected.

here is the code of ViewStoryModal:

class StoriesPerUser extends StatefulWidget {
  StoriesPerUser({
    @required this.storiesList,
    @required this.selectedIndex,
  });

  final List<Stories> storiesList;
  int selectedIndex;

  _StoriesPerUserState createState() => _StoriesPerUserState();
}

class _StoriesPerUserState extends State<StoriesPerUser> {
  int _index = 0;
  int storiesLength = 0;
  CountDown _countDown;
  StreamSubscription _countDownSubscription;

  @override
  void initState() {
    super.initState();
    this._initTimer();
    print(widget.storiesList.length);
  }

  _initTimer() {
    if (mounted)
      setState(() {
        this._countDown = CountDown(Duration(seconds: 5));
        this._countDown.remainingTime = Duration(seconds: 5);
        this._countDownSubscription = this._countDown.stream.listen(null);
      });

    this._countDownSubscription.onData((d) {
      setState(() {}); // Updates the UI to animate the progress bar
    });

    this._countDownSubscription.onDone(() {
      if (widget.storiesList[widget.selectedIndex].story.length ==
          this._index + 1) {
        setState(() {
          storiesLength++;
        });
        if (widget.storiesList.length == storiesLength) {
          Navigator.of(context).pop();
        } else {
          setState(() {
             //THIS IS WHERE THE UPDATING SHOULD HAPPEN
            //IT SHOWS NEXT ITEM OF MY LISTVIEW BUT DOESN'T CHANGE THE INDEX OF THE ACTUAL LISTVIEW
            widget.selectedIndex++;
            this._index = 0;
            this._initTimer();
          });
        }
      } else {
        if (mounted)
          setState(() {
            this._index++;
            this._initTimer();
          });
      }
    });
  }

Leave a Reply

Your email address will not be published. Required fields are marked *