问题:使用PageController * 加载浏览量后,无法滚动到POSITION


Widget _buildCarousel(BuildContext context, int selectedIndex) {

    PageController controller = PageController(viewportFraction: 1, keepPage: true);
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        SizedBox(
          // you may want to use an aspect ratio here for tablet support
          height: 400.0,
          width: 240,
          child: PageView.builder(
            itemCount: assetImageList.length,
            controller: controller,
            itemBuilder: (BuildContext context, int itemIndex) {
              return _buildCarouselItem(context, selectedIndex, itemIndex);
            },
          ),
        )
      ],
    );
  }

最佳答案

终于找到答案了。只需设置 initialPage: mSelectedPosition 属性即可,例如:

child: PageView.builder(
        itemCount: mTemplateModelList.length,
        controller: PageController(initialPage: mSelectedPosition, keepPage: true, viewportFraction: 1),
        itemBuilder: (BuildContext context, int itemIndex) {
          return _buildCarouselItem(context, selectedIndex, itemIndex);
        },
      ),

或者,如果您想在单击按钮后滚动页面,则可以使用 PageController 使用 jumpTo()方法,该方法在下面已由其他用户明确提及:@android。

关于scroll - 如何在Flutter中滚动或跳到PageView.builder或PageController的位置?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56054358/

10-11 00:58