Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listener on header sticked #70

Open
royvangeel opened this issue Nov 26, 2021 · 4 comments
Open

Listener on header sticked #70

royvangeel opened this issue Nov 26, 2021 · 4 comments
Labels
enhancement New feature or request

Comments

@royvangeel
Copy link

royvangeel commented Nov 26, 2021

Is it possible to detect when a header is sticked (like using a listener of provide a callback)? I need to create the effect like in the video; I have a header for each weekday, the calendar above the listview needs to be updated based on the sticked header.
I haven't figured out how to do. Thanks!

example.mov
@shw2ypro
Copy link

Hello
Did you find a way to do that? @royvangeel

@royvangeel
Copy link
Author

@shw2ypro yes, but not using this package. I managed using the sticky_and_expandable_list package and wrap the upper calendar with a StreamBuilder so the ListView pushes the sticked Date into the sink. But I would like to know how to achieve it with this package.

@letsar
Copy link
Owner

letsar commented Jul 13, 2022

Interesting use case

@letsar letsar added the enhancement New feature or request label Jul 13, 2022
@daveshirman
Copy link

daveshirman commented Dec 15, 2022

For anyone needing it, I achieved this by using:

  • ValueNotifier
  • ValueListenableBuilder
  • SliverStickyHeader.builder
  • WidgetsBinding.instance.addPostFrameCallback

Example: Something like here, where in this example I'm changing a header title that looke like "DECEMBER 2022":

  1. Declare your ValueNotifier at the class level, e.g.

ValueNotifier<String> _currentTitleNotifier = ValueNotifier<String>("---");

  1. Wire up your UI to pump out the _currentTitleNotifier value:
ValueListenableBuilder(
    valueListenable: _currentTitleNotifier,
    builder: (context, value, child) {
        return Text(
            value.toString().toUpperCase(),
        );
    },
)
  1. Observe the state.isPinned in the SliverStickyHeader.builder(:
builder: (context, state) {
    String newCurrentTitle = DateFormat("MMMM yyyy")
        .format(group.date)
        .toUpperCase();
    if (state.isPinned &&
        _currentTitleNotifier.value.toString() !=
            newCurrentTitle) {
        WidgetsBinding.instance.addPostFrameCallback((_) {
            _currentTitleNotifier.value =
            newCurrentTitle;
        });
    }
}

The WidgetsBinding.instance.addPostFrameCallback will only get called once, because of the if condition around it. Seems to work well so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants