flutter - SliverList / SliverChildBuilderDelegate supply initial index or allow negative indices

I'm not aware of an easy way to do this, there's no initialPositition parameter in ListView nor in SliverList. The reason I can think of is that lists are a series of widgets embedded on a ScrollView, such that in order for you to set an initial item, you would need to know the exact scroll offset of that item.

By default the two list widgets make no assumption about the height of its items, so in general finding that offset would require you to compute the heights of all widgets before it one by one, which is inefficient.

However, you can make things easier if you know beforehand the height of all your list items, or if you can force them a fixed height through either the ListView.itemExtent field or the SliverFixedExtentList.


In case you do know (or forced) the height of your list items beforehand, you can set an initial item through an initialScrollOffset in your ScrollController. Here's an example with a ListView.

@override
Widget build(BuildContext context) {
  final _itemExtent = 56.0; // I know item heights beforehand
  final generatedList = List.generate(500, (index) => 'Item $index');

  return ListView(
    controller: ScrollController(initialScrollOffset: _itemExtent * 401),
    children: generatedList
        .map((index) =>
            ListTile(title: Text(index, style: TextStyle(fontSize: 20.0))))
        .toList(),
  );
}

Or in a SliverList.

@override
Widget build(BuildContext context) {
  final _itemExtent = 56.0;
  final generatedList = List.generate(500, (index) => 'Item $index');

  return CustomScrollView(
    controller: ScrollController(initialScrollOffset: _itemExtent * 401),
    slivers: [
      SliverFixedExtentList(
        itemExtent: _itemExtent,  // I'm forcing item heights
        delegate: SliverChildBuilderDelegate(
          (context, index) => ListTile(
                title: Text(
                  generatedList[index],
                  style: TextStyle(fontSize: 20.0),
                ),
              ),
          childCount: generatedList.length,
        ),
      ),
    ],
  );
}

In both cases this is the result when you first open the app.

enter image description here