Flutter 鼠标滚轮灵敏度

4

我正在使用Flutter 2.0开发Windows桌面应用程序。使用鼠标滚轮滚动列表感觉有些乏味,该如何设置每滚动一格添加一个项目?

1个回答

3

我曾经遇到过同样的问题,这里是我找到的解决办法。 在你的小部件中,你有一个滚动元素(可能是一个列表,或者在我的情况下是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(...),
    );
  }
}

1
只是确认一下。在有状态的小部件中也起作用了。 - Gad D Lord

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接