requestRefresh method

Future<void>? requestRefresh({
  1. bool needMove = true,
  2. bool needCallback = true,
  3. Duration duration = const Duration(milliseconds: 500),
  4. Curve curve = Curves.linear,
})

手动触发下拉刷新

参数

  • needMove:是否需要动画滚动到刷新位置,默认为 true
  • needCallback:是否需要调用 onRefresh 回调,默认为 true
  • duration:动画持续时间,默认为 500 毫秒
  • curve:动画曲线,默认为 Curves.linear

返回值

  • Future

使用示例

// 手动触发刷新
_refreshController.requestRefresh();

// 带自定义动画的刷新
_refreshController.requestRefresh(
  duration: Duration(milliseconds: 800),
  curve: Curves.easeInOut,
);

Implementation

Future<void>? requestRefresh(
    {bool needMove = true,
    bool needCallback = true,
    Duration duration = const Duration(milliseconds: 500),
    Curve curve = Curves.linear}) {
  assert(position != null, '请不要在构建完成前调用 requestRefresh(),请在 UI 渲染后调用');
  if (isRefresh) return Future.value();

  StatefulElement? indicatorElement = _findIndicator(position!.context.storageContext, RefreshIndicator);
  if (indicatorElement == null || _refresherState == null) return null;

  (indicatorElement.state as RefreshIndicatorState).floating = true;

  if (needMove && _refresherState!.mounted) {
    _refresherState!.setCanDrag(false);
  }

  if (needMove) {
    return Future.delayed(const Duration(milliseconds: 50)).then((_) async {
      // - 0.0001 是为了兼容 NestedScrollView
      await position?.animateTo(position!.minScrollExtent - 0.0001, duration: duration, curve: curve).then((_) {
        if (_refresherState != null && _refresherState!.mounted) {
          _refresherState!.setCanDrag(true);
          if (needCallback) {
            headerMode!.value = RefreshStatus.refreshing;
          } else {
            headerMode!.setValueWithNoNotify(RefreshStatus.refreshing);
            if (indicatorElement.state.mounted) {
              (indicatorElement.state as RefreshIndicatorState).setState(() {});
            }
          }
        }
      });
    });
  } else {
    Future.value().then((_) {
      headerMode!.value = RefreshStatus.refreshing;
    });
  }
  return null;
}