Skip to content

Commit

Permalink
Merge pull request #90 from karvulf/88-abnormal-operation-when-deleti…
Browse files Browse the repository at this point in the history
…ng-an-item-by-dragging

88 abnormal operation when deleting an item by dragging
  • Loading branch information
karvulf committed Sep 1, 2023
2 parents c27c314 + 990bef3 commit 9b83658
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/flutter_code_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

# Uncomment this step to verify the use of 'flutter format' on each commit.
- name: Verify formatting
run: flutter format --output=none --set-exit-if-changed .
run: dart format --output=none --set-exit-if-changed .

# Consider passing '--fatal-infos' for slightly stricter analysis.
- name: Analyze project source
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.0-dev.9
🐛 **Bug fixes**
* fixed issue when deleting the dragged child (thanks to `khjde1207` - Issue [#88](https://github.com/karvulf/flutter-reorderable-grid-view/issues/88))

## 5.0.0-dev.8
🐛 **Bug fixes**
* fixed animation when releasing a dragged item
Expand Down
6 changes: 6 additions & 0 deletions lib/utils/definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ typedef ReorderableEntityCallback = void Function(
typedef ReleasedReorderableEntityCallback = void Function(
ReleasedReorderableEntity releasedReorderableEntity);

///
typedef OnDragEndFunction = void Function(
ReorderableEntity reorderableEntity,
Offset globalOffset,
);

/// Called if drag was canceled, e.g. the dragged item is removed while dragging.
typedef OnDragCanceledFunction = void Function(
ReorderableEntity reorderableEntity,
);

typedef OnCreatedFunction = void Function(
ReorderableEntity reorderableEntity,
GlobalKey key,
Expand Down
22 changes: 18 additions & 4 deletions lib/widgets/draggable_feedback.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import 'package:flutter/material.dart';
import 'package:flutter_reorderable_grid_view/entities/reorderable_entity.dart';
import 'package:flutter_reorderable_grid_view/utils/definitions.dart';

class DraggableFeedback extends StatelessWidget {
class DraggableFeedback extends StatefulWidget {
final Widget child;
final ReorderableEntity reorderableEntity;
final Animation<Decoration> decoration;
final ReorderableEntityCallback onDeactivate;

const DraggableFeedback({
required this.child,
required this.reorderableEntity,
required this.decoration,
required this.onDeactivate,
Key? key,
}) : super(key: key);

@override
State<DraggableFeedback> createState() => _DraggableFeedbackState();
}

class _DraggableFeedbackState extends State<DraggableFeedback> {
@override
void deactivate() {
widget.onDeactivate(widget.reorderableEntity);
super.deactivate();
}

@override
Widget build(BuildContext context) {
final size = reorderableEntity.size;
final size = widget.reorderableEntity.size;

return Material(
color: Colors.transparent, // removes white corners when having shadow
Expand All @@ -24,8 +38,8 @@ class DraggableFeedback extends StatelessWidget {
width: size.width,
child: DecoratedBoxTransition(
position: DecorationPosition.background,
decoration: decoration,
child: child,
decoration: widget.decoration,
child: widget.child,
),
),
);
Expand Down
14 changes: 11 additions & 3 deletions lib/widgets/reorderable_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ class _ReorderableBuilderState extends State<ReorderableBuilder>
dragChildBoxDecoration: widget.dragChildBoxDecoration,
onDragStarted: _handleDragStarted,
onDragEnd: _handleDragEnd,
onDragCanceled: _handleDragCanceled,
child: child,
),
),
Expand Down Expand Up @@ -394,6 +395,16 @@ class _ReorderableBuilderState extends State<ReorderableBuilder>
setState(() {});

_finishDragging();

// important to update the dragged entity which should be null at this point
setState(() {});
}

/// Called after the dragged child was canceled, e.g. deleted.
///
/// Finishes dragging without doing any animation for the dragged entity.
void _handleDragCanceled(ReorderableEntity reorderableEntity) {
_finishDragging();
}

void _handleScrollUpdate(Offset scrollOffset) {
Expand All @@ -414,9 +425,6 @@ class _ReorderableBuilderState extends State<ReorderableBuilder>
reorderUpdateEntities: reorderUpdateEntities,
));
}

// important to update the dragged entity which should be null at this point
setState(() {});
}

/// Animation part
Expand Down
5 changes: 5 additions & 0 deletions lib/widgets/reorderable_draggable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ReorderableDraggable extends StatefulWidget {

final ReorderableEntityCallback onDragStarted;
final OnDragEndFunction onDragEnd;
final OnDragCanceledFunction onDragCanceled;

final ReorderableEntity? currentDraggedEntity;

Expand All @@ -34,6 +35,7 @@ class ReorderableDraggable extends StatefulWidget {
required this.enableDraggable,
required this.onDragStarted,
required this.onDragEnd,
required this.onDragCanceled,
required this.currentDraggedEntity,
this.dragChildBoxDecoration,
Key? key,
Expand Down Expand Up @@ -96,6 +98,9 @@ class _ReorderableDraggableState extends State<ReorderableDraggable>
decoration: _decorationTween.animate(
_decoratedBoxAnimationController,
),
onDeactivate: (reorderableEntity) {
widget.onDragCanceled(reorderableEntity);
},
child: child,
);

Expand Down

0 comments on commit 9b83658

Please sign in to comment.