我正在使用Flutter 2.0开发Windows桌面应用程序。使用鼠标滚轮滚动列表感觉有些乏味,该如何设置每滚动一格添加一个项目?
我曾经遇到过同样的问题,这里是我找到的解决办法。
在你的小部件中,你有一个滚动元素(可能是一个列表,或者在我的情况下是SingleChildScrollView
),添加一个ScrollController
并添加一个监听器:
class ScrollViewTest extends StatelessWidget
{
static const _extraScrollSpeed = 80; // your "extra" scroll speed
final ScrollController _scrollController = ScrollController();
// Constructor.
ScrollViewTest({Key? key}) : super(key: key)
{
_scrollController.addListener(() {
ScrollDirection scrollDirection = _scrollController.position.userScrollDirection;
if (scrollDirection != ScrollDirection.idle)
{
double scrollEnd = _scrollController.offset + (scrollDirection == ScrollDirection.reverse
? _extraScrollSpeed
: -_extraScrollSpeed);
scrollEnd = min(
_scrollController.position.maxScrollExtent,
max(_scrollController.position.minScrollExtent, scrollEnd));
_scrollController.jumpTo(scrollEnd);
}
});
}
@override
Widget build(BuildContext context)
{
return SingleChildScrollView(
controller: _scrollController,
child: Container(...),
);
}
}