如何制作一个ListView.builder从一个特定的索引开始 [英] How to make a `ListView.builder` Start at a Specific Index

查看:95
本文介绍了如何制作一个ListView.builder从一个特定的索引开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListView.builder是否可以从小部件列表的第二个(index = 1)项目开始?

Is there a way for a ListView.builder to start, say, at the second (index = 1) item of a list of widgets?

就我而言-有关更多信息,请此处

In my case — more info here and here if you're interested —, I'm trying to add some empty space at the top of a ListView so the user can scroll the top card closer to his thumb. A workaround would be to add empty Containers to the top and bottom of the List of widgets to simulate empty space. However, the ListView will render the Containers on the screen and user might become confused and not know that there are more cards available by simply scrolling.

也许可以用ScrollController来做到这一点?

Maybe there is a way of doing this with a ScrollController?

我认为公开代码不会有太大帮助,因为我只是使用典型的ListView.builder,但是在这种情况下,如果您需要复习.

I don't think that exposing code will help much because I'm just using a typical ListView.builder, but here it is in case you need a refresher.

ListView.builder(
  itemCount: widgetsList.length,
  itemBuilder: (context, index){
    return widgetsList[index];
  },
),

推荐答案

您可以使用ScrollController并将其initialScrollOffset保留在原来的位置.

You can use a ScrollController and have its initialScrollOffset where you want it to originally be.

  1. 在窗口小部件的类中声明您的ScrollController.
ScrollController scrollController = ScrollController(
  initialScrollOffset: 10, // or whatever offset you wish
  keepScrollOffset: true,
);

  • 将控制器添加到您的ListView

    ListView.builder(
      controller: scrollController,
      itemCount: widgetsList.length,
      itemBuilder: (context, index){
        return widgetsList[index];
      },
    ),
    

  • 此外,您甚至可能要为背景滚动动作创建动画.这可以通过在窗口小部件的initState中使用.animateTo方法来实现.

    Additionally, you may even want to create animations for the background scrolling action. That is possible through using the .animateTo method inside your widget's initState.

    这篇关于如何制作一个ListView.builder从一个特定的索引开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆