improved support for disable animations
This commit is contained in:
@@ -44,43 +44,59 @@ ValueNotifier<bool> useScrollVisibility(
|
||||
double hideThreshold = 5.0,
|
||||
double showThreshold = 5.0,
|
||||
Duration initializationDelay = const Duration(milliseconds: 1000),
|
||||
bool disableAnimations = false,
|
||||
}) {
|
||||
final isVisible = useState(true);
|
||||
final lastScrollOffset = useRef(0.0);
|
||||
final isInitialized = useRef(false);
|
||||
|
||||
useEffect(() {
|
||||
// Start timer to enable scroll listener after initialization delay
|
||||
final timer = Timer(initializationDelay, () {
|
||||
isInitialized.value = true;
|
||||
});
|
||||
useEffect(
|
||||
() {
|
||||
// Start timer to enable scroll listener after initialization delay
|
||||
final timer = Timer(
|
||||
disableAnimations ? Duration.zero : initializationDelay,
|
||||
() {
|
||||
if (scrollController.hasClients) {
|
||||
lastScrollOffset.value = scrollController.offset;
|
||||
}
|
||||
isInitialized.value = true;
|
||||
},
|
||||
);
|
||||
|
||||
void scrollListener() {
|
||||
// Ignore scroll events until initialization is complete
|
||||
if (!isInitialized.value) return;
|
||||
void scrollListener() {
|
||||
// Ignore scroll events until initialization is complete
|
||||
if (!isInitialized.value) return;
|
||||
|
||||
final currentOffset = scrollController.offset;
|
||||
final difference = currentOffset - lastScrollOffset.value;
|
||||
final currentOffset = scrollController.offset;
|
||||
final difference = currentOffset - lastScrollOffset.value;
|
||||
|
||||
// Hide when scrolling down beyond threshold
|
||||
if (difference > hideThreshold && isVisible.value) {
|
||||
isVisible.value = false;
|
||||
}
|
||||
// Show when scrolling up beyond threshold or at top
|
||||
else if ((difference < -showThreshold || currentOffset <= 0) &&
|
||||
!isVisible.value) {
|
||||
isVisible.value = true;
|
||||
// Hide when scrolling down beyond threshold
|
||||
if (difference > hideThreshold && isVisible.value) {
|
||||
isVisible.value = false;
|
||||
}
|
||||
// Show when scrolling up beyond threshold or at top
|
||||
else if ((difference < -showThreshold || currentOffset <= 0) &&
|
||||
!isVisible.value) {
|
||||
isVisible.value = true;
|
||||
}
|
||||
|
||||
lastScrollOffset.value = currentOffset;
|
||||
}
|
||||
|
||||
lastScrollOffset.value = currentOffset;
|
||||
}
|
||||
|
||||
scrollController.addListener(scrollListener);
|
||||
return () {
|
||||
timer.cancel();
|
||||
scrollController.removeListener(scrollListener);
|
||||
};
|
||||
}, [scrollController, hideThreshold, showThreshold, initializationDelay]);
|
||||
scrollController.addListener(scrollListener);
|
||||
return () {
|
||||
timer.cancel();
|
||||
scrollController.removeListener(scrollListener);
|
||||
};
|
||||
},
|
||||
[
|
||||
scrollController,
|
||||
hideThreshold,
|
||||
showThreshold,
|
||||
initializationDelay,
|
||||
disableAnimations,
|
||||
],
|
||||
);
|
||||
|
||||
return isVisible;
|
||||
}
|
||||
|
||||
@@ -24,14 +24,19 @@ void useSyncPageWithTab(
|
||||
TabController tabController,
|
||||
PageController pageController, {
|
||||
void Function(int index)? onIndexChanged,
|
||||
bool disableAnimations = false,
|
||||
}) {
|
||||
useEffect(() {
|
||||
Future<void> syncPage() async {
|
||||
await pageController.animateToPage(
|
||||
tabController.index,
|
||||
curve: Curves.linear,
|
||||
duration: const Duration(milliseconds: 300),
|
||||
);
|
||||
if (disableAnimations) {
|
||||
pageController.jumpToPage(tabController.index);
|
||||
} else {
|
||||
await pageController.animateToPage(
|
||||
tabController.index,
|
||||
curve: Curves.linear,
|
||||
duration: const Duration(milliseconds: 300),
|
||||
);
|
||||
}
|
||||
onIndexChanged?.call(tabController.index);
|
||||
}
|
||||
|
||||
@@ -39,7 +44,10 @@ void useSyncPageWithTab(
|
||||
if (!tabController.indexIsChanging) {
|
||||
final index = pageController.page!.round();
|
||||
|
||||
tabController.animateTo(index);
|
||||
tabController.animateTo(
|
||||
index,
|
||||
duration: disableAnimations ? Duration.zero : kTabScrollDuration,
|
||||
);
|
||||
onIndexChanged?.call(index);
|
||||
}
|
||||
}
|
||||
@@ -51,5 +59,5 @@ void useSyncPageWithTab(
|
||||
tabController.removeListener(syncPage);
|
||||
pageController.removeListener(syncTab);
|
||||
};
|
||||
}, [tabController, pageController]);
|
||||
}, [tabController, pageController, disableAnimations]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user