Skip to content

Commit

Permalink
Fixed known issues [#21] Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Hetpra9653 committed Mar 19, 2024
1 parent 4a70bf8 commit 309f842
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 88 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,8 @@
## [0.0.11] - 2024-04-19

* Fixed known issues [#21]
* Minor improvements

## [0.0.10] - 2024-03-08

* Updated example and changes into library
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# flutter_draggable_gridview

<a href="https://pub.dev/packages/image_cropping">
<a href="https://pub.dev/packages/flutter_draggable_gridview">
<img src="https://img.shields.io/pub/v/flutter_draggable_gridview?label=flutter_draggable_gridview" alt="flutter_draggable_gridview version">
</a>
<a href="https://github.com/Mindinventory/flutter_draggable_gridview/stargazers">
Expand Down
82 changes: 15 additions & 67 deletions example/lib/pages/grid_example.dart
@@ -1,7 +1,6 @@
import 'dart:developer';

import 'package:example/constants/images.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_draggable_gridview/flutter_draggable_gridview.dart';

Expand All @@ -18,12 +17,10 @@ class GridExample extends StatefulWidget {

class GridExampleState extends State<GridExample> {
final List<DraggableGridItem> _listOfDraggableGridItem = [];
final List<DraggableGridItem> _listOfDraggableGridItem2 = [];

@override
void initState() {
_generateImageData1();
_generateImageData2();
_generateImageData();
super.initState();
}

Expand All @@ -37,40 +34,17 @@ class GridExampleState extends State<GridExample> {
),
),
body: SafeArea(
child: Column(
children: [
SizedBox(
height: MediaQuery.of(context).size.height / 3,
child: DraggableGridViewBuilder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 3),
),
children: _listOfDraggableGridItem,
dragCompletion: onDragAccept,
isOnlyLongPress: true,
dragFeedback: feedback,
dragPlaceHolder: placeHolder,
),
),
const SizedBox(height: 50),
SizedBox(
height: MediaQuery.of(context).size.height / 3,
child: DraggableGridViewBuilder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 3),
),
children: _listOfDraggableGridItem2,
dragCompletion: onDragAccept2,
isOnlyLongPress: true,
dragFeedback: feedback2,
dragPlaceHolder: placeHolder2,
),
),
],
child: DraggableGridViewBuilder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 3),
),
children: _listOfDraggableGridItem,
dragCompletion: onDragAccept,
isOnlyLongPress: true,
dragFeedback: feedback,
dragPlaceHolder: placeHolder,
),
),
);
Expand All @@ -97,28 +71,7 @@ class GridExampleState extends State<GridExample> {
log('onDragAccept: $beforeIndex -> $afterIndex');
}

Widget feedback2(List<DraggableGridItem> list, int index) {
return SizedBox(
width: 200,
height: 150,
child: list[index].child,
);
}

PlaceHolderWidget placeHolder2(List<DraggableGridItem> list, int index) {
return PlaceHolderWidget(
child: Container(
color: Colors.green,
),
);
}

void onDragAccept2(
List<DraggableGridItem> list, int beforeIndex, int afterIndex) {
log('onDragAccept: $beforeIndex -> $afterIndex');
}

void _generateImageData1() {
void _generateImageData() {
_listOfDraggableGridItem.addAll(
[
DraggableGridItem(
Expand All @@ -134,13 +87,8 @@ class GridExampleState extends State<GridExample> {
child: const GridItem(image: Images.asset_3), isDraggable: true),
DraggableGridItem(
child: const GridItem(image: Images.asset_4), isDraggable: true),
],
);
}

void _generateImageData2() {
_listOfDraggableGridItem2.addAll(
[
DraggableGridItem(
child: const GridItem(image: Images.asset_5), isDraggable: false),
DraggableGridItem(
child: const GridItem(image: Images.asset_6), isDraggable: true),
DraggableGridItem(
Expand Down
34 changes: 16 additions & 18 deletions lib/widgets/drag_target_grid.dart
Expand Up @@ -37,8 +37,8 @@ class DragTargetGrid extends StatefulWidget {
}

class DragTargetGridState extends State<DragTargetGrid> {
List<DraggableGridItem>? _orgList;
List<DraggableGridItem>? _list;
late List<DraggableGridItem> _orgList;
late List<DraggableGridItem> _list;
static bool _draggedIndexRemoved = false;
static int _lastIndex = -1;
static int _draggedIndex = -1;
Expand All @@ -53,20 +53,18 @@ class DragTargetGridState extends State<DragTargetGrid> {
@override
void didUpdateWidget(DragTargetGrid oldWidget) {
_orgList = [...widget.orgList];

super.didUpdateWidget(oldWidget);
}

@override
Widget build(BuildContext context) {
return DragTarget<(int, DraggableGridItem)>(
/// When drag is completes and other item index is ready to accept it.
onAcceptWithDetails: (data) {
onAcceptWithDetails: (details) {
setState(() {
_onDragComplete(widget.index);
});
},
onLeave: (details) {},

/// Drag is acceptable in this index else this place.
onWillAcceptWithDetails: (details) {
Expand All @@ -89,14 +87,14 @@ class DragTargetGridState extends State<DragTargetGrid> {
/// [_isOnlyLongPress] is true then set the 'LongPressDraggableGridView' class or else set 'PressDraggableGridView' class.
return (widget.isOnlyLongPress)
? LongPressDraggableGridView(
list: _list!,
list: _list,
index: widget.index,
feedback: widget.feedback,
childWhenDragging: widget.childWhenDragging,
onDragCancelled: () => _onDragComplete(_lastIndex),
)
: PressDraggableGridView(
list: _list!,
list: _list,
index: widget.index,
feedback: widget.feedback,
childWhenDragging: widget.childWhenDragging,
Expand Down Expand Up @@ -125,7 +123,7 @@ class DragTargetGridState extends State<DragTargetGrid> {
/// Here, check [_draggedIndex] is != -1.
/// And also check index is not equal to _lastIndex. Means if both will true then skip it. else do some operations.
if (_draggedIndex != -1 && index != _lastIndex) {
_list!.removeWhere((element) {
_list.removeWhere((element) {
return (widget.placeHolder != null)
? element.child is PlaceHolderWidget
: element.child is EmptyItem;
Expand All @@ -139,9 +137,9 @@ class DragTargetGridState extends State<DragTargetGrid> {
/// For ex:
/// If _draggedIndex is 6 and _lastIndex = 4 then _draggedChild will be 5.
if (_draggedIndex > _lastIndex) {
_draggedGridItem = _orgList?[_draggedIndex - 1];
_draggedGridItem = _orgList[_draggedIndex - 1];
} else {
_draggedGridItem = _orgList?[(_draggedIndex + 1 >= _list!.length)
_draggedGridItem = _orgList[(_draggedIndex + 1 >= _list.length)
? _draggedIndex
: _draggedIndex + 1];
}
Expand All @@ -154,9 +152,9 @@ class DragTargetGridState extends State<DragTargetGrid> {

if (!_draggedIndexRemoved) {
_draggedIndexRemoved = true;
_list?.removeAt(_draggedIndex);
_list.removeAt(_draggedIndex);
}
_list?.insert(
_list.insert(
_lastIndex,
DraggableGridItem(
child: widget.placeHolder ?? const EmptyItem(), isDraggable: true),
Expand All @@ -167,17 +165,17 @@ class DragTargetGridState extends State<DragTargetGrid> {
/// This method will execute when dragging is completes or else dragging is cancelled.
void _onDragComplete(int index) {
if (_draggedIndex == -1) return;
_list?.removeAt(index);
_list?.insert(index, _orgList![_draggedIndex]);
_list.removeAt(index);
_list.insert(index, _orgList[_draggedIndex]);

_orgList = _list!.toList();
_orgList = _list.toList();
_dragStarted = false;

widget.onListUpdate.call(_list!);
widget.onOrgListUpdate.call(_orgList!);
widget.onListUpdate.call(_list);
widget.onOrgListUpdate.call(_orgList);
widget.onChangeCallback?.call();

widget.dragCompletion?.call(_orgList!, _draggedIndex, _lastIndex);
widget.dragCompletion?.call(_orgList, _draggedIndex, _lastIndex);

_draggedIndex = -1;
_lastIndex = -1;
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
@@ -1,6 +1,6 @@
name: flutter_draggable_gridview
description: This package helps you to add drag & drop functionality to exising Gridview.builder.
version: 0.0.10
description: This package helps you to add drag & drop functionality to existing Gridview.builder.
version: 0.0.11
repository: https://github.com/Mindinventory/flutter_draggable_gridview
issue_tracker: https://github.com/Mindinventory/flutter_draggable_gridview/issues

Expand Down

0 comments on commit 309f842

Please sign in to comment.