riverscuomo / public-bug-hunt

A place to file bug reports for all of my apps
GNU General Public License v3.0
7 stars 0 forks source link

Shellappbar react to scroll position via bloc? #80

Closed riverscuomo closed 11 months ago

riverscuomo commented 11 months ago

so you can disappear the title's in the app bar as user scrolls, like spotify

Yes, using Flutter Bloc for managing the state of the ScrollController is indeed a possibility. This would keep with the overall architecture of your current app.

Here is a rough outline how you would do it:

First, define a Bloc event that parameterizes the relevant scroll position:

class ScrollUpdated extends YourBlocEvent {
  final double scrollPosition;

  ScrollUpdated(this.scrollPosition);
}

Then you can define a corresponding state:

class ScrollState extends YourBlocState {
  final double scrollPosition;

  ScrollState(this.scrollPosition);
}

Next, in the initState() method of your widget, you can add listener to your ScrollController that will dispatch an event to your bloc every time new scroll position is detected:

_scrollController.addListener(() {
  context.read<YourBloc>().add(ScrollUpdated(_scrollController.offset));
});

And lastly, in your Bloc mapEventToState, handle the ScrollUpdated event:

on<ScrollUpdated>((event, emit) {
  emit(ScrollState(event.scrollPosition));
});

Remember to dispose of your ScrollController in the dispose method of the widget.

Now, your bloc will emit a new ScrollState every time the scroll position is changed and you can listen to these events from your appBar or any other part of your app by using BlocBuilder or BlocConsumer.

Although it's not a common use case to manage scroll position using a state management solution such as Bloc, in complex apps with many layers that require such synchronization it might be one way to tackle this problem. Using Bloc in this case allows multiple components to react to the ScrollPosition changes.

riverscuomo commented 11 months ago

1.11.0