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

+79 -63
View File
@@ -9,76 +9,92 @@ import 'package:fruitshorts/core/widgets/app_pull_to_refresh.dart';
import 'package:fruitshorts/l10n/app_localizations.dart';
void main() {
testWidgets(
'should_show_pull_release_and_refreshing_states_and_refresh_once',
(tester) async {
final refresh = Completer<void>();
var calls = 0;
await tester.pumpWidget(
MaterialApp(
locale: const Locale('en'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: AppPullToRefresh(
onRefresh: () {
calls++;
return refresh.future;
},
child: ListView(
key: const Key('pull-refresh-list'),
physics: AppPullToRefresh.physics,
children: const [SizedBox(height: 1200)],
),
testWidgets('should_hold_pull_offset_until_refresh_completes', (
tester,
) async {
final refresh = Completer<void>();
var calls = 0;
await tester.pumpWidget(
MaterialApp(
locale: const Locale('en'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: AppPullToRefresh(
onRefresh: () {
calls++;
return refresh.future;
},
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 scrollable = tester.state<ScrollableState>(
find.descendant(of: list, matching: find.byType(Scrollable)).first,
);
expect(scrollable.position.pixels, lessThan(0));
expect(find.text('Pull to refresh'), findsOneWidget);
expect(
tester.widget<Text>(find.text('Pull to refresh')).style?.color,
AppColors.blue,
);
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 scrollable = tester.state<ScrollableState>(
find.descendant(of: list, matching: find.byType(Scrollable)).first,
);
expect(scrollable.position.pixels, 0);
expect(find.text('Pull to refresh'), findsOneWidget);
expect(_contentOffset(tester), greaterThan(0));
expect(
tester.widget<Text>(find.text('Pull to refresh')).style?.color,
AppColors.blue,
);
await gesture.moveBy(const Offset(0, 120));
await tester.pump();
expect(find.text('Release to refresh'), findsOneWidget);
expect(
tester.widget<Text>(find.text('Release to refresh')).style?.color,
AppColors.blue,
);
await gesture.moveBy(const Offset(0, 120));
await tester.pump();
expect(find.text('Release to refresh'), findsOneWidget);
expect(
tester.widget<Text>(find.text('Release to refresh')).style?.color,
AppColors.blue,
);
await gesture.up();
await tester.pump();
expect(find.text('Refreshing…'), findsOneWidget);
expect(
tester.widget<Text>(find.text('Refreshing…')).style?.color,
AppColors.blue,
);
expect(calls, 1);
await gesture.up();
await tester.pump();
expect(find.text('Refreshing…'), findsOneWidget);
expect(
tester.widget<Text>(find.text('Refreshing…')).style?.color,
AppColors.blue,
);
expect(calls, 1);
expect(_contentOffset(tester), greaterThan(56));
expect(find.byType(CircularProgressIndicator), findsOneWidget);
final duplicate = await tester.startGesture(
tester.getCenter(find.byKey(const Key('pull-refresh-list'))),
);
await duplicate.moveBy(const Offset(0, 70));
await duplicate.up();
await tester.pump();
expect(calls, 1);
await tester.pump(const Duration(milliseconds: 200));
expect(_contentOffset(tester), closeTo(56, 0.1));
await tester.pump(const Duration(milliseconds: 300));
expect(_contentOffset(tester), closeTo(56, 0.1));
refresh.complete();
await tester.pump();
await tester.pump(const Duration(milliseconds: 200));
expect(find.text('Refreshing…'), findsNothing);
expect(find.text('Pull to refresh'), findsNothing);
},
final duplicate = await tester.startGesture(
tester.getCenter(find.byKey(const Key('pull-refresh-list'))),
);
await duplicate.moveBy(const Offset(0, 70));
await duplicate.up();
await tester.pump();
expect(calls, 1);
expect(_contentOffset(tester), closeTo(56, 0.1));
refresh.complete();
await tester.pump();
await tester.pumpAndSettle();
expect(_contentOffset(tester), closeTo(0, 0.1));
expect(find.text('Refreshing…'), findsNothing);
expect(find.text('Pull to refresh'), findsNothing);
});
}
double _contentOffset(WidgetTester tester) {
final transform = tester.widget<Transform>(
find.byKey(const Key('pull-refresh-content')),
);
return transform.transform.storage[13];
}