首页 > 解决方案 > 滚动控制器未附加到任何滚动视图(Swiper)

问题描述

我正在使用一个Swiper包来在我的图像上实现轮播效果。我试图Swiper通过将callback函数传递给它的孩子来更新我的当前索引。

但是当我尝试调用该函数时,它会返回此“ scrollcontroller not attached”错误。

我添加了一个SwiperController但仍然相同。

这是我的代码:

SwiperController swiperController;

  @override
  Widget build(BuildContext context) {
    return Container(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width,
        color: Colors.black,
        child: Swiper(
          controller: swiperController,
          index: _index,
          scrollDirection: Axis.horizontal,
          itemBuilder: (BuildContext c, int i) {
            return StoriesPerUser(
              storiesList: widget.storiesList,
              selectedIndex: i,
              updateFunction: callBack,
            );
          },
          itemCount: widget.storiesList.length,
          loop: false,
          duration: 1000,
        ));
  }

  callBack() {
    setState(() {
     _index++; 
    });
  }

Please help.

标签: flutterdartflutter-layoutflutter-dependenciesflutter-test

解决方案


答案

如果你们中的任何一个人想要使用这个包,并且如果你想要一个类似于我的功能,而不是更新索引,只需使用其中一种方法SwiperControllernext().

这解决了我的问题:

callBack() {
    setState(() {
      swiperController.next();
    });
  }

推荐阅读