当列表未填满整个页面时刷新指示器不起作用



>我有一个页面,有一个带有刷新指示器的列表。当我在列表中有许多元素(填充整个视图等等,即我可以滚动(时,刷新指示器起作用。 但是,当我列表中只有一个或两个元素(没有可滚动的内容(时,刷新指示器不起作用。这是预期的行为吗?有没有办法让指标也适用于短列表?

我尝试用滚动条包装列表视图(刷新指示器的子项(,但问题仍然存在。


Future<void> _onRefresh() async {
_getPeople();
}

@override
Widget build(BuildContext context) {
super.build(context);
return new Container(
child: _items.length == 0
? Center(child: CircularProgressIndicator())
: new RefreshIndicator(
onRefresh: _onRefresh,
child: Scrollbar(
child: new ListView.builder(
controller: controller,
itemBuilder: (context, index) {
return PeopleCard(
People: _items[index],
source: 2,
);
},
itemCount: _items.length,
),
)),
);
}

如果项目列表的长度为 1 或 2 或任何不需要滚动的数量,则刷新指示器也应该有效。

将物理设置为AlwaysScrollableScrollPhysics,就像下面的代码一样。

new RefreshIndicator(
key: _refreshIndicatorKey,
color: Colors.blue,
onRefresh: (){
setState(() {
_future = fetchPosts(http.Client()); 
_getAvalailableSlots();
});
},
child: ListView.builder(
physics: AlwaysScrollableScrollPhysics(),
itemCount: data.length,
itemBuilder: (context, i) {
//do something
}
)
)

情况 1:

在 Listview 或 SingleChildScrollView 中使用 AlwaysScrollableScrollPhysics((。

physics: const AlwaysScrollableScrollPhysics(),

案例2:

如果你想使用其他滚动物理,使用这种方式...

physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics(),
),

案例3:

确保你没有做收缩包装,真的。收缩包装必须像默认值一样为假。

收缩包装:真。 ❌

收缩包装:假。 ✅

shrinkWrap: false,

案例4:

如果您有除列表视图以外的任何自定义小部件,请以这种方式使用堆栈和列表视图...

RefreshIndicator(
onRefresh: () async {
print('refreshing');
},
child: Stack(
children: [
YourWidget(),
ListView(
physics: const AlwaysScrollableScrollPhysics(),
),
],
),
),

与 BouncingScrollPhysics 一起使用:

RefreshIndicator(
onRefresh: () async {

},
child: ListView.builder(
physics: BouncingScrollPhysics(
parent: AlwaysScrollableScrollPhysics()),
itemBuilder: (context, index) {
return Text("${index}");
}),

要使刷新指示器始终显示,请为可滚动子项设置physics: const AlwaysScrollableScrollPhysics(),如下所示。

ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: <Widget>[widget.bodyWidget]
)

如果您仍然有一些错误,以下内容可能会有所帮助。

  • ListView小部件需要具有高度值才能"始终"垂直滚动。因此,请用SizedBox或其他适当的小部件将其包裹并设置高度。
  • 如何固定高度?使用MediaQuery.of(context).size.height获取视口的高度,如果需要,减去不可滚动部分的值。
  • 如果你使用SingleChildScrollView而它不适用于AlwaysScrollableScrollPhysics(),那是因为孩子没有身高。 固定孩子的身高或只使用ListView.

综上所述,以下内容对我有用。

sizes.bottomNavigationBarsizes.appBar是我为自定义应用栏和导航栏而创建的常量变量。

RefreshIndicator(
onRefresh: widget.onRefresh,
child: SizedBox(
height: MediaQuery.of(context).size.height -
sizes.bottomNavigationBar -
sizes.appBar,
child: ListView(
physics: const AlwaysScrollableScrollPhysics(),
children: <Widget>[widget.bodyWidget]
)),
)

如果您发现任何可靠性,刷新指示器不显示官方文档的一部分会有所帮助。

如果您在带有展开的列中使用列表视图。

尝试删除

shrinkWrap:true

最新更新