TatsuUkraine / flutter_sticky_infinite_list

Multi directional infinite list with Sticky headers for Flutter applications
BSD 2-Clause "Simplified" License
341 stars 31 forks source link

How can I set or scroll to an initial Item? #60

Closed desmeit closed 2 months ago

desmeit commented 1 year ago

At the first start I want to show a specific item of my InfiniteList. How is that possible?

InfiniteList(
                                controller: controller,
                                direction: InfiniteListDirection.single,

                                //negChildCount: 2,
                                posChildCount: _allItems.length,
                                scrollDirection: Axis.horizontal,
                                builder: (BuildContext context, int index) {
                                  return _buildInfinitItem(index);
                                })
TatsuUkraine commented 1 year ago

if you know offset, you can create a controller with the needed initial position ScrollController. initialScrollOffset. Just keep in mind that on first run Flutter will build list items starting from 0 position up to provided offset and then will dispose items that aren't visible.

Another option split your collection into 2 - one for items in the past (neg list) from your selected date, and one for current date and everything that goes in future (pos list). In that way, Current date will become the center of you list (e.g starting point for scrollable area)

desmeit commented 1 year ago

Okay I understand. If I try option 2, how can I work with negative indexes since Dart is not working with negative indexes?

TatsuUkraine commented 1 year ago

Everything depends on that collection you actually have. If you have Lists for both pos and neg collections, then you need to convert negative index in the builder to the positive number that corresponds to the index in negative list.

If you using Maps were key is a Date object, you can do selectedDate.add(Duration (days: index)) and you will get your Date key