PageView.builder中的RefreshIndicator抖动

时间:2018-11-05 14:12:20

标签: flutter

我想在lights = b.lights # Creates a new Resource with its own URL 内实现水平PageView.builder。如果用户滚动到末尾,我想刷新并获取新数据,但我尝试下拉以刷新,但指标未显示,并且不会获取新数据。 我该怎么办?

RefreshIndicator

1 个答案:

答案 0 :(得分: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-仅用于模拟延迟。