答案 0 :(得分:0)
不确定这是否是最好的方法,但是它可行:
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,
...