[英]Flutter DragTarget on hover
When exploring the Flutter's DragTarget class I could not find any mechanism to hook to the 'on hover event' if you will.在探索 Flutter 的 DragTarget 类时,如果你愿意的话,我找不到任何机制来挂钩“悬停事件”。 Here is an example:
这是一个例子:
class DropZone extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Container(
height: 40,
color: Colors.green,
child: DragTarget(
builder: (context, List<int> candidateData, rejectedData) {
print("dragged");
return new Container(color: Colors.red,);
},
onWillAccept: (data) {
return true;
},),
);
}
}
I would like to change the color of the container when a Draggable object is hovered but not yet dropped.当 Draggable 对象悬停但尚未放下时,我想更改容器的颜色。 The builder method, in this case, only executes during the initial rendering and when the Draggable leaves the target.
在这种情况下,builder 方法仅在初始渲染期间和 Draggable 离开目标时执行。 I suspect I have to do something in the onWillAccept method but not sure what.
我怀疑我必须在 onWillAccept 方法中做一些事情,但不确定是什么。 Does anybody have a solution?
有人有解决方案吗?
here is the code for what I think you're looking for这是我认为您正在寻找的代码
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: App(),
),
);
}
}
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
Color caughtColor = Colors.grey;
@override
Widget build(BuildContext context) {
return Stack(
children: <Widget>[
DragBox(Offset(0.0, 0.0), Colors.green),
Positioned(
left: 100,
bottom: 0.0,
child: DragTarget(onAccept: (Color color) {
caughtColor = color;
}, builder: (
BuildContext context,
List<dynamic> accepted,
List<dynamic> rejected,
) {
return Container(
width: 200,
height: 200,
color: accepted.isEmpty ? caughtColor : Colors.grey.shade200,
);
}))
],
);
}
}
class DragBox extends StatefulWidget {
final Offset initPos;
final Color itemColor;
DragBox(this.initPos, this.itemColor);
@override
_DragBoxState createState() => _DragBoxState();
}
class _DragBoxState extends State<DragBox> {
Offset position = Offset(0.0, 0.0);
@override
void initState() {
super.initState();
position = widget.initPos;
}
@override
Widget build(BuildContext context) {
return Positioned(
left: position.dx,
top: position.dy,
child: Draggable(
data: widget.itemColor,
child: Container(
width: 100,
height: 100,
color: widget.itemColor,
),
onDraggableCanceled: (velocity, offset) {
setState(() {
position = offset;
});
},
feedback: Container(
width: 120,
height: 120,
color: widget.itemColor.withOpacity(0.5),
),
),
);
}
}
You can make combination of onAccept, onMove and onLeave functions in DragTarget to achieve the result.您可以在 DragTarget 中组合 onAccept、onMove 和 onLeave 函数来实现结果。
Here is how you do it:这是您的操作方法:
// ...
bool isTargetGettingDroppedForDelete = false;
//...
DragTarget<String>(
builder: (
BuildContext context,
List<dynamic> accepted,
List<dynamic> rejected,
) {
return Container(
height: 100,
width: 100,
color: isTargetGettingDroppedForDelete ? Colors.red : Colors.green,
);
},
onWillAccept: (data) {
return data == Constants.draggable;
},
onAccept: (data) {
setState(() {
isTargetGettingDroppedForDelete = false;
});
},
onMove: (details) {
setState(() {
isTargetGettingDroppedForDelete = true;
});
},
onLeave: (data) {
setState(() {
isTargetGettingDroppedForDelete = false;
});
},
),
You can use the candidateData
parameter in the DragTarget.builder
:您可以在
candidateData
中使用DragTarget.builder
参数:
...
border: Border.all(
color: candidateData.isNotEmpty
? Colors.red
: Colors.black)),
The code above changes the border color to red
upon hover.上面的代码在悬停时将边框颜色更改为
red
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.