karvulf / flutter-reorderable-grid-view

BSD 3-Clause "New" or "Revised" License
155 stars 23 forks source link

[feature] Add a way to add data to the draggable widgets. #77

Closed isenbj closed 1 year ago

isenbj commented 1 year ago

I'd like to be able to drag elements from the grid onto DragTargets in my app. Currently null is passed through as the data though.

see draggables I would like data added to here: https://github.com/karvulf/flutter-reorderable-grid-view/blob/master/lib/widgets/animated/reorderable_draggable.dart#L150

karvulf commented 1 year ago

Hi @isenbj Thanks for opening the issue. I can add the data as parameter, but only for the prerelease of 5.0.0. Can you explain me, how the data is used or when do you want to access the data?

isenbj commented 1 year ago

A Draggable class in flutter has a data class that will hold any data I desire.

In my case, I have a grid of images and each image has some metadata I allow a user to customize. I want to allow a user to drag these grid elements to an off-grid widget (such as a folder) and have that metadata available. This will allow me to execute some code that actually adds the image to that folder. Currently if I add a DragTarget anywhere in the app, the data for the widget is not passed through and is null.

I branched your repo and put together a crude example of what I mean to get the data through here.

Since you allow any child type in the gridview for each element, you may have to build in an optional child type that allows this data to be passed through per child.

karvulf commented 1 year ago

I will add this parameter just by passing it to the widget, I will add it this week in the version 5.0.0-dev.7 @isenbj

isenbj commented 1 year ago

Will you be some to make it unique per grid item this way?

karvulf commented 1 year ago

Oh now I know what you mean, yeah it has to be unique per item, that makes it more complicated, I will think about a solution @isenbj

karvulf commented 1 year ago

My idea is that you can optionally wrap a widget around your child that has the data property. If your widget has this, I could extract the info and use it for Draggable. @isenbj

isenbj commented 1 year ago

Yes, see the shared fork of the repo I shared above. I am doing this locally and it is working.

On Mon, Jun 19, 2023 at 12:27 AM karvulf @.***> wrote:

My idea is that you can optionally wrap a widget around your child that has the data property. If your widget has this, I could extract the info and use it for Draggable. @isenbj https://github.com/isenbj

— Reply to this email directly, view it on GitHub https://github.com/karvulf/flutter-reorderable-grid-view/issues/77#issuecomment-1596528778, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL322JUZSR4HRNTQERMMESTXL7PL5ANCNFSM6AAAAAAZJZZYFA . You are receiving this because you were mentioned.Message ID: @.***>

karvulf commented 1 year ago

Yes I saw the repo. My concern about this change is, that it would be a breaking change changing the type. It has also to be changed for the factory .builder. That is the reason I prefer using an extra widget. @isenbj

isenbj commented 1 year ago

Yes, so another option would be add a custom class with a data property, and when you create the Draggable and LongPressDraggable widgets, you can type check the child to see if the data exists. This would require the fewest changes I think.

On Mon, Jun 19, 2023 at 12:40 AM karvulf @.***> wrote:

Yes I saw the repo. My concern about this change is, that it would be a breaking change changing the type. It has also to be changed for the factory .builder. That is the reason I prefer using an extra widget. @isenbj https://github.com/isenbj

— Reply to this email directly, view it on GitHub https://github.com/karvulf/flutter-reorderable-grid-view/issues/77#issuecomment-1596536779, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL322JU6FI3QMQCW2KTQ2PLXL7Q3TANCNFSM6AAAAAAZJZZYFA . You are receiving this because you were mentioned.Message ID: @.***>

karvulf commented 1 year ago

That sounds good @isenbj

karvulf commented 1 year ago

Can you check if this change does that what you wished for? @isenbj

You can add the following to your pubspec.yaml:

flutter_reorderable_grid_view:
  git:
    url: https://github.com/karvulf/flutter-reorderable-grid-view.git
    ref: 7f06cb0c954a2353029ee9b79f22a208dde27fd7

and you just have to add to every child:

return CustomDraggable(
  key: Key(children[index].toString()),
  data: index,
  child: ...
isenbj commented 1 year ago

Yes this passes the data as I wanted to a drag target.

Note: I am not sure if you are working on other changes, but this commit broke other things in my code. ex.

That's all I've noticed so far, so I can't use this commit yet with those breaking changes perhaps related to other changes.

karvulf commented 1 year ago

Hmm that sounds weird. Did you use version 5.0.0-dev.6 before? And can you give the code that has this behavior? @isenbj

karvulf commented 1 year ago

The issues you describe I couldn't reproduce them with the example app. Can you show me your code? @isenbj I will merge this as it is. Another thing I recognized are some performance issues after a while using drag and drop. I will fix that in another PR.

karvulf commented 1 year ago

I added the feature to the branch for the release 5.0.0. Soon I will update the pre version to 5.0.0-dev.7 @isenbj

isenbj commented 1 year ago

I've been off for a few days, checking again now to see if I can reproduce issues. I will use commit: aaa8d9b0a6c5315bb6cdc4582a78d50ad406f7a3

isenbj commented 1 year ago

This looks good to me now, and I cannot reproduce the issues. Nice job!

How do I use the onReorder function now? I am confused since it has changed.

karvulf commented 1 year ago

The reorder function now gives you a function that reorders for you the list. You can see it in the example app. @isenbj

  void _handleReorder(ReorderedListFunction reorderedListFunction) {
    setState(() {
      children = reorderedListFunction(children) as List<int>;
    });
  }