在 Flutter 中停止 Listview.builder
Stop Listview.builder in Flutter
我正在从一个 WordPress 实例加载文章,我想在 ListView 中显示这些文章,但是我不想只在开始时加载所有文章的列表,因为这最终可能会很多。所以我想我可以用 FutureBuilders 做一个 Listview.builder 然后每次加载几篇文章,但是我有一个问题,在某些时候用户会到达我想要 ListView.builder 停止加载,因为没有更多文章可供加载。有没有这个功能,或者如何用 Flutter 做到这一点?另外,如果 Listview.Builder 一次只构建一个 Future Builder 就好了,这样屏幕上就不会出现 10 个加载图标了。
这是我的代码:
ListView.builder(
//controller: _scrollController,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 15,
),
scrollDirection: Axis.vertical,
// separatorBuilder: (context, index) {
// return SizedBox(
// height: 15,
// );
// },
// itemCount: this.articleList.length,
itemBuilder: (context, index) {
return FutureBuilder(
future: Article().getArticleList(index+1),
// ignore: missing_return
builder: (context, snapshot){
// ignore: missing_enum_constant_in_switch
switch (snapshot.connectionState){
case ConnectionState.none:
case ConnectionState.waiting:
return SpinKitWave(
color: Colors.red,
);
case ConnectionState.done:
case ConnectionState.active:
if (snapshot.hasData)
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return SomeWidgetThatDisplaysTheArticle;
},
);
return Text("Ende");
}
},
);
},
),
提前致谢
请指定一个整数。
int itemcount = 25; //指定你想要初始显示的列表视图项的数量。
ListView.builder(
//controller: _scrollController,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 15,
),
scrollDirection: Axis.vertical,
// separatorBuilder: (context, index) {
// return SizedBox(
// height: 15,
// );
// },
// itemCount: this.articleList.length,
itemBuilder: (context, index) {
return FutureBuilder(
future: Article().getArticleList(index+1),
// ignore: missing_return
builder: (context, snapshot){
// ignore: missing_enum_constant_in_switch
switch (snapshot.connectionState){
case ConnectionState.none:
case ConnectionState.waiting:
return SpinKitWave(
color: Colors.red,
);
case ConnectionState.done:
case ConnectionState.active:
if (snapshot.hasData)
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount:itemcount,
itemBuilder: (context, index){
return SomeWidgetThatDisplaysTheArticle;
},
);
return Text("Ende");
}
},
);
},
),
然后在最后加一个凸起的按钮
并指定按下以增加项目数:
onPressed(){
setState((){
itemCount = Fieldvalue.increment(10); //This will increase the itemCount by 10 so itemcount will become 35 .
});
}
用于将列表限制为 10 个项目
ListView.builder(
//controller: _scrollController,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 15,
),
scrollDirection: Axis.vertical,
// separatorBuilder: (context, index) {
// return SizedBox(
// height: 15,
// );
// },
// itemCount: this.articleList.length,
itemBuilder: (context, index) {
return FutureBuilder(
future: Article().getArticleList(index+1),
// ignore: missing_return
builder: (context, snapshot){
// ignore: missing_enum_constant_in_switch
switch (snapshot.connectionState){
case ConnectionState.none:
case ConnectionState.waiting:
return SpinKitWave(
color: Colors.red,
);
case ConnectionState.done:
case ConnectionState.active:
if (snapshot.hasData)
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return SomeWidgetThatDisplaysTheArticle;
},
);
return if(index<11) Text("Ende");
}
},
);
},
),
我正在从一个 WordPress 实例加载文章,我想在 ListView 中显示这些文章,但是我不想只在开始时加载所有文章的列表,因为这最终可能会很多。所以我想我可以用 FutureBuilders 做一个 Listview.builder 然后每次加载几篇文章,但是我有一个问题,在某些时候用户会到达我想要 ListView.builder 停止加载,因为没有更多文章可供加载。有没有这个功能,或者如何用 Flutter 做到这一点?另外,如果 Listview.Builder 一次只构建一个 Future Builder 就好了,这样屏幕上就不会出现 10 个加载图标了。
这是我的代码:
ListView.builder(
//controller: _scrollController,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 15,
),
scrollDirection: Axis.vertical,
// separatorBuilder: (context, index) {
// return SizedBox(
// height: 15,
// );
// },
// itemCount: this.articleList.length,
itemBuilder: (context, index) {
return FutureBuilder(
future: Article().getArticleList(index+1),
// ignore: missing_return
builder: (context, snapshot){
// ignore: missing_enum_constant_in_switch
switch (snapshot.connectionState){
case ConnectionState.none:
case ConnectionState.waiting:
return SpinKitWave(
color: Colors.red,
);
case ConnectionState.done:
case ConnectionState.active:
if (snapshot.hasData)
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return SomeWidgetThatDisplaysTheArticle;
},
);
return Text("Ende");
}
},
);
},
),
提前致谢
请指定一个整数。
int itemcount = 25; //指定你想要初始显示的列表视图项的数量。
ListView.builder(
//controller: _scrollController,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 15,
),
scrollDirection: Axis.vertical,
// separatorBuilder: (context, index) {
// return SizedBox(
// height: 15,
// );
// },
// itemCount: this.articleList.length,
itemBuilder: (context, index) {
return FutureBuilder(
future: Article().getArticleList(index+1),
// ignore: missing_return
builder: (context, snapshot){
// ignore: missing_enum_constant_in_switch
switch (snapshot.connectionState){
case ConnectionState.none:
case ConnectionState.waiting:
return SpinKitWave(
color: Colors.red,
);
case ConnectionState.done:
case ConnectionState.active:
if (snapshot.hasData)
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount:itemcount,
itemBuilder: (context, index){
return SomeWidgetThatDisplaysTheArticle;
},
);
return Text("Ende");
}
},
);
},
),
然后在最后加一个凸起的按钮 并指定按下以增加项目数:
onPressed(){
setState((){
itemCount = Fieldvalue.increment(10); //This will increase the itemCount by 10 so itemcount will become 35 .
});
}
用于将列表限制为 10 个项目
ListView.builder(
//controller: _scrollController,
physics: BouncingScrollPhysics(),
padding: EdgeInsets.symmetric(
horizontal: 15,
vertical: 15,
),
scrollDirection: Axis.vertical,
// separatorBuilder: (context, index) {
// return SizedBox(
// height: 15,
// );
// },
// itemCount: this.articleList.length,
itemBuilder: (context, index) {
return FutureBuilder(
future: Article().getArticleList(index+1),
// ignore: missing_return
builder: (context, snapshot){
// ignore: missing_enum_constant_in_switch
switch (snapshot.connectionState){
case ConnectionState.none:
case ConnectionState.waiting:
return SpinKitWave(
color: Colors.red,
);
case ConnectionState.done:
case ConnectionState.active:
if (snapshot.hasData)
return ListView.builder(
shrinkWrap: true,
primary: false,
itemCount: snapshot.data.length,
itemBuilder: (context, index){
return SomeWidgetThatDisplaysTheArticle;
},
);
return if(index<11) Text("Ende");
}
},
);
},
),