计时器到期后,列表视图会转到下一个项目

时间:2019-10-25 04:51:10

标签: flutter dart flutter-layout flutter-listview

我想更改widget过期后父Listview.builder的索引,该索引是timer。基本上,它将转到list的下一项。

这是我的父窗口小部件中的代码

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

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

0 个答案:

没有答案