fix(flutter): 修复下拉刷新页面跟随
This commit is contained in:
1 parent
c4dcb714ab
commit
69eaacd74f
16 files changed
+85
-34
No files matched your search
@@ -9,6 +9,8 @@ import '../theme/app_colors.dart';
|
||||
|
||||
const double _maxPullDistance = 96;
|
||||
const double _refreshThreshold = 56;
|
||||
const double _indicatorHeight = 64;
|
||||
const double _indicatorMaxOffset = 10;
|
||||
const Duration _arrowTurnDuration = Duration(milliseconds: 160);
|
||||
const Duration _retractDuration = Duration(milliseconds: 200);
|
||||
|
||||
@@ -23,6 +25,9 @@ class AppPullToRefresh extends StatefulWidget {
|
||||
this.edgeOffset = 0,
|
||||
});
|
||||
|
||||
static const AlwaysScrollableScrollPhysics physics =
|
||||
AlwaysScrollableScrollPhysics(parent: BouncingScrollPhysics());
|
||||
|
||||
final Widget child;
|
||||
final Future<void> Function() onRefresh;
|
||||
final double edgeOffset;
|
||||
@@ -40,6 +45,12 @@ class _AppPullToRefreshState extends State<AppPullToRefresh> {
|
||||
if (notification.depth != 0 || _refreshing) return false;
|
||||
if (notification is ScrollStartNotification) {
|
||||
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) {
|
||||
if (notification.dragDetails != null) {
|
||||
_setPullDistance(_pullDistance - notification.overscroll);
|
||||
@@ -100,19 +111,46 @@ class _AppPullToRefreshState extends State<AppPullToRefresh> {
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
widget.child,
|
||||
Listener(
|
||||
onPointerUp: (_) => _release(),
|
||||
onPointerCancel: (_) => _release(),
|
||||
child: widget.child,
|
||||
),
|
||||
if (_state != _PullState.idle)
|
||||
Positioned(
|
||||
top: widget.edgeOffset,
|
||||
left: 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) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
if (_state == _PullState.refreshing) {
|
||||
|
||||
@@ -159,7 +159,7 @@ class _DiscoverPageState extends State<DiscoverPage> {
|
||||
onRefresh: categories.load,
|
||||
child: ListView(
|
||||
controller: logic.scroll,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: EdgeInsets.only(
|
||||
top: showHeader ? _stickyHeaderHeight : 0,
|
||||
bottom: AppDimens.spaceXl,
|
||||
|
||||
@@ -94,7 +94,7 @@ class _DownloadsBody extends StatelessWidget {
|
||||
return AppPullToRefresh(
|
||||
onRefresh: downloads.load,
|
||||
child: ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.fromLTRB(16, 10, 16, 28),
|
||||
children: [
|
||||
_PolicyPanel(downloads: downloads, policy: policy),
|
||||
|
||||
@@ -167,7 +167,7 @@ class _FavoritesBody extends StatelessWidget {
|
||||
return AppPullToRefresh(
|
||||
onRefresh: favorites.load,
|
||||
child: ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.fromLTRB(
|
||||
_pageHorizontal,
|
||||
4,
|
||||
|
||||
@@ -146,7 +146,7 @@ class _HomeBrowseViewState extends State<HomeBrowseView> {
|
||||
child: ListView(
|
||||
key: const Key('home-browse-page'),
|
||||
controller: _scroll,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.only(bottom: AppDimens.spaceXl),
|
||||
children: _sections(l10n, catalog),
|
||||
),
|
||||
|
||||
@@ -133,7 +133,7 @@ class _NewReleasesPageState extends State<NewReleasesPage> {
|
||||
child: ListView(
|
||||
key: const Key('new-releases-list'),
|
||||
controller: _scroll,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.only(bottom: 24),
|
||||
children: hero == null
|
||||
? [
|
||||
|
||||
@@ -69,7 +69,7 @@ class _NoticesPageState extends State<NoticesPage> {
|
||||
return AppPullToRefresh(
|
||||
onRefresh: _controller.refresh,
|
||||
child: ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
children: [
|
||||
SizedBox(
|
||||
height: MediaQuery.sizeOf(context).height * 0.7,
|
||||
@@ -94,7 +94,7 @@ class _NoticesPageState extends State<NoticesPage> {
|
||||
return AppPullToRefresh(
|
||||
onRefresh: _controller.refresh,
|
||||
child: ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
children: [
|
||||
SizedBox(
|
||||
height: MediaQuery.sizeOf(context).height * 0.7,
|
||||
@@ -113,7 +113,7 @@ class _NoticesPageState extends State<NoticesPage> {
|
||||
onRefresh: _controller.refresh,
|
||||
child: ListView.separated(
|
||||
key: const Key('notices-list'),
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 28),
|
||||
itemCount: _controller.notices.length + 1,
|
||||
separatorBuilder: (_, _) => const SizedBox(height: 10),
|
||||
|
||||
@@ -122,7 +122,7 @@ class _RankingPageState extends State<RankingPage> {
|
||||
child: ListView(
|
||||
key: const Key('ranking-list'),
|
||||
controller: _scroll,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.only(top: 4, bottom: 24),
|
||||
children: top.isEmpty
|
||||
? [
|
||||
|
||||
@@ -49,7 +49,7 @@ class _ReservePageState extends State<ReservePage> {
|
||||
onRefresh: _refresh,
|
||||
child: ListView(
|
||||
key: const Key('reserve-list'),
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.fromLTRB(0, 4, 0, 24),
|
||||
children: [
|
||||
AppSubpageHeader(
|
||||
|
||||
@@ -120,7 +120,7 @@ class _WalletPageState extends State<WalletPage> {
|
||||
return AppPullToRefresh(
|
||||
onRefresh: wallet.load,
|
||||
child: ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 28),
|
||||
children: [
|
||||
_BalanceHero(wallet: snapshot.wallet),
|
||||
|
||||
@@ -149,7 +149,7 @@ class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
|
||||
return AppPullToRefresh(
|
||||
onRefresh: controller.load,
|
||||
child: ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
physics: AppPullToRefresh.physics,
|
||||
padding: const EdgeInsets.fromLTRB(
|
||||
_pageHorizontal,
|
||||
4,
|
||||
|
||||
Reference in new issue
Block a user