[英]How to disable drag scrolling but keep mouse wheel scrolling for ListView in Flutter MacOS?
So when I set physics: const NeverScrollableScrollPhysics()
on ListView.builder
, it also disables mouse wheel scrolling.因此,当我在
ListView.builder
上设置physics: const NeverScrollableScrollPhysics()
时,它也会禁用鼠标滚轮滚动。 Is there a way to keep mouse wheel but disable drag?有没有办法保持鼠标滚轮但禁用拖动?
Thanks谢谢
Thanks for the answer (@HasilT), it helped!感谢您的回答(@HasilT),它有帮助! I found that
PointerScrollEvent.scrollDelta
is already "animated", and you can use it with ScrollController.jumpTo
.我发现
PointerScrollEvent.scrollDelta
已经“动画化”了,您可以将它与ScrollController.jumpTo
一起使用。 In my code, I prevent overscroll with min
and max
functions.在我的代码中,我使用
min
和max
函数防止过度滚动。
import 'dart:math' as math;
Listener(
onPointerSignal: (ps) {
if (ps is PointerScrollEvent) {
final newOffset = _controller.offset + ps.scrollDelta.dy;
if (ps.scrollDelta.dy.isNegative) {
_controller.jumpTo(math.max(0, newOffset));
} else {
_controller
.jumpTo(math.min(_controller.position.maxScrollExtent, newOffset));
}
}
},
child: ListView.builder(
itemCount: 100,
controller: _controller,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
return Container(
height: 100,
margin: EdgeInsets.all(10),
color: index.isEven ? Colors.blue : Colors.green);
},
),
);
You can use a Listener
widget to watch for drag events happening on your ListView
and manually scroll the ListView
to desired offset using ScrollController
attached to that ListView
.您可以使用
Listener
小部件来监视ListView
上发生的拖动事件,并使用附加到该ListView
的ScrollController
手动将ListView
滚动到所需的偏移量。
Here is a sample code:这是一个示例代码:
Listener(
onPointerSignal: (ps) {
if (ps is PointerScrollEvent) {
_controller.animateTo(ps.position.dy,
duration: Duration(milliseconds: 500),
curve: Curves.linear);
}
},
child: ListView.builder(
itemCount: 100,
controller: _controller,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (BuildContext context, int index) {
return Container(
height: 100,
margin: EdgeInsets.all(10),
color: index.isEven ? Colors.blue : Colors.green);
}))
And here is a link to a working demo ( You may have to play around with listener events to make the scrolling perfect, this is just an example)这是一个工作演示的链接(您可能必须使用侦听器事件才能使滚动完美,这只是一个示例)
Feature request has been created for this case: https://github.com/flutter/flutter/issues/71322已为此案例创建功能请求: https://github.com/flutter/flutter/issues/71322
Just wrap in a widget @truthmast's solution只需包装一个小部件@truthmast 的解决方案
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:responsive_builder/responsive_builder.dart';
class DragPreventingSingleChildScrollView extends StatelessWidget {
final _controller = ScrollController();
final Widget child;
DragPreventingSingleChildScrollView({Key key, this.child}) : super(key: key);
@override
Widget build(BuildContext context) => ResponsiveBuilder(
builder: (c, si) => !si.isDesktop
? SingleChildScrollView(child: child)
: Listener(
onPointerSignal: (ps) {
if (ps is PointerScrollEvent) {
final newOffset = _controller.offset + ps.scrollDelta.dy;
if (ps.scrollDelta.dy.isNegative) {
_controller.jumpTo(math.max(0, newOffset));
} else {
_controller.jumpTo(math.min(
_controller.position.maxScrollExtent, newOffset));
}
}
},
child: SingleChildScrollView(
controller: _controller,
physics: NeverScrollableScrollPhysics(),
child: child,
),
),
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.