Flutter. Set ListView initial position on an very large ListView

为君一笑 提交于 2020-03-26 06:29:16

问题


I want to create an almost infinite list of element, but I want to set the initial position of the list to some specific element. Something like this image:

Where index 0 would be the initial position, and this list may or may not extend very long in both directions.

I can create my elements like:

  Widget build_tile(int i){
    return Container(child:Text(i.toString()));
  }

and my list would be something like this

ListView.builder(
                  itemBuilder: (context, i) {
                    return build_tile(i-offset);
                  },
              scrollDirection: Axis.horizontal,
            ),

where offset would be 2 for the example in the picture, but how can I set where the list should start?

TLDR; How to make a very long list ListView like in 1 to start in certain element?

Thanks!


回答1:


You can set one of ScrollController's property: initialScrollOffset

But on the condition that you know the offset position of the target item/index.

ScrollController _controller = ScrollController(initialScrollOffset: itemHeight * index)

ListView.builder(
  controller: _controller,
  ...
),

(note that this example assumes that the sizes of your list's widgets are of constant size, but if you don't have constant widget sizes in your list, then you must be able to calculate the final offset position of your target item - and that is a whole other issue)

Or, if you want it to always be the last item/index, then it's much easier:

ScrollController _controller = ScrollController(initialScrollOffset: _controller.position.maxScrollExtent)


来源:https://stackoverflow.com/questions/55799477/flutter-set-listview-initial-position-on-an-very-large-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!