[英]ListView - Only allow double-finger scrolling and disable single-finger one (Flutter)
我正在使用 Flutter ListView,并且只在用户使用两个手指时才需要让用户滚动。 如果用户只使用一根手指,我应该禁用滚动。
我不知道如何实现这一点:/我尝试过的:我尝试对 ScrollPhysics 进行子类化并重写 shouldAcceptUserOffset 或 applyPhysicsToUserOffset,但没有任何效果。 我也尝试通读文档,但没有找到任何相关内容。
非常感谢您的任何建议!
挖了Flutter源码(虽然代码很多,但是看起来很漂亮),终于找到了一个hack。 这适用于我的 iOS 模拟器和 Android 手机。
评论:
onlyScrollableWhenMultiFinger: true
),则在下面的示例中为MultiFingerListViewController
设置onlyScrollableWhenMultiFinger: true
。使用步骤:
MultiFingerListViewParent
作为列表视图的父级。physics
特性。样本:
class TestDoubleFingerScrollV2 extends StatefulWidget {
@override
_TestDoubleFingerScrollV2State createState() => _TestDoubleFingerScrollV2State();
}
class _TestDoubleFingerScrollV2State extends State<TestDoubleFingerScrollV2> {
final controller = MultiFingerListViewController(onlyScrollableWhenMultiFinger: true);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('TestDoubleFingerScrollV2')),
body: MultiFingerListViewParent(
controller: controller,
child: ListView.builder(
physics: MultiFingerScrollPhysics(controller: controller),
itemCount: 100,
itemBuilder: (context, index) =>
Container(height: 60, color: Colors.green.withAlpha((index * 50) % 255), child: Text('$index'))),
));
}
}
来源:
import 'package:flutter/material.dart';
import 'package:flutter_proj/components/finger_aware_listener.dart';
import 'package:flutter_proj/components/mux_listener.dart';
class MultiFingerListViewController {
final bool onlyScrollableWhenMultiFinger;
final _fingerAwareListener = FingerAwareListener();
bool _lastShouldScrollable = false;
MultiFingerListViewController({@required this.onlyScrollableWhenMultiFinger});
bool _updateShouldScrollable() {
_lastShouldScrollable =
(!onlyScrollableWhenMultiFinger) || _fingerAwareListener.info.isCurrentRunMultiFingerUpToNow;
return _lastShouldScrollable;
}
}
class MultiFingerListViewParent extends StatelessWidget {
final MultiFingerListViewController controller;
final Widget child;
const MultiFingerListViewParent({Key key, this.child, this.controller}) : super(key: key);
@override
Widget build(BuildContext context) {
return MuxListener(
onPointer: controller._fingerAwareListener.handlePointer,
child: child,
);
}
}
// This sub-class should have some boilerplate code, e.g. look at [NeverScrollableScrollPhysics] to see it
/// NOTE (WARN): This is a HACK, VIOLATING what the comments said for `applyPhysicsToUserOffset`. But works well for me.
class MultiFingerScrollPhysics extends ScrollPhysics {
final MultiFingerListViewController controller;
const MultiFingerScrollPhysics({ScrollPhysics parent, @required this.controller}) : super(parent: parent);
@override
MultiFingerScrollPhysics applyTo(ScrollPhysics ancestor) {
return MultiFingerScrollPhysics(controller: controller, parent: buildParent(ancestor));
}
/// NOTE This **HACK** is actually **VIOLATING** what the comment says!
/// The comment in [ScrollPhysics.applyPhysicsToUserOffset] says:
/// "This method must not adjust parts of the offset that are entirely within
/// the bounds described by the given `position`."
/// In addition, when looking at [BouncingScrollPhysics.applyPhysicsToUserOffset],
/// we see they directly return the original `offset` when `!position.outOfRange`
double applyPhysicsToUserOffset(ScrollMetrics position, double offset) {
if (controller._updateShouldScrollable()) {
// When k fingers are dragging, the speed is actually *k* times the normal speed. So we divide it by k.
// (see https://github.com/flutter/flutter/issues/11884)
final currNumFinger = controller._fingerAwareListener.info.currentRunSeenPointers.length;
return offset / currNumFinger;
} else {
return 0.0;
}
}
@override
Simulation createBallisticSimulation(ScrollMetrics position, double velocity) {
// When this method is called, the fingers seem to all *have left* the screen. Thus we cannot calculate the
// current information, but should use the previous cache.
if (controller._lastShouldScrollable) {
return super.createBallisticSimulation(position, velocity);
} else {
return null;
}
}
}
它依赖于其他几个小组件(非常短的代码),我把它们放在这里。
希望能帮助到你! 如果您有更好的解决方案,请告诉我:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.