knopp / flutter_reorderable_list

ReorderableList for Flutter
BSD 3-Clause "New" or "Revised" License
335 stars 98 forks source link

Passing the context onReorder callback #5

Closed dxps closed 5 years ago

dxps commented 5 years ago

Hi Matej,

I want to use a BLoC (concretely, I am using the flutter_bloc package) inside the onReorder callback in order to 'announce' the changes and persist them.

But I would need to have access to the BuildContext to get the BLoC using something such as BlocProvider.of<MyCustomBloc>(context). So, in a StatelessWidget, a concrete implemenetation of onReorder callback should have this context as parameter as well:

  _onReorder(Key itemKey, Key newPositionKey, BuildContext context) {
    // ...
 }

Of course, for the moment, a StatefulWidget can be used to just store the BLoC retrieved from hierarchy in its state.

What do you think?

Thanks!

dxps commented 5 years ago

Sorry, my bad, silly question ... 😄 Solution is to just declare the 'extended' function like this:

myBloc = BlocProvider.of<MyCustomBloc>(context);
return ReorderableList(
  onReorder: (itemKey, newPositionKey) => _onReorder(itemKey, newPositionKey, myBloc),
  ...