hcbpassos / drag_select_grid_view

A grid that supports both dragging and tapping to select its items.
BSD 2-Clause "Simplified" License
133 stars 34 forks source link

Is there away to initialize the selections ? #14

Closed vin-fandemand closed 4 years ago

vin-fandemand commented 4 years ago

Is there a way to initialize the selections? Like pass in the indexes to the controller?

ghost commented 4 years ago

As per Advanced Usage:

You can use the setter DragSelectGridViewController.selection to directly change the selection (I'm not quite sure why you'd need this, but I don't ask questions).

Here's an example:

https://github.com/hugocbpassos/drag_select_grid_view/blob/0221e5dbeecc9f809117d525c041e21a7f897e77/example/lib/example_with_fab.dart#L77-L78

Is this helpful?

vin-fandemand commented 4 years ago

Yes. Thanks a lot.

vin-fandemand commented 4 years ago

actually, this is not exactly initialization. When you set this, you would notify listeners. Now we usually initialize in the beginning and hence setting state then wouldn't be ideal. The example you have given is useful when you want to update the selection later.

So this is the use case. We have a grid of stuff. And we need to show the ones that have already been selected as selected and don't want to select them again. Hence we need a way to initialize it.

vin-fandemand commented 4 years ago

can you please reopen this issue if possible ?

ghost commented 4 years ago

Now we usually initialize in the beginning and hence setting state then wouldn't be ideal.

I agree. There's probably a way to work around that, though. I'll take a look later.

ghost commented 4 years ago

There are some ways to implement this feature, but I need to check how Flutter and other libraries manage to get this working, in order to follow their pattern.

In the meanwhile, you may use after_layout to change the selection after the first frame has been displayed:

class _MyAppState extends State<MyApp> with AfterLayoutMixin<MyApp> {
  final controller = DragSelectGridViewController();

  @override
  void initState() {
    super.initState();
    controller.addListener(scheduleRebuild);
  }

  @override
  void dispose() {
    controller.removeListener(scheduleRebuild);
    super.dispose();
  }

  @override
  void afterFirstLayout(BuildContext context) {
    controller.selection = const Selection({1, 2, 3});
  }

  ...
}
ghost commented 4 years ago

@vin-fandemand Now, specifying a Selection in DragSelectGridViewController should work for you :)

vin-fandemand commented 4 years ago

@vin-fandemand Now, specifying a Selection in DragSelectGridViewController should work for you :)

Thanks, mate, I will test and let me know