I am attempting to create a Drag and Drop with a GridView.custom. What I desire to have happen, is that when dragging an item, if it reaches the top or bottom of the screen, the gridview scrolls. Is there a build in way to do that, or is there a work around that has to be implemented. I am using the flutter_staggered_grid_view package in this case.
GridView.custom(
shrinkWrap: true,
primary: false,
scrollDirection: Axis.vertical,
gridDelegate: SliverQuiltedGridDelegate(
crossAxisCount: 4,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
repeatPattern: QuiltedGridRepeatPattern.inverted,
pattern: widget.pattern,
),
childrenDelegate: SliverChildBuilderDelegate(
childCount: widget.children.length,
(context, index) {
var selectedWidget = widget.children[index];
LongPressDraggable<GlobalKey>(
data: selectedWidget.key,
onDragStarted: _onDragStarted,
onDragEnd: _onDragEnd,
feedback:
SizedBox(width: 100, height: 100, child: selectedWidget),
childWhenDragging: Container(),
child: DragTarget<GlobalKey>(
builder: (context, accepted, rejected) => selectedWidget,
onWillAccept: (GlobalKey? accept) {
return true;
},
onAccept: (GlobalKey item) {
int startIndex =
widget.children.indexWhere((x) => x.key == item);
int endIndex = widget.children
.indexWhere((x) => x.key == selectedWidget.key);
widget.onReorder(startIndex, endIndex);
},
),
)
},
),
);
2
Answers
After some research, I cobbled together a method that created the effect I desired.
I also added
ClampingScrollPhysics()
to avoid over scrolling. Credit to https://github.com/DevOrbiter/drag_and_drop_gridview/ for giving me the base code.compare
Draggable
position of the Y axis inGridView
area.Here is the sample; Scroll up if
Draggle
positions on a top area where is height 20, scroll down if it positions on a bottom area where is height 20.