gpt4 book ai didi

PageView.builder 中的 flutter RefreshIndicator

转载 作者:IT王子 更新时间:2023-10-29 06:43:36 26 4
gpt4 key购买 nike

我想在 RefreshIndicator 中实现横向 PageView.builder。如果用户滚动到最后,我想刷新并获取新数据,但我试图下拉刷新但指标没有出来,新数据也不会被获取。我该怎么做?

return new Scaffold(
appBar: new AppBar(),
body: new RefreshIndicator(
onRefresh: _onRefresh,
child: new PageView.builder(
scrollDirection: Axis.horizontal,
itemCount: a.length,
itemBuilder: (context, index) {
return new Center(
child: Text(a[index].toString()),
);
},
),
)
);

最佳答案

你可以试试这个

  int itemCount = 10;
StreamController<bool> streamController = StreamController.broadcast();

void getData() async {
await Future.delayed(Duration(seconds: 1));
itemCount += 10;
streamController.add(false);
}

Widget horizontalList() {
ScrollController scrollController = ScrollController();
return StreamBuilder(
initialData: false,
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
if (snapshot.hasData) {
if (snapshot.data) {
getData();
}
return Listener(
onPointerMove: (event) async {
scrollController.position.maxScrollExtent;
if (event.delta.dx < 0.0 && scrollController.position.pixels == scrollController.position.maxScrollExtent) {
streamController.add(true);
}
},
child: Stack(alignment: Alignment.center, children: <Widget>[
ListView.builder(
controller: scrollController,
itemCount: itemCount,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int pos) {
return Container(
child: Text('$pos'),
alignment: Alignment.center,
padding: EdgeInsets.all(12.0),
);
}),
snapshot.data ? CircularProgressIndicator() : Container()
],));
}
return Container();
},
stream: streamController.stream.distinct(),
);
}

getData - 仅用于模拟延迟。

关于PageView.builder 中的 flutter RefreshIndicator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53156084/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com