问题描述
我正在尝试在 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