If there is a way to implement automatically scrolling the SliverAppBar to a collapsed or expanded state at the moment the user stops scrolling in an intermediate position (between collapsedHeight and expandedHeight of SliverAppBar).

Not sure if this is the best approach, but it works:
final _controller = ScrollController();
...
Scaffold(
body: NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
if (scrollNotification is ScrollEndNotification &&
scrollNotification.depth == 0) {
final minExtent = scrollNotification.metrics.minScrollExtent;
final maxExtent = scrollNotification.metrics.maxScrollExtent;
final middle = (maxExtent - minExtent) / 2;
final pos = scrollNotification.metrics.pixels;
double scrollTo;
if (minExtent < pos && pos <= middle)
scrollTo = minExtent;
else if (middle < pos && pos < maxExtent) scrollTo = maxExtent;
if (scrollTo != null)
// Doesn't work without Timer
Timer(
Duration(milliseconds: 1),
() => _controller.animateTo(scrollTo,
duration: Duration(milliseconds: 300),
curve: Curves.ease));
}
return false;
},
child: NestedScrollView(
controller: _controller,
...
working example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With