5

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:

ListView

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!

Community
  • 1
  • 1
Isaac
  • 1,436
  • 2
  • 15
  • 29

1 Answers1

5

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)
TWL
  • 6,228
  • 29
  • 65