当我开始在 Flutter 中拖动 Widget 时如何显示 Icon
我想知道当我开始在 LongPressDraggable 小部件中拖动容器时,如何在屏幕底部显示删除图标
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => _onTap(context),
child: LongPressDraggable(
data: index,
maxSimultaneousDrags: 1,
onDragUpdate: (details) => print('update'),
onDragStarted: () => _buildDragTarget(),
onDragEnd: (_) => print('end'),
feedback: Material(
child: Container(
height: Sizes.height / 4.5,
width: Sizes.height / 4.5,
child: _DraggableContent(
index: index,
place: place,
),
),
),
childWhenDragging: Container(color: Colors.transparent),
child: _DraggableContent(
index: index,
place: place,
),
));
}
Widget _buildDragTarget() {
return DragTarget<int>(
builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
return Icon(Icons.delete);
},
onAcceptWithDetails: (DragTargetDetails<int> dragTargetDetails) {
print('onAcceptWithDetails');
print('Data: ${dragTargetDetails.data}');
print('Offset: ${dragTargetDetails.offset}');
},
);
}
目前,当我开始拖动项目时,会发生任何事情,我不知道如何继续
I want to know how can I show at the bottom of the screen a delete icon when I start dragging a Container in LongPressDraggable widget
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => _onTap(context),
child: LongPressDraggable(
data: index,
maxSimultaneousDrags: 1,
onDragUpdate: (details) => print('update'),
onDragStarted: () => _buildDragTarget(),
onDragEnd: (_) => print('end'),
feedback: Material(
child: Container(
height: Sizes.height / 4.5,
width: Sizes.height / 4.5,
child: _DraggableContent(
index: index,
place: place,
),
),
),
childWhenDragging: Container(color: Colors.transparent),
child: _DraggableContent(
index: index,
place: place,
),
));
}
Widget _buildDragTarget() {
return DragTarget<int>(
builder: (BuildContext context, List<int> data, List<dynamic> rejects) {
return Icon(Icons.delete);
},
onAcceptWithDetails: (DragTargetDetails<int> dragTargetDetails) {
print('onAcceptWithDetails');
print('Data: ${dragTargetDetails.data}');
print('Offset: ${dragTargetDetails.offset}');
},
);
}
At the moment, when I start dragging the item, anything happens and I don't know how to continue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在这里有一个可拖动小部件的示例: https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/
上删除项目部分https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/#:~:text=the%20tomato%20image.-,删除%20an%20item,-At%20this%20point
您需要在 DragTarget 小部件上使用 onAccept 事件:
在 Drag 开始时,您可以显示您的使用此事件删除图标 (https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/#:~:text=Listening%20to%20drag%20events) :
希望对你有帮助
You have an example with Dragable widget here : https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/
On the DROPPING AN ITEM part https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/#:~:text=the%20tomato%20image.-,Dropping%20an%20item,-At%20this%20point
You need to use the onAccept event on your DragTarget widget :
And on Drag starting, you can show up your delete icon by using this event (https://blog.logrocket.com/drag-and-drop-ui-elements-in-flutter-with-draggable-and-dragtarget/#:~:text=Listening%20to%20drag%20events) :
I hope it will help you
据我了解,当您拖动对象时,您需要在底部显示删除图标。
您可以使用 Visibility 小部件包裹删除图标,并根据拖动活动设置可见参数 true 或 false。
它会是这样的:
As far as I understood you need to show the delete icon on bottom when you are dragging an object.
You can wrap the delete icon with Visibility widget and set the visible parameter true or false based on the drag activity.
It will go something like this:
我找到了解决方案。它可以创建一个 BlocProvider 并与状态一起工作。
I found the solution. It works creating a BlocProvider and working with the state.