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,
})

make the header enter refreshing state,and callback onRefresh

Implementation

Future<void>? requestRefresh({bool needMove = true, bool needCallback = true, Duration duration = const Duration(milliseconds: 500), Curve curve = Curves.linear}) {
  assert(position != null, 'Try not to call requestRefresh() before build,please call after the ui was rendered');
  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 is for 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;
}