如何防止RefreshIndicator触发ScrollStartNotification

时间:2019-11-11 21:59:34

标签: flutter dart

当刷新指示器处于活动状态时,如何防止_onStartScroll函数运行?尝试向下滑动以激活刷新,您应该在控制台中输入“ bottom”。

class TempScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: NotificationListener(
          onNotification: (scrollNotification) {
            if (scrollNotification is ScrollStartNotification) {
              _onStartScroll(scrollNotification.metrics);
            }
          },
          child: RefreshIndicator(
            onRefresh: () {

              return Future.delayed(
                  Duration(
                    milliseconds: 700,
                  ),
                      () => null);
            },
            child: CustomScrollView(
              physics: AlwaysScrollableScrollPhysics(),
              slivers: <Widget>[
                SliverToBoxAdapter(
                  child: Container(
                    child: Center(
                      child: Text("Swipe Down"),
                    ),
                    margin: EdgeInsets.only(top: 5),
                  ),
                ),
              ],
            ),
          )),
    );
  }

  _onStartScroll(ScrollMetrics metrics) {
    if (metrics.pixels == metrics.maxScrollExtent) {
      print('bottom');
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我们只能在更新时了解滚动方向,因此我们将仅在拖动开始后收听第一次更新:

bool isStarted = false;

...
      onNotification: (scrollNotification) {
        if (scrollNotification is ScrollUpdateNotification) {
          if (!isStarted) {
            isStarted = true;

            if (scrollNotification.metrics.pixels - scrollNotification.dragDetails.delta.dy > 0) {
              _onStartScroll(scrollNotification.metrics);
            }
          }
        } else if (scrollNotification is ScrollEndNotification) {
          isStarted = false;
        }
      },