Is it possible to use ListView.builder
(or something similar) inside of a CustomScrollView
? I have a CustomScrollView
like this:
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(...),
SliverList(delegate: SliverChildListDelegate(children))
],
),
);
This works great, but in my actual scenario the list could have thousands of items, so I do not want to pass them all in to SliverChildListDelegate
. I want to use ListView.builder
(or something similar) to build the items as they are scrolled into view. I was expecting there to be a .builder
constructor on either SliverList
or SliverChildListDelegate
but I don't see anything like that. Am I missing something?
ListView is a very important widget in a flutter. It is used to create the list of children But when we want to create a list recursively without writing code again and again then ListView. builder is used instead of ListView.
builder takes properties similar to ListView but has two special parameters known as itemCount and itemBuilder .
To create a ListView call the constructor of the ListView class provided by flutter and provide required properties. There are no required properties for a listview widget. But we have to provide data to the children property, in order to display the listview. Basic implementation of ListView.
The delegate
argument of SliverList
is not necessarily a SliverChildListDelegate
.
You can also use SliverChildBuilderDelegate
to achieve the builder
effect of ListView.builder
SliverList(delegate: SliverChildBuilderDelegate((context, index) {
return Container();
}));
You can use List.generate as example below
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(...),
SliverList(delegate: SliverChildListDelegate(
List.generate(yourList.length, (idx) {
return Padding(
padding: const EdgeInsets.only(left: 8.0, right: 8.0),
child: Card(
child: ListTile(
leading: Icon(null),
title: Text(yourList[idx]),
onTap: null,
),
),
);
})
))
],
),
);
I'm not sure how it is done in CustomScrollView but you can try this:
Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return <Widget>[
SliverAppBar(...),
];
},
body: ListView.builder(..),)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With