feat(ui): 下拉刷新完成后才回弹

This commit is contained in:
Zhou Weidong committed 2026-10-21 15:12:41 +08:00
1 parent d39d8d43c1
commit 45e8707f1e
2 files changed
+157 -104

No files matched your search

+78 -41
View File
@@ -9,10 +9,11 @@ import '../theme/app_colors.dart';
const double _maxPullDistance = 96;
const double _refreshThreshold = 56;
const double _indicatorHeight = 64;
const double _holdDistance = 56;
const double _indicatorHeight = 56;
const double _indicatorMaxOffset = 10;
const Duration _arrowTurnDuration = Duration(milliseconds: 160);
const Duration _retractDuration = Duration(milliseconds: 200);
const Duration _settleDuration = Duration(milliseconds: 200);
enum _PullState { idle, pull, armed, refreshing }
@@ -26,7 +27,7 @@ class AppPullToRefresh extends StatefulWidget {
});
static const AlwaysScrollableScrollPhysics physics =
AlwaysScrollableScrollPhysics(parent: BouncingScrollPhysics());
AlwaysScrollableScrollPhysics(parent: ClampingScrollPhysics());
final Widget child;
final Future<void> Function() onRefresh;
@@ -36,11 +37,28 @@ class AppPullToRefresh extends StatefulWidget {
State<AppPullToRefresh> createState() => _AppPullToRefreshState();
}
class _AppPullToRefreshState extends State<AppPullToRefresh> {
class _AppPullToRefreshState extends State<AppPullToRefresh>
with SingleTickerProviderStateMixin {
late final AnimationController _offsetController;
_PullState _state = _PullState.idle;
double _pullDistance = 0;
bool _refreshing = false;
@override
void initState() {
super.initState();
_offsetController = AnimationController(
vsync: this,
duration: _settleDuration,
upperBound: _maxPullDistance,
);
}
@override
void dispose() {
_offsetController.dispose();
super.dispose();
}
bool _onScroll(ScrollNotification notification) {
if (notification.depth != 0 || _refreshing) return false;
if (notification is ScrollStartNotification) {
@@ -53,7 +71,7 @@ class _AppPullToRefreshState extends State<AppPullToRefresh> {
}
} else if (notification is OverscrollNotification) {
if (notification.dragDetails != null) {
_setPullDistance(_pullDistance - notification.overscroll);
_setPullDistance(_offsetController.value - notification.overscroll);
}
} else if (notification is ScrollEndNotification) {
_release();
@@ -68,19 +86,17 @@ class _AppPullToRefreshState extends State<AppPullToRefresh> {
: distance > 0
? _PullState.pull
: _PullState.idle;
if (distance == _pullDistance && state == _state) return;
setState(() {
_pullDistance = distance;
_state = state;
});
_offsetController.value = distance;
if (state != _state) setState(() => _state = state);
}
void _release() {
if (_refreshing) return;
if (_state == _PullState.armed) {
unawaited(_refresh());
} else if (_state == _PullState.pull) {
_offsetController.value = 0;
setState(() {
_pullDistance = 0;
_state = _PullState.idle;
});
}
@@ -90,53 +106,74 @@ class _AppPullToRefreshState extends State<AppPullToRefresh> {
if (_refreshing) return;
_refreshing = true;
setState(() => _state = _PullState.refreshing);
final refreshFuture = Future<void>.sync(widget.onRefresh);
try {
await widget.onRefresh();
await _animateOffsetTo(_holdDistance);
await refreshFuture;
} finally {
await Future<void>.delayed(_retractDuration);
if (mounted) {
setState(() {
_pullDistance = 0;
_state = _PullState.idle;
_refreshing = false;
});
await _animateOffsetTo(0);
if (mounted) {
setState(() {
_state = _PullState.idle;
_refreshing = false;
});
}
}
}
}
Future<void> _animateOffsetTo(double target) async {
try {
await _offsetController
.animateTo(target, duration: _settleDuration, curve: Curves.easeOut)
.orCancel;
} on TickerCanceled {
// The overlay can be removed while the refresh future is still pending.
}
}
@override
Widget build(BuildContext context) {
return NotificationListener<ScrollNotification>(
onNotification: _onScroll,
child: Stack(
fit: StackFit.expand,
children: [
Listener(
onPointerUp: (_) => _release(),
onPointerCancel: (_) => _release(),
child: widget.child,
),
if (_state != _PullState.idle)
Positioned(
top: widget.edgeOffset,
left: 0,
right: 0,
child: IgnorePointer(child: _indicatorLayer(context)),
),
],
child: AnimatedBuilder(
animation: _offsetController,
child: Listener(
onPointerUp: (_) => _release(),
onPointerCancel: (_) => _release(),
child: widget.child,
),
builder: (context, child) {
final offset = _offsetController.value;
return Stack(
fit: StackFit.expand,
children: [
Transform.translate(
key: const Key('pull-refresh-content'),
offset: Offset(0, offset),
child: IgnorePointer(ignoring: _refreshing, child: child),
),
if (_state != _PullState.idle && offset > 0)
Positioned(
top: widget.edgeOffset,
left: 0,
right: 0,
child: IgnorePointer(child: _indicatorLayer(context, offset)),
),
],
);
},
),
);
}
Widget _indicatorLayer(BuildContext context) {
final progress = (_pullDistance / _refreshThreshold).clamp(0.0, 1.0);
final opacity = _state == _PullState.refreshing
? 1.0
: progress.clamp(0.0, 1.0);
Widget _indicatorLayer(BuildContext context, double offset) {
final progress = (offset / _refreshThreshold).clamp(0.0, 1.0);
return SizedBox(
height: _indicatorHeight,
child: Opacity(
opacity: opacity,
opacity: progress,
child: Transform.translate(
offset: Offset(0, _indicatorMaxOffset * progress),
child: Align(