【问题标题】:Is it possible to use ListView.builder inside of CustomScrollView?是否可以在 CustomScrollView 中使用 ListView.builder?
【发布时间】:2019-07-25 05:12:23
【问题描述】:

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

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

这很好用,但在我的实际场景中,列表可能包含数千个项目,所以我不想将它们全部传递给SliverChildListDelegate。我想使用ListView.builder(或类似的东西)来构建滚动到视图中的项目。我原以为SliverListSliverChildListDelegate 上会有一个.builder 构造函数,但我没有看到类似的东西。我错过了什么吗?

【问题讨论】:

    标签: listview dart flutter


    【解决方案1】:

    这对我有用,我可以有一个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) {
    
                        }
                      ),
                    ),
                  ],
                ),
              )
            ]))
          ],
        );
    

    【讨论】:

    【解决方案2】:

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

    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,
                        ),
                      ),
                    );
                  })
          ))
        ],
      ),
    );
    

    【讨论】:

    • 除了您提供的代码之外,请考虑总结一下为什么这样可以解决问题。
    【解决方案3】:

    SliverListdelegate 参数不一定是 SliverChildListDelegate

    也可以使用SliverChildBuilderDelegate实现ListView.builderbuilder效果

    SliverList(delegate: SliverChildBuilderDelegate((context, index) {
      return Container();
    }));
    

    【讨论】:

    • 我正在使用 SilverBuilderDelegate 但滚动仍然不起作用。我提出了一个问题。 stackoverflow.com/questions/62437049/…
    • 需要添加子计数...所以委托:SliverChildBuilderDelegate( (BuildContext context, int index) { return ...; }, childCount: ...length, ),
    【解决方案4】:

    我不确定它是如何在 CustomScrollView 中完成的,但你可以试试这个:

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

    【讨论】:

    • 这正是我正在寻找的答案,谢谢
    • 你成就了我的一天。我努力将 ListView 转换为 Slivers 以在滚动时隐藏页面标题,直到我找到使用 NestedScrollViewheaderSliverBuilder 的解决方案。非常感谢。
    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2020-01-22
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    相关资源
    最近更新 更多