slovnicki / beamer

A routing package built on top of Router and Navigator's pages API, supporting arbitrary nested navigation, guards and more.
MIT License
591 stars 129 forks source link

[Question] Cleaning up state on beam away #647

Closed tinaroh closed 3 months ago

tinaroh commented 1 year ago

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 commented 3 months ago

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 commented 3 months ago

@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 commented 3 months ago

Got it, thank you!