是否可以在CustomScrollView中使用ListView.builder?

时间:2019-03-03 21:30:50

标签: listview dart flutter

是否可以在ListView.builder内使用CustomScrollView(或类似的东西)?我有这样的CustomScrollView

return Scaffold(
  body: CustomScrollView(
    slivers: [
      SliverAppBar(...),
      SliverList(delegate: SliverChildListDelegate(children))
    ],
  ),
);

这很好用,但是在我的实际情况下,列表可能包含成千上万个项目,因此我不想将它们全部传递给SliverChildListDelegate。我想使用ListView.builder(或类似的东西)来构建滚动到视图中的项目。我期望.builderSliverList上有一个SliverChildListDelegate构造函数,但我看不到任何类似的东西。我想念什么吗?

4 个答案:

答案 0 :(得分:3)

这对我有用,我可以拥有一个SliverAppBar

return CustomScrollView(
      slivers: [
        SliverAppBar(
        ),
        SliverList(
            delegate: SliverChildListDelegate([
          SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  child: ListView.builder(
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    itemBuilder: (BuildContext context, int index) {

                    }
                  ),
                ),
              ],
            ),
          )
        ]))
      ],
    );

答案 1 :(得分:0)

delegate的{​​{1}}参数不一定是SliverList

您还可以使用SliverChildListDelegate来达到SliverChildBuilderDelegate的{​​{1}}效果

builder

答案 2 :(得分:0)

您可以使用List.generate作为下面的示例

   author_books GET    /authors/:author_id/books(.:format)      books#index
                POST   /authors/:author_id/books(.:format)      books#create
new_author_book GET    /authors/:author_id/books/new(.:format)  books#new
      edit_book GET    /books/:id/edit(.:format)                books#edit
           book GET    /books/:id(.:format)                     books#show
                PATCH  /books/:id(.:format)                     books#update
                PUT    /books/:id(.:format)                     books#update
                DELETE /books/:id(.:format)                     books#destroy
        authors GET    /authors(.:format)                       authors#index
                POST   /authors(.:format)                       authors#create
     new_author GET    /authors/new(.:format)                   authors#new
    edit_author GET    /authors/:id/edit(.:format)              authors#edit
         author GET    /authors/:id(.:format)                   authors#show
                PATCH  /authors/:id(.:format)                   authors#update
                PUT    /authors/:id(.:format)                   authors#update
                DELETE /authors/:id(.:format)                   authors#destroy
          books GET    /books(.:format)                         books#index
                POST   /books(.:format)                         books#create
       new_book GET    /books/new(.:format)                     books#new

答案 3 :(得分:-1)

我不确定如何在CustomScrollView中完成此操作,但是您可以尝试以下操作:

Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
          return <Widget>[
            SliverAppBar(...),
          ];
        },
        body: ListView.builder(..),)
);
相关问题