android - Flutter - 多个手势,无需抬起手指
问题描述
我正在尝试创建以下效果:当用户在空白屏幕上长按时,会出现一个矩形。在不抬起手指的情况下,我希望用户能够拖动矩形的边缘之一(例如,垂直)。
我可以分别实现这些效果(长按、释放、拖动),但我需要在不抬起手指的情况下拥有它们。
目前,我的代码如下所示:
@override
Widget build(BuildContext context) {
return GestureDetector(
onPanStart: startDrag,
onPanUpdate: onDrag,
onPanEnd: endDrag,
child: CustomPaint(
painter: BoxPainter(
color: BOX_COLOR,
boxPosition: boxPosition,
boxPositionOnStart: boxPositionOnStart ?? boxPosition,
touchPoint: point,
),
child: Container(),
),
);
}
这样就实现了边缘的拖动,基于本教程。
为了使元素出现在长按上,我使用了一个Opacity
小部件。
@override
Widget build(BuildContext context) {
return new GestureDetector(
onLongPress: () {
setState(() {
this.opacity = 1.0;
});
},
child: new Container(
width: width,
height: height,
child: new Opacity(
opacity: opacity,
child: PhysicsBox(
boxPosition: 0.5,
),
),
),
);
}
解决方案
如果有人仍然感兴趣,我可以使用DelayedMultiDragGestureRecognizer
该类实现所需的行为。
代码如下所示:
void onDrag(details) {
// Called on drag update
}
void onEndDrag(details) {
// Called on drag end
}
@override
Widget build(BuildContext context) {
return new RawGestureDetector(
gestures: <Type, GestureRecognizerFactory>{
DelayedMultiDragGestureRecognizer:
new GestureRecognizerFactoryWithHandlers<
DelayedMultiDragGestureRecognizer>(
() => new DelayedMultiDragGestureRecognizer(),
(DelayedMultiDragGestureRecognizer instance) {
instance
..onStart = (Offset offset) {
/* More code here */
return new ItemDrag(onDrag, endDrag);
};
},
),
},
);
}
ItemDrag
是一个扩展 Flutter 类的Drag
类:
class ItemDrag extends Drag {
final GestureDragUpdateCallback onUpdate;
final GestureDragEndCallback onEnd;
ItemDrag(this.onUpdate, this.onEnd);
@override
void update(DragUpdateDetails details) {
super.update(details);
onUpdate(details);
}
@override
void end(DragEndDetails details) {
super.end(details);
onEnd(details);
}
}
推荐阅读
- python - Django:带有PK问题的DetailView URL
- favicon - 生活网站图标 - 他们是如何做到的
- java - 如何使用 JPA 注释双 varchar 主键?
- c - 我无法将 argc 和 argv 从命令行获取到函数调用中
- php - 无法使用 httpClient 将字符串上传到我的 SSL 服务器
- mysql - 如何使用一个自动生成的值创建复合主键?
- node.js - Node.Js、express、res.sendFile 无法建立根目录
- python - 使用 re.match() 查找以两个可能的字母开头的字符串
- python - 在网络服务器上运行的 Flask 应用程序以调用远程服务器 python 模块
- javascript - 将大写字母转换为小写字母,将小写字母转换为大写字母