I had a List of dynamic List of ListTile
widgets and I wanted to load it lazily, ie ListTile
will be built only when the user scrolls to that position. And I achieved it using ListView.builder()
, and it was working fine for me.
But, when I wrapped that ListView.builder()
in SliverList
using SliverChildListDelegate
, the lazy loading property of Listview.builder()
is not working any more, now the whole Listview is built during initialization only, instead of being built lazily.
I want lazy loading in Listview.builder()
even after wrapping it with SliverList
. How can I achieve that?
The code before wrapping into SliverList:-
ListView.builder(
itemCount: items.length,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
if (index == 0) return userLocationWidget;
return ListTile(items[index]);
},
),
The code after warping it into SliverList:-
SliverList(
delegate: SliverChildListDelegate(
[
ListView.builder(
itemCount: items.length + 1,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: const EdgeInsets.all(0.0),
itemBuilder: (BuildContext context, int index) {
if (index == 0) return userLocationWidget;
return makeCard(items[index - 1]);
},
),
],
),
),
You can used following code:
SliverList(delegate: SliverChildBuilderDelegate(
[
SingleChildScrollView(
child: Container(
child:ListView.builder(
itemCount: items.length + 1,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
padding: const EdgeInsets.all(0.0),
itemBuilder: (BuildContext context, int index) {
if (index == 0) return userLocationWidget;
return makeCard(items[index - 1]);
},
),),
)
],
),
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.