如何制作一个ListView.builder从一个特定的索引开始 [英] How to make a `ListView.builder` Start at a Specific Index
问题描述
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 Container
s to the top and bottom of the List
of widgets to simulate empty space. However, the ListView
will render the Container
s 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.
- 在窗口小部件的类中声明您的
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屋!