Reputation: 11
I'm working on a Flutter app where users can scroll between categories by pulling up or down. My implementation allows switching categories when the user performs a "hard pull." However, I have two main issues:
I’m currently using a NotificationListener
to detect overscroll and move between categories. Here's the relevant part of my code:
void moveToNextCategory() {
if (!isScrolling && selectedTabIndex < tabs.length - 1) {
setState(() {
isScrolling = true;
selectedTabIndex++;
});
Future.delayed(const Duration(milliseconds: 300), () {
isScrolling = false; // Reset scrolling flag
});
}
}
void moveToPreviousCategory() {
if (!isScrolling && selectedTabIndex > 0) {
setState(() {
isScrolling = true;
selectedTabIndex--;
});
Future.delayed(const Duration(milliseconds: 300), () {
isScrolling = false; // Reset scrolling flag
});
}
}
void showTopGlow() {
setState(() {
isTopGlowVisible = true;
});
}
void showBottomGlow() {
setState(() {
isBottomGlowVisible = true;
});
}
void hideGlow() {
setState(() {
isTopGlowVisible = false;
isBottomGlowVisible = false;
});
}
In the NotificationListener
, I'm checking for overscroll events and triggering the category change or glow as follows:
NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification scroll) {
const double hardPullThreshold = 50.0; // Threshold for hard pull velocity
if (scroll is OverscrollNotification) {
if (scroll.overscroll > 0 &&
scroll.metrics.pixels == scroll.metrics.maxScrollExtent) {
showBottomGlow(); // Show custom glow
if (scroll.dragDetails?.primaryDelta != null &&
scroll.dragDetails!.primaryDelta!.abs() < hardPullThreshold) {
moveToNextCategory();
}
} else if (scroll.overscroll < 0 &&
scroll.metrics.pixels == scroll.metrics.minScrollExtent) {
showTopGlow(); // Show custom glow
if (scroll.dragDetails?.primaryDelta != null &&
scroll.dragDetails!.primaryDelta!.abs() < hardPullThreshold) {
moveToPreviousCategory();
}
}
} else if (scroll is ScrollEndNotification) {
hideGlow(); // Hide glow when scrolling stops
}
return false; // Allow default behavior
},
)
Duration
in Future.delayed
, but this didn’t feel smooth.CustomGlowIndicator
), but it still doesn’t look as good as the effect in the original design.Upvotes: 0
Views: 54