merge: 合并下拉刷新页面跟随修正

This commit is contained in:
Zhou Weidong committed 2026-10-20 16:57:36 +08:00
commit fb0585c9cf
16 files changed
+85 -34

No files matched your search

+40 -2
View File
@@ -9,6 +9,8 @@ import '../theme/app_colors.dart';
const double _maxPullDistance = 96; const double _maxPullDistance = 96;
const double _refreshThreshold = 56; const double _refreshThreshold = 56;
const double _indicatorHeight = 64;
const double _indicatorMaxOffset = 10;
const Duration _arrowTurnDuration = Duration(milliseconds: 160); const Duration _arrowTurnDuration = Duration(milliseconds: 160);
const Duration _retractDuration = Duration(milliseconds: 200); const Duration _retractDuration = Duration(milliseconds: 200);
@@ -23,6 +25,9 @@ class AppPullToRefresh extends StatefulWidget {
this.edgeOffset = 0, this.edgeOffset = 0,
}); });
static const AlwaysScrollableScrollPhysics physics =
AlwaysScrollableScrollPhysics(parent: BouncingScrollPhysics());
final Widget child; final Widget child;
final Future<void> Function() onRefresh; final Future<void> Function() onRefresh;
final double edgeOffset; final double edgeOffset;
@@ -40,6 +45,12 @@ class _AppPullToRefreshState extends State<AppPullToRefresh> {
if (notification.depth != 0 || _refreshing) return false; if (notification.depth != 0 || _refreshing) 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) {
if (notification.dragDetails != null) {
_setPullDistance(
notification.metrics.minScrollExtent - notification.metrics.pixels,
);
}
} else if (notification is OverscrollNotification) { } else if (notification is OverscrollNotification) {
if (notification.dragDetails != null) { if (notification.dragDetails != null) {
_setPullDistance(_pullDistance - notification.overscroll); _setPullDistance(_pullDistance - notification.overscroll);
@@ -100,19 +111,46 @@ class _AppPullToRefreshState extends State<AppPullToRefresh> {
child: Stack( child: Stack(
fit: StackFit.expand, fit: StackFit.expand,
children: [ children: [
widget.child, Listener(
onPointerUp: (_) => _release(),
onPointerCancel: (_) => _release(),
child: widget.child,
),
if (_state != _PullState.idle) if (_state != _PullState.idle)
Positioned( Positioned(
top: widget.edgeOffset, top: widget.edgeOffset,
left: 0, left: 0,
right: 0, right: 0,
child: IgnorePointer(child: _indicator(context)), child: IgnorePointer(child: _indicatorLayer(context)),
), ),
], ],
), ),
); );
} }
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);
return SizedBox(
height: _indicatorHeight,
child: Opacity(
opacity: opacity,
child: Transform.translate(
offset: Offset(0, _indicatorMaxOffset * progress),
child: Align(
alignment: Alignment.topCenter,
child: Padding(
padding: const EdgeInsets.only(top: 8),
child: _indicator(context),
),
),
),
),
);
}
Widget _indicator(BuildContext context) { Widget _indicator(BuildContext context) {
final l10n = AppLocalizations.of(context)!; final l10n = AppLocalizations.of(context)!;
if (_state == _PullState.refreshing) { if (_state == _PullState.refreshing) {
+1 -1
View File
@@ -159,7 +159,7 @@ class _DiscoverPageState extends State<DiscoverPage> {
onRefresh: categories.load, onRefresh: categories.load,
child: ListView( child: ListView(
controller: logic.scroll, controller: logic.scroll,
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: EdgeInsets.only( padding: EdgeInsets.only(
top: showHeader ? _stickyHeaderHeight : 0, top: showHeader ? _stickyHeaderHeight : 0,
bottom: AppDimens.spaceXl, bottom: AppDimens.spaceXl,
+1 -1
View File
@@ -94,7 +94,7 @@ class _DownloadsBody extends StatelessWidget {
return AppPullToRefresh( return AppPullToRefresh(
onRefresh: downloads.load, onRefresh: downloads.load,
child: ListView( child: ListView(
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.fromLTRB(16, 10, 16, 28), padding: const EdgeInsets.fromLTRB(16, 10, 16, 28),
children: [ children: [
_PolicyPanel(downloads: downloads, policy: policy), _PolicyPanel(downloads: downloads, policy: policy),
+1 -1
View File
@@ -167,7 +167,7 @@ class _FavoritesBody extends StatelessWidget {
return AppPullToRefresh( return AppPullToRefresh(
onRefresh: favorites.load, onRefresh: favorites.load,
child: ListView( child: ListView(
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.fromLTRB( padding: const EdgeInsets.fromLTRB(
_pageHorizontal, _pageHorizontal,
4, 4,
+1 -1
View File
@@ -146,7 +146,7 @@ class _HomeBrowseViewState extends State<HomeBrowseView> {
child: ListView( child: ListView(
key: const Key('home-browse-page'), key: const Key('home-browse-page'),
controller: _scroll, controller: _scroll,
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.only(bottom: AppDimens.spaceXl), padding: const EdgeInsets.only(bottom: AppDimens.spaceXl),
children: _sections(l10n, catalog), children: _sections(l10n, catalog),
), ),
@@ -133,7 +133,7 @@ class _NewReleasesPageState extends State<NewReleasesPage> {
child: ListView( child: ListView(
key: const Key('new-releases-list'), key: const Key('new-releases-list'),
controller: _scroll, controller: _scroll,
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.only(bottom: 24), padding: const EdgeInsets.only(bottom: 24),
children: hero == null children: hero == null
? [ ? [
+3 -3
View File
@@ -69,7 +69,7 @@ class _NoticesPageState extends State<NoticesPage> {
return AppPullToRefresh( return AppPullToRefresh(
onRefresh: _controller.refresh, onRefresh: _controller.refresh,
child: ListView( child: ListView(
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
children: [ children: [
SizedBox( SizedBox(
height: MediaQuery.sizeOf(context).height * 0.7, height: MediaQuery.sizeOf(context).height * 0.7,
@@ -94,7 +94,7 @@ class _NoticesPageState extends State<NoticesPage> {
return AppPullToRefresh( return AppPullToRefresh(
onRefresh: _controller.refresh, onRefresh: _controller.refresh,
child: ListView( child: ListView(
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
children: [ children: [
SizedBox( SizedBox(
height: MediaQuery.sizeOf(context).height * 0.7, height: MediaQuery.sizeOf(context).height * 0.7,
@@ -113,7 +113,7 @@ class _NoticesPageState extends State<NoticesPage> {
onRefresh: _controller.refresh, onRefresh: _controller.refresh,
child: ListView.separated( child: ListView.separated(
key: const Key('notices-list'), key: const Key('notices-list'),
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.fromLTRB(16, 12, 16, 28), padding: const EdgeInsets.fromLTRB(16, 12, 16, 28),
itemCount: _controller.notices.length + 1, itemCount: _controller.notices.length + 1,
separatorBuilder: (_, _) => const SizedBox(height: 10), separatorBuilder: (_, _) => const SizedBox(height: 10),
+1 -1
View File
@@ -122,7 +122,7 @@ class _RankingPageState extends State<RankingPage> {
child: ListView( child: ListView(
key: const Key('ranking-list'), key: const Key('ranking-list'),
controller: _scroll, controller: _scroll,
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.only(top: 4, bottom: 24), padding: const EdgeInsets.only(top: 4, bottom: 24),
children: top.isEmpty children: top.isEmpty
? [ ? [
+1 -1
View File
@@ -49,7 +49,7 @@ class _ReservePageState extends State<ReservePage> {
onRefresh: _refresh, onRefresh: _refresh,
child: ListView( child: ListView(
key: const Key('reserve-list'), key: const Key('reserve-list'),
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.fromLTRB(0, 4, 0, 24), padding: const EdgeInsets.fromLTRB(0, 4, 0, 24),
children: [ children: [
AppSubpageHeader( AppSubpageHeader(
+1 -1
View File
@@ -120,7 +120,7 @@ class _WalletPageState extends State<WalletPage> {
return AppPullToRefresh( return AppPullToRefresh(
onRefresh: wallet.load, onRefresh: wallet.load,
child: ListView( child: ListView(
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.fromLTRB(16, 8, 16, 28), padding: const EdgeInsets.fromLTRB(16, 8, 16, 28),
children: [ children: [
_BalanceHero(wallet: snapshot.wallet), _BalanceHero(wallet: snapshot.wallet),
@@ -149,7 +149,7 @@ class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
return AppPullToRefresh( return AppPullToRefresh(
onRefresh: controller.load, onRefresh: controller.load,
child: ListView( child: ListView(
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
padding: const EdgeInsets.fromLTRB( padding: const EdgeInsets.fromLTRB(
_pageHorizontal, _pageHorizontal,
4, 4,
+8 -5
View File
@@ -26,7 +26,7 @@ void main() {
}, },
child: ListView( child: ListView(
key: const Key('pull-refresh-list'), key: const Key('pull-refresh-list'),
physics: const AlwaysScrollableScrollPhysics(), physics: AppPullToRefresh.physics,
children: const [SizedBox(height: 1200)], children: const [SizedBox(height: 1200)],
), ),
), ),
@@ -34,14 +34,17 @@ void main() {
), ),
); );
final gesture = await tester.startGesture( final list = find.byKey(const Key('pull-refresh-list'));
tester.getCenter(find.byKey(const Key('pull-refresh-list'))), final gesture = await tester.startGesture(tester.getCenter(list));
);
await gesture.moveBy(const Offset(0, 30)); await gesture.moveBy(const Offset(0, 30));
await tester.pump(); 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(find.text('Pull to refresh'), findsOneWidget);
await gesture.moveBy(const Offset(0, 40)); await gesture.moveBy(const Offset(0, 120));
await tester.pump(); await tester.pump();
expect(find.text('Release to refresh'), findsOneWidget); expect(find.text('Release to refresh'), findsOneWidget);
+5 -1
View File
@@ -300,7 +300,11 @@ void main() {
); );
expect(refresh.edgeOffset, 52); expect(refresh.edgeOffset, 52);
await tester.drag(_verticalFeed, const Offset(0, 120)); expect(
tester.widget<ListView>(_verticalFeed).physics,
AppPullToRefresh.physics,
);
await tester.drag(_verticalFeed, const Offset(0, 240));
await tester.pump(); await tester.pump();
expect(find.text('Refreshing…'), findsOneWidget); expect(find.text('Refreshing…'), findsOneWidget);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
+14 -12
View File
@@ -6,6 +6,7 @@ import 'package:fruitshorts/app.dart';
import 'package:fruitshorts/core/data/models/drama.dart'; import 'package:fruitshorts/core/data/models/drama.dart';
import 'package:fruitshorts/core/data/models/home_feed.dart'; import 'package:fruitshorts/core/data/models/home_feed.dart';
import 'package:fruitshorts/core/theme/app_colors.dart'; import 'package:fruitshorts/core/theme/app_colors.dart';
import 'package:fruitshorts/core/widgets/app_pull_to_refresh.dart';
import 'package:fruitshorts/core/data/repositories/favorite_repository.dart'; import 'package:fruitshorts/core/data/repositories/favorite_repository.dart';
import 'package:fruitshorts/core/data/repositories/playback_repository.dart'; import 'package:fruitshorts/core/data/repositories/playback_repository.dart';
import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart'; import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart';
@@ -181,7 +182,9 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
final list = find.byKey(const Key('ranking-list')); final list = find.byKey(const Key('ranking-list'));
await tester.drag(list, const Offset(0, -8000)); expect(tester.widget<ListView>(list).physics, AppPullToRefresh.physics);
final position = _scrollPosition(tester, const Key('ranking-list'));
position.jumpTo(position.maxScrollExtent);
await tester.pump(); await tester.pump();
expect(find.byKey(const Key('ranking-row-19')), findsOneWidget); expect(find.byKey(const Key('ranking-row-19')), findsOneWidget);
@@ -191,23 +194,22 @@ void main() {
expect(find.byKey(const Key('ranking-load-more')), findsOneWidget); expect(find.byKey(const Key('ranking-load-more')), findsOneWidget);
await tester.pump(const Duration(milliseconds: 1)); await tester.pump(const Duration(milliseconds: 1));
await tester.drag(list, const Offset(0, -8000)); position.jumpTo(position.maxScrollExtent);
await tester.pump(); await tester.pump();
expect(find.byKey(const Key('ranking-row-39')), findsOneWidget); expect(find.byKey(const Key('ranking-row-39')), findsOneWidget);
expect(find.byKey(const Key('ranking-row-40')), findsNothing); expect(find.byKey(const Key('ranking-row-40')), findsNothing);
expect(find.byKey(const Key('ranking-no-more')), findsOneWidget); expect(find.byKey(const Key('ranking-no-more')), findsOneWidget);
final position = _scrollPosition(tester, const Key('ranking-list'));
position.jumpTo(0); position.jumpTo(0);
await tester.pump(); await tester.pump();
final beforeRefresh = repository.fetchCalls; final beforeRefresh = repository.fetchCalls;
await tester.drag(list, const Offset(0, 120)); await tester.drag(list, const Offset(0, 240));
await tester.pump(); await tester.pump();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(repository.fetchCalls, beforeRefresh + 1); expect(repository.fetchCalls, beforeRefresh + 1);
expect(position.pixels, 0); expect(position.pixels, 0);
await tester.drag(list, const Offset(0, -8000)); position.jumpTo(position.maxScrollExtent);
await tester.pump(); await tester.pump();
expect(find.byKey(const Key('ranking-load-more')), findsOneWidget); expect(find.byKey(const Key('ranking-load-more')), findsOneWidget);
expect(find.byKey(const Key('ranking-no-more')), findsNothing); expect(find.byKey(const Key('ranking-no-more')), findsNothing);
@@ -223,11 +225,13 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
final list = find.byKey(const Key('new-releases-list')); final list = find.byKey(const Key('new-releases-list'));
expect(tester.widget<ListView>(list).physics, AppPullToRefresh.physics);
await tester.drag(list, const Offset(0, -400)); await tester.drag(list, const Offset(0, -400));
await tester.pump(); await tester.pump();
expect(_countKeys('home-drama-card-'), 20); expect(_countKeys('home-drama-card-'), 20);
await tester.drag(list, const Offset(0, -8000)); final position = _scrollPosition(tester, const Key('new-releases-list'));
position.jumpTo(position.maxScrollExtent);
await tester.pump(); await tester.pump();
expect(find.byKey(const Key('new-releases-load-more')), findsOneWidget); expect(find.byKey(const Key('new-releases-load-more')), findsOneWidget);
await tester.pump(const Duration(milliseconds: 299)); await tester.pump(const Duration(milliseconds: 299));
@@ -237,7 +241,6 @@ void main() {
expect(_countKeys('home-drama-card-'), 40); expect(_countKeys('home-drama-card-'), 40);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
final position = _scrollPosition(tester, const Key('new-releases-list'));
position.jumpTo(position.maxScrollExtent); position.jumpTo(position.maxScrollExtent);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.byKey(const Key('new-releases-no-more')), findsOneWidget); expect(find.byKey(const Key('new-releases-no-more')), findsOneWidget);
@@ -245,7 +248,7 @@ void main() {
position.jumpTo(0); position.jumpTo(0);
await tester.pump(); await tester.pump();
final beforeRefresh = repository.fetchCalls; final beforeRefresh = repository.fetchCalls;
await tester.drag(list, const Offset(0, 120)); await tester.drag(list, const Offset(0, 240));
await tester.pump(); await tester.pump();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(repository.fetchCalls, beforeRefresh + 1); expect(repository.fetchCalls, beforeRefresh + 1);
@@ -263,10 +266,9 @@ void main() {
await tester.pumpAndSettle(); await tester.pumpAndSettle();
final beforeRefresh = repository.fetchCalls; final beforeRefresh = repository.fetchCalls;
await tester.drag( final list = find.byKey(const Key('reserve-list'));
find.byKey(const Key('reserve-list')), expect(tester.widget<ListView>(list).physics, AppPullToRefresh.physics);
const Offset(0, 120), await tester.drag(list, const Offset(0, 240));
);
await tester.pump(); await tester.pump();
await tester.pumpAndSettle(); await tester.pumpAndSettle();
+5 -1
View File
@@ -75,7 +75,11 @@ void main() {
expect(find.byIcon(Icons.star_rounded), findsNWidgets(2)); expect(find.byIcon(Icons.star_rounded), findsNWidgets(2));
expect(find.byIcon(Icons.favorite_rounded), findsNothing); expect(find.byIcon(Icons.favorite_rounded), findsNothing);
expect(find.byType(AppPullToRefresh), findsOneWidget); expect(find.byType(AppPullToRefresh), findsOneWidget);
await tester.drag(find.byType(ListView), const Offset(0, 120)); expect(
tester.widget<ListView>(find.byType(ListView)).physics,
AppPullToRefresh.physics,
);
await tester.drag(find.byType(ListView), const Offset(0, 240));
await tester.pump(); await tester.pump();
expect(find.text('Refreshing…'), findsOneWidget); expect(find.text('Refreshing…'), findsOneWidget);
await tester.pumpAndSettle(); await tester.pumpAndSettle();
+1 -1
View File
@@ -85,7 +85,7 @@ void main() {
matching: find.byType(ListView), matching: find.byType(ListView),
), ),
); );
expect(list.physics, isA<AlwaysScrollableScrollPhysics>()); expect(list.physics, AppPullToRefresh.physics);
}); });
testWidgets( testWidgets(