在PageView.builder中展开RefreshIndicator

问题描述 投票:2回答:1

我想在PageView.builder中实现水平RefreshIndicator。如果用户滚动到最后,我想刷新并获取新数据,但我尝试下拉刷新但指示灯没有出来,并且不会获取新数据。我怎样才能做到这一点?

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()),
            );
          },
        ),
      )
    );
flutter
1个回答
1
投票

你可以试试这种

  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 - 只是为了模拟延迟。

© www.soinside.com 2019 - 2024. All rights reserved.