merge: 合并 K87.1 下拉刷新冻结位移修复
This commit is contained in:
commit
e6f34977f5
2 files changed
+73
-15
No files matched your search
@@ -9,7 +9,6 @@ import '../theme/app_colors.dart';
|
|||||||
|
|
||||||
const double _maxPullDistance = 96;
|
const double _maxPullDistance = 96;
|
||||||
const double _refreshThreshold = 56;
|
const double _refreshThreshold = 56;
|
||||||
const double _holdDistance = 56;
|
|
||||||
const double _indicatorHeight = 56;
|
const double _indicatorHeight = 56;
|
||||||
const double _indicatorMaxOffset = 10;
|
const double _indicatorMaxOffset = 10;
|
||||||
const Duration _arrowTurnDuration = Duration(milliseconds: 160);
|
const Duration _arrowTurnDuration = Duration(milliseconds: 160);
|
||||||
@@ -42,6 +41,7 @@ class _AppPullToRefreshState extends State<AppPullToRefresh>
|
|||||||
late final AnimationController _offsetController;
|
late final AnimationController _offsetController;
|
||||||
_PullState _state = _PullState.idle;
|
_PullState _state = _PullState.idle;
|
||||||
bool _refreshing = false;
|
bool _refreshing = false;
|
||||||
|
bool _retracting = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -60,7 +60,7 @@ class _AppPullToRefreshState extends State<AppPullToRefresh>
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool _onScroll(ScrollNotification notification) {
|
bool _onScroll(ScrollNotification notification) {
|
||||||
if (notification.depth != 0 || _refreshing) return false;
|
if (notification.depth != 0 || _refreshing || _retracting) return false;
|
||||||
if (notification is ScrollStartNotification) {
|
if (notification is ScrollStartNotification) {
|
||||||
if (notification.dragDetails != null) _setPullDistance(0);
|
if (notification.dragDetails != null) _setPullDistance(0);
|
||||||
} else if (notification is ScrollUpdateNotification) {
|
} else if (notification is ScrollUpdateNotification) {
|
||||||
@@ -91,24 +91,31 @@ class _AppPullToRefreshState extends State<AppPullToRefresh>
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _release() {
|
void _release() {
|
||||||
if (_refreshing) return;
|
if (_refreshing || _retracting) return;
|
||||||
if (_state == _PullState.armed) {
|
if (_state == _PullState.armed) {
|
||||||
unawaited(_refresh());
|
unawaited(_refresh());
|
||||||
} else if (_state == _PullState.pull) {
|
} else if (_state == _PullState.pull) {
|
||||||
_offsetController.value = 0;
|
unawaited(_retract());
|
||||||
setState(() {
|
|
||||||
_state = _PullState.idle;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _retract() async {
|
||||||
|
if (_retracting) return;
|
||||||
|
_retracting = true;
|
||||||
|
await _animateOffsetTo(0);
|
||||||
|
if (!mounted) return;
|
||||||
|
setState(() {
|
||||||
|
_state = _PullState.idle;
|
||||||
|
_retracting = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _refresh() async {
|
Future<void> _refresh() async {
|
||||||
if (_refreshing) return;
|
if (_refreshing) return;
|
||||||
_refreshing = true;
|
_refreshing = true;
|
||||||
setState(() => _state = _PullState.refreshing);
|
setState(() => _state = _PullState.refreshing);
|
||||||
final refreshFuture = Future<void>.sync(widget.onRefresh);
|
final refreshFuture = Future<void>.sync(widget.onRefresh);
|
||||||
try {
|
try {
|
||||||
await _animateOffsetTo(_holdDistance);
|
|
||||||
await refreshFuture;
|
await refreshFuture;
|
||||||
} finally {
|
} finally {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
@@ -152,7 +159,10 @@ class _AppPullToRefreshState extends State<AppPullToRefresh>
|
|||||||
Transform.translate(
|
Transform.translate(
|
||||||
key: const Key('pull-refresh-content'),
|
key: const Key('pull-refresh-content'),
|
||||||
offset: Offset(0, offset),
|
offset: Offset(0, offset),
|
||||||
child: IgnorePointer(ignoring: _refreshing, child: child),
|
child: IgnorePointer(
|
||||||
|
ignoring: _refreshing || _retracting,
|
||||||
|
child: child,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
if (_state != _PullState.idle && offset > 0)
|
if (_state != _PullState.idle && offset > 0)
|
||||||
Positioned(
|
Positioned(
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ import 'package:fruitshorts/core/widgets/app_pull_to_refresh.dart';
|
|||||||
import 'package:fruitshorts/l10n/app_localizations.dart';
|
import 'package:fruitshorts/l10n/app_localizations.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('should_hold_pull_offset_until_refresh_completes', (
|
testWidgets('should_freeze_pull_offset_until_refresh_completes', (
|
||||||
tester,
|
tester,
|
||||||
) async {
|
) async {
|
||||||
|
expect(AppPullToRefresh.physics.parent, isA<ClampingScrollPhysics>());
|
||||||
final refresh = Completer<void>();
|
final refresh = Completer<void>();
|
||||||
var calls = 0;
|
var calls = 0;
|
||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
@@ -66,13 +67,12 @@ void main() {
|
|||||||
AppColors.blue,
|
AppColors.blue,
|
||||||
);
|
);
|
||||||
expect(calls, 1);
|
expect(calls, 1);
|
||||||
expect(_contentOffset(tester), greaterThan(56));
|
final releaseOffset = _contentOffset(tester);
|
||||||
|
expect(releaseOffset, greaterThan(56));
|
||||||
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
||||||
|
|
||||||
await tester.pump(const Duration(milliseconds: 200));
|
|
||||||
expect(_contentOffset(tester), closeTo(56, 0.1));
|
|
||||||
await tester.pump(const Duration(milliseconds: 300));
|
await tester.pump(const Duration(milliseconds: 300));
|
||||||
expect(_contentOffset(tester), closeTo(56, 0.1));
|
expect(_contentOffset(tester), closeTo(releaseOffset, 0.1));
|
||||||
|
|
||||||
final duplicate = await tester.startGesture(
|
final duplicate = await tester.startGesture(
|
||||||
tester.getCenter(find.byKey(const Key('pull-refresh-list'))),
|
tester.getCenter(find.byKey(const Key('pull-refresh-list'))),
|
||||||
@@ -81,7 +81,7 @@ void main() {
|
|||||||
await duplicate.up();
|
await duplicate.up();
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
expect(calls, 1);
|
expect(calls, 1);
|
||||||
expect(_contentOffset(tester), closeTo(56, 0.1));
|
expect(_contentOffset(tester), closeTo(releaseOffset, 0.1));
|
||||||
|
|
||||||
refresh.complete();
|
refresh.complete();
|
||||||
await tester.pump();
|
await tester.pump();
|
||||||
@@ -90,6 +90,54 @@ void main() {
|
|||||||
expect(find.text('Refreshing…'), findsNothing);
|
expect(find.text('Refreshing…'), findsNothing);
|
||||||
expect(find.text('Pull to refresh'), findsNothing);
|
expect(find.text('Pull to refresh'), findsNothing);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('should_animate_back_when_released_below_threshold', (
|
||||||
|
tester,
|
||||||
|
) async {
|
||||||
|
var calls = 0;
|
||||||
|
await tester.pumpWidget(
|
||||||
|
MaterialApp(
|
||||||
|
locale: const Locale('en'),
|
||||||
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||||
|
supportedLocales: AppLocalizations.supportedLocales,
|
||||||
|
home: Scaffold(
|
||||||
|
body: AppPullToRefresh(
|
||||||
|
onRefresh: () async {
|
||||||
|
calls++;
|
||||||
|
},
|
||||||
|
child: ListView(
|
||||||
|
key: const Key('pull-refresh-list'),
|
||||||
|
physics: AppPullToRefresh.physics,
|
||||||
|
children: const [SizedBox(height: 1200)],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
final list = find.byKey(const Key('pull-refresh-list'));
|
||||||
|
final gesture = await tester.startGesture(tester.getCenter(list));
|
||||||
|
await gesture.moveBy(const Offset(0, 30));
|
||||||
|
await tester.pump();
|
||||||
|
final releaseOffset = _contentOffset(tester);
|
||||||
|
expect(releaseOffset, greaterThan(0));
|
||||||
|
expect(releaseOffset, lessThan(56));
|
||||||
|
expect(find.text('Pull to refresh'), findsOneWidget);
|
||||||
|
|
||||||
|
await gesture.up();
|
||||||
|
await tester.pump();
|
||||||
|
expect(calls, 0);
|
||||||
|
expect(find.text('Refreshing…'), findsNothing);
|
||||||
|
expect(_contentOffset(tester), greaterThan(0));
|
||||||
|
|
||||||
|
await tester.pump(const Duration(milliseconds: 100));
|
||||||
|
expect(_contentOffset(tester), greaterThan(0));
|
||||||
|
expect(_contentOffset(tester), lessThan(releaseOffset));
|
||||||
|
|
||||||
|
await tester.pumpAndSettle();
|
||||||
|
expect(_contentOffset(tester), closeTo(0, 0.1));
|
||||||
|
expect(find.text('Pull to refresh'), findsNothing);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
double _contentOffset(WidgetTester tester) {
|
double _contentOffset(WidgetTester tester) {
|
||||||
|
|||||||
Reference in new issue
Block a user