Make draggable widget more generic

This commit is contained in:
Ming Ming 2024-10-26 19:41:03 +08:00
parent 0e6fe1dede
commit 791a65713b
3 changed files with 12 additions and 20 deletions

View file

@ -15,12 +15,11 @@ class Draggable<T extends Object> extends StatelessWidget {
this.onDropAfter, this.onDropAfter,
this.onDragStarted, this.onDragStarted,
this.onDragEndedAny, this.onDragEndedAny,
this.feedbackSize,
}); });
@override @override
build(BuildContext context) { Widget build(BuildContext context) {
buildIndicator(alignment, isActive) { Widget buildIndicator(alignment, isActive) {
return Stack( return Stack(
children: [ children: [
Container(), Container(),
@ -39,7 +38,7 @@ class Draggable<T extends Object> extends StatelessWidget {
} }
return Stack( return Stack(
fit: StackFit.expand, fit: StackFit.passthrough,
children: [ children: [
LongPressDraggable<T>( LongPressDraggable<T>(
data: data, data: data,
@ -48,11 +47,10 @@ class Draggable<T extends Object> extends StatelessWidget {
onDragEnd: (_) => onDragEndedAny?.call(), onDragEnd: (_) => onDragEndedAny?.call(),
onDragCompleted: onDragEndedAny, onDragCompleted: onDragEndedAny,
onDraggableCanceled: (v, o) => onDragEndedAny?.call(), onDraggableCanceled: (v, o) => onDragEndedAny?.call(),
feedback: FractionalTranslation( feedback: Material(
type: MaterialType.transparency,
child: FractionalTranslation(
translation: const Offset(-.5, -.5), translation: const Offset(-.5, -.5),
child: SizedBox(
width: feedbackSize?.width ?? 128,
height: feedbackSize?.height ?? 128,
child: Opacity( child: Opacity(
opacity: .5, opacity: .5,
child: feedback ?? child, child: feedback ?? child,
@ -122,9 +120,4 @@ class Draggable<T extends Object> extends StatelessWidget {
/// ///
/// The callback might be called multiple times per each drag event /// The callback might be called multiple times per each drag event
final VoidCallback? onDragEndedAny; final VoidCallback? onDragEndedAny;
/// Size of the feedback widget that appears under the pointer.
///
/// Right now a translucent version of [child] is being shown
final Size? feedbackSize;
} }

View file

@ -63,7 +63,11 @@ class _DraggableItemListState<T extends DraggableItemMetadata>
if (meta.isDraggable) { if (meta.isDraggable) {
return my.Draggable<_DraggableData>( return my.Draggable<_DraggableData>(
data: _DraggableData(i, meta), data: _DraggableData(i, meta),
feedback: widget.itemDragFeedbackBuilder?.call(context, i, meta), feedback: SizedBox(
width: widget.maxCrossAxisExtent * .65,
height: widget.maxCrossAxisExtent * .65,
child: widget.itemDragFeedbackBuilder?.call(context, i, meta),
),
onDropBefore: (data) => _onMoved(data.index, i, true), onDropBefore: (data) => _onMoved(data.index, i, true),
onDropAfter: (data) => _onMoved(data.index, i, false), onDropAfter: (data) => _onMoved(data.index, i, false),
onDragStarted: () { onDragStarted: () {
@ -72,8 +76,6 @@ class _DraggableItemListState<T extends DraggableItemMetadata>
onDragEndedAny: () { onDragEndedAny: () {
widget.onDraggingChanged?.call(false); widget.onDraggingChanged?.call(false);
}, },
feedbackSize: Size(widget.maxCrossAxisExtent * .65,
widget.maxCrossAxisExtent * .65),
child: widget.itemBuilder(context, i, meta), child: widget.itemBuilder(context, i, meta),
); );
} else { } else {

View file

@ -26,7 +26,6 @@ class _DemoView extends StatelessWidget {
feedback: _DraggingButton( feedback: _DraggingButton(
child: _DemoButtonDelegate(e), child: _DemoButtonDelegate(e),
), ),
feedbackSize: const Size.square(48),
onDropBefore: (data) { onDropBefore: (data) {
context.addEvent(_MoveButton.before( context.addEvent(_MoveButton.before(
which: data, which: data,
@ -148,7 +147,6 @@ class _DemoBottomView extends StatelessWidget {
feedback: _DraggingButton( feedback: _DraggingButton(
child: _DemoButtonDelegate(e), child: _DemoButtonDelegate(e),
), ),
feedbackSize: const Size.square(48),
onDropBefore: (data) { onDropBefore: (data) {
context.addEvent(_MoveButton.before( context.addEvent(_MoveButton.before(
which: data, which: data,
@ -232,7 +230,6 @@ class _CandidateGrid extends StatelessWidget {
feedback: _DraggingButton( feedback: _DraggingButton(
child: _DemoButtonDelegate(e), child: _DemoButtonDelegate(e),
), ),
feedbackSize: const Size.square(48),
child: _CandidateButtonDelegate(e), child: _CandidateButtonDelegate(e),
)) ))
.toList(), .toList(),