Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设置refreshOnStart为true,首次进入页面无法下拉刷新 #874

Open
Xwnine opened this issue Nov 18, 2024 · 2 comments
Open

设置refreshOnStart为true,首次进入页面无法下拉刷新 #874

Xwnine opened this issue Nov 18, 2024 · 2 comments

Comments

@Xwnine
Copy link

Xwnine commented Nov 18, 2024

设置了 refreshOnStart 为true,首次进入页面不刷新。代码取自您这边的example 中nested_scroll_view.dart,您可以在nested_scroll_view.dart 中做如下尝试:

`import 'dart:async';

import 'package:example/widget/skeleton_item.dart';
import 'package:flutter/material.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:extended_nested_scroll_view/extended_nested_scroll_view.dart';

class NestedScrollViewPage extends StatefulWidget {
const NestedScrollViewPage({super.key});

@OverRide
NestedScrollViewPageState createState() {
return NestedScrollViewPageState();
}
}

class NestedScrollViewPageState extends State
with SingleTickerProviderStateMixin {
late TabController _tabController;

@OverRide
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this);
}

@OverRide
void dispose() {
super.dispose();
_tabController.dispose();
}

@OverRide
Widget build(BuildContext context) {
final themeData = Theme.of(context);
return Scaffold(
body: ExtendedNestedScrollView(
onlyOneScrollInBody: true,
pinnedHeaderSliverHeightBuilder: () {
return MediaQuery.of(context).padding.top + kToolbarHeight;
},
headerSliverBuilder: (context, innerBoxIsScrolled) {
return [
SliverAppBar(
expandedHeight: 120,
pinned: true,
flexibleSpace: FlexibleSpaceBar(
title: Text(
'NestedScrollView',
style: TextStyle(color: Theme.of(context).textTheme.titleLarge?.color),
),
centerTitle: false,
),
),
SliverPersistentHeader(
pinned: true,
delegate: SliverStickHeaderDelegate(
height: 44,
child: Container(
color: Theme.of(context).secondaryHeaderColor,
child: TabBar(
controller: _tabController,
labelColor: themeData.colorScheme.primary,
indicatorColor: themeData.colorScheme.primary,
tabs: const [
Tab(text: 'List 1'),
Tab(text: 'List 2'),
Tab(text: 'List 3'),
],
),
),
),
),
];
},
body: TabBarView(controller: _tabController, children: const [
AutoKeepAliveWrapper(child: NestedRefreshList(tabIndex: 1)),
AutoKeepAliveWrapper(child: NestedRefreshList(tabIndex: 1)),
AutoKeepAliveWrapper(child: NestedRefreshList(tabIndex: 2)),
]),
),
);
}
}

class NestedRefreshList extends StatefulWidget {
const NestedRefreshList({super.key, required this.tabIndex});
final int tabIndex;
@OverRide
State createState() => _NestedRefreshListState();
}

class _NestedRefreshListState extends State {
int _listCount = 20;
late EasyRefreshController _controller;

@OverRide
void initState() {
_controller = EasyRefreshController();
///尝试手动刷新,未生效
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_controller.callRefresh();
});

super.initState();
}

@OverRide
Widget build(BuildContext context) {
return ExtendedVisibilityDetector(
uniqueKey: const Key('Tab0'),
child: EasyRefresh(
controller: _controller,
refreshOnStart: true, //首次进入页面也无法刷新
header: const MaterialHeader(
position: IndicatorPosition.locator,
),
footer: const MaterialFooter(
position: IndicatorPosition.locator,
),
child: CustomScrollView(
slivers: [
const HeaderLocator.sliver(clearExtent: false),
SliverToBoxAdapter(child: Text('${widget.tabIndex}')),
SliverList(
delegate: SliverChildBuilderDelegate((context, index) {
return const SkeletonItem();
}, childCount: _listCount)),
const FooterLocator.sliver(clearExtent: false),
],
),
onRefresh: () async {
await Future.delayed(const Duration(seconds: 2), () {
if (mounted) {
setState(() {
_listCount = 20;
});
}
});
},
onLoad: () async {
await Future.delayed(const Duration(seconds: 2), () {
if (mounted) {
setState(() {
_listCount += 10;
});
}
});
},
),
);
}
}

class AutoKeepAliveWrapper extends StatefulWidget {
const AutoKeepAliveWrapper({
super.key,
this.keepAlive = true,
required this.child,
});

final bool keepAlive;
final Widget child;

@OverRide
State createState() => _AutoKeepAliveWrapperState();
}

class _AutoKeepAliveWrapperState extends State
with AutomaticKeepAliveClientMixin {
@OverRide
Widget build(final BuildContext context) {
super.build(context);
return widget.child;
}

@OverRide
void didUpdateWidget(covariant final AutoKeepAliveWrapper oldWidget) {
if (oldWidget.keepAlive != widget.keepAlive) {
updateKeepAlive();
}
super.didUpdateWidget(oldWidget);
}

@OverRide
bool get wantKeepAlive => widget.keepAlive;
}

class SliverStickHeaderDelegate extends SliverPersistentHeaderDelegate {
SliverStickHeaderDelegate({required this.child, this.height});

final double? height;
final Widget child;

@OverRide
Widget build(final BuildContext context, final double shrinkOffset, final bool overlapsContent) {
return Container(
alignment: Alignment.topCenter,
child: child,
);
}

@OverRide
double get maxExtent => height ?? 45;

@OverRide
double get minExtent => height ?? 45;

@OverRide
bool shouldRebuild(final SliverPersistentHeaderDelegate oldDelegate) {
return true;
}
}
`

@OICQ469
Copy link

OICQ469 commented Nov 21, 2024

同样的问题

@zhiye1995
Copy link

zhiye1995 commented Nov 30, 2024

refreshOnStart: true和存在refreshOnStartHeader, 必须下上拉一下,才能下拉刷新,上拉的第一次会有个停顿不滚动,用作者的apk也有这种情况,作者可以反复进入“启动时刷新”尝试一下
在web上,数据出来的同时立马下拉就会这样

Clipchamp.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants