首页 > 解决方案 > 计时器到期后,Listview 转到下一项

问题描述

我想更改过期后的父母widget的索引。基本上,它将转到.Listview.buildertimerlist

这是我的父小部件的代码

编辑我发布了错误的代码。这是父母的正确代码

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,
        ));
  }

我将整个 List传递给用户选择的项目的ViewStoryModalindex

这是 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();
          });
      }
    });
  }

标签: flutterdartflutter-layoutflutter-listview

解决方案


推荐阅读