[英]Flutter Grid View Reorder & Drag Drop onto Another Item and Merge
I have been trying to add drag/drop support to my app, currently what I have come with is using this library:我一直在尝试向我的应用程序添加拖放支持,目前我使用的是这个库:
reorderable_grid_view可重新排序的网格视图
I used this example code: code link我使用了这个示例代码: 代码链接
The reason I used this library is that it's smooth enough of animations when dragging.我使用这个库的原因是它在拖动时动画足够流畅。 But what I want to do is to drag one item to another so that I can merge the one to another object when I drop.
但是我想做的是将一个项目拖到另一个项目上,以便在我放下时可以将一个项目合并到另一个项目中 object。 (It's like in Android/iOS home screen where you can drag apps to folders or drag into another that it creates a folder)
(这就像在 Android/iOS 主屏幕中,您可以将应用程序拖到文件夹或拖到另一个它会创建一个文件夹)
I have searched all the site but couldn't come across with such thing, only drag/drop libraries are available.我已经搜索了所有网站,但没有遇到这样的事情,只有拖放库可用。 Can anyone help me on this?
谁可以帮我这个事?
Thanks in advance.提前致谢。
class MyHomePage extends StatelessWidget {
MyHomePage({super.key});
final ValueNotifier<List<ValueNotifier<List<Widget>>>> items = ValueNotifier([
ValueNotifier([Text("A")]),
ValueNotifier([Text("B")]),
ValueNotifier([Text("C")]),
ValueNotifier([Text("D")]),
]);
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 20.0),
child: ValueListenableBuilder(
valueListenable: items,
builder: (BuildContext context, List<ValueNotifier<List<Widget>>> folders, Widget? child) {
return GridView.builder(
physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3),
itemCount: folders.length,
itemBuilder: (context, index) {
ValueNotifier<List<Widget>> item = folders[index];
return LongPressDraggable(
delay: const Duration(milliseconds: 500),
feedback: SizedBox(width: MediaQuery.of(context).size.width / 4, height: MediaQuery.of(context).size.width / 4, child: FittedBox(child: Icon(Icons.folder))),
data: index,
childWhenDragging: const SizedBox(),
child: DragTarget(
onAccept: (data) {
List<Widget> alreadyHaved = item.value;
alreadyHaved.addAll(folders[data as int].value);
item.value = alreadyHaved;
items.value.removeAt(data);
items.notifyListeners();
},
builder: (context, candidateData, rejectedData) {
return ValueListenableBuilder(
valueListenable: item,
builder: (BuildContext context, List<Widget> boxValues, Widget? child) {
return Stack(children: [
const Positioned.fill(
child: FittedBox(
child: Icon(
Icons.folder,
color: Colors.amber,
))),
Positioned.fill(
child: LayoutBuilder(
builder: (p0, p1) => SizedBox(
height: p1.maxHeight * .7,
width: p1.maxWidth * .7,
child: Center(
child: Wrap(
children: boxValues,
),
))),
)
]);
},
);
},
),
);
});
},
),
),
),
],
),
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.