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

[Question] Cleaning up state on beam away #647

Closed
tinaroh opened this issue Nov 22, 2023 · 3 comments
Closed

[Question] Cleaning up state on beam away #647

tinaroh opened this issue Nov 22, 2023 · 3 comments

Comments

@tinaroh
Copy link

tinaroh commented Nov 22, 2023

Hello, I'm trying to find the best way to clean up some state if a user beams away from the page.

My setup is similar to bottom_navigation_multiple_beamers with nested Beamers, except my ArticleDetailsScreen is a StatefulWidget that contains an audio player.

The app preserves the state of the Articles "stack" when the user taps away (which I like), but because it's still in the widget tree, I'm having trouble figuring out where to handle clean up on navigation away.

If the user taps on the "Books" bottom navigation while the audio player is playing, I'm looking to declare and call something like ArticleDetailsScreen.onHide() to pause the audio player and clean up some state. Is there a place like that?

Currently I'm thinking of having ArticleDetailsScreenState implement a onHide and adding a routeListener at the AppScreen level that calls it by accessing the state using a GlobalKey as mentioned here https://stackoverflow.com/a/46545141. But I wanted to see if there was a cleaner way through the library.

Thank you for the help!

@stan-at-work
Copy link
Contributor

Hello, I'm trying to find the best way to clean up some state if a user beams away from the page.

My setup is similar to bottom_navigation_multiple_beamers with nested Beamers, except my ArticleDetailsScreen is a StatefulWidget that contains an audio player.

The app preserves the state of the Articles "stack" when the user taps away (which I like), but because it's still in the widget tree, I'm having trouble figuring out where to handle clean up on navigation away.

If the user taps on the "Books" bottom navigation while the audio player is playing, I'm looking to declare and call something like ArticleDetailsScreen.onHide() to pause the audio player and clean up some state. Is there a place like that?

Currently I'm thinking of having ArticleDetailsScreenState implement a onHide and adding a routeListener at the AppScreen level that calls it by accessing the state using a GlobalKey as mentioned here https://stackoverflow.com/a/46545141. But I wanted to see if there was a cleaner way through the library.

Thank you for the help!

I think the best way is to listen to it using the routeListener...

@stan-at-work
Copy link
Contributor

@tinaroh you schould use inherited widget to manage that or even something like riverpod or bloc to manage your logic.

GlobalKey is possible but not scalable

@tinaroh
Copy link
Author

tinaroh commented Jul 30, 2024

Got it, thank you!

@tinaroh tinaroh closed this as completed Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants