如果存在实现在用户停止在中间位置(在collapsedHeight和SliverAppBar的expandedHeight之间)滚动的时刻将SliverAppBar自动滚动到折叠或展开状态的方法。

发布于 2020-11-07 20:41:58
不确定这是否是最好的方法,但它是有效的:
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,
...https://stackoverflow.com/questions/64726368
复制相似问题