如何在 Flutter 中的 CustomScrollView 中拥有 ListView.builder大列表?

问题描述

我正在尝试在 CustomScrollView 内的 SliverList 中使用大约 500 个项目的 ListView 构建器。

Scaffold(
    appBar: AppBar(
      title: Text(
        'Test',style: TextStyle(fontFamily: 'diogenes',fontSize: 30),),actions: [
        // IconButton(icon: Icon(Icons.list)),],body: CustomScrollView(
      slivers: <Widget>[
        SliverAppBar(...),SliverToBoxAdapter(...),SliverList(
            delegate: SliverChildBuilderDelegate((context,index) =>
                getHistoricalFixtures(pythiePerformance)))
      ],);

Widget getHistoricalFixtures(data) {
return Padding(
  padding: EdgeInsets.fromLTRB(10,10,10),child: Container(
    height: 2000,child: _buildFixtures(data),);

}

Widget _buildFixtures(data) {
return new ListView.builder(
    itemCount: data.historicalFixtures.length,physics: NeverScrollableScrollPhysics(),itemBuilder: (context,index) {
      return Container(...)
    });

}

当 ListView 嵌入到具有固定大小的容器中时,它工作正常。但是,很明显,我无法看到列表中的所有项目。

我试图将 ListView 放在一个 Expanded 小部件中,但出现此错误

任何帮助将不胜感激。

谢谢。

解决方法

您必须使用 SliverList(关于在 CustomScrollView 内添加列表视图)或使用 SliverGrid(关于在 CustomScrollView 内添加网格视图) 像下面的代码:

SliverList(
          delegate: SliverChildListDelegate(
            [
              BodyWidget(Colors.blue),BodyWidget(Colors.red),BodyWidget(Colors.green),BodyWidget(Colors.orange),BodyWidget(Colors.blue),],),

SliverGrid(
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),delegate: SliverChildListDelegate(
            [
              BodyWidget(Colors.blue),BodyWidget(Colors.yellow),

我希望这个链接有用:)

https://medium.com/codechai/flutter-listview-gridview-inside-scrollview-68b722ae89d4

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...