packages icon indicating copy to clipboard operation
packages copied to clipboard

[go_router] [shell_route] Add observers parameter

Open angjelkom opened this issue 3 years ago • 1 comments

This PR adds observers parameter to the ShellRoute and passes that to the nested Navigator created by that same ShellRoute.

The need for this PR is because it fixes cases where a Hero animation doesn't work when used with nested navigation and it requires a HeroController to be used as an observer for that nested Navigator.

fixes https://github.com/flutter/flutter/issues/112095

Pre-launch Checklist

  • [x] I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • [x] I read the Tree Hygiene wiki page, which explains my responsibilities.
  • [x] I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • [x] I signed the CLA.
  • [x] The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • [x] I listed at least one issue that this PR fixes in the description above.
  • [x] I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • [x] I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • [x] I updated/added relevant documentation (doc comments with ///).
  • [x] I added new tests to check the change I am making, or this PR is test-exempt.
  • [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

angjelkom avatar Sep 30 '22 21:09 angjelkom

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

flutter-dashboard[bot] avatar Sep 30 '22 21:09 flutter-dashboard[bot]

Hi @angjelkom , are you still planning to work on this pr?

chunhtai avatar Nov 11 '22 20:11 chunhtai

Hi @angjelkom , are you still planning to work on this pr?

Apologize, yes I will add your suggested change over the weeekend. I've been busy with work.

angjelkom avatar Nov 11 '22 20:11 angjelkom

@chunhtai I will need a bit more time, as merging the latest commits from the main branch caused my example to have weird side-effects where the Hero animation doesn't work when going to the next page but when going back it works in a buggy-weird way.

So until i solve that I can't commit the change for HeroControllerScope.

angjelkom avatar Nov 13 '22 00:11 angjelkom

@angjelkom are still planning on coming back to this pr?

chunhtai avatar Nov 30 '22 19:11 chunhtai

@chunhtai Sorry I am too busy at the moment with work, I will for sure complete it by the end of next week!

angjelkom avatar Dec 03 '22 13:12 angjelkom

@chunhtai I can't complete the pull request until #115832 its fixed, its blocking me.

I could use a lower commit but still I would need to test it against the latest commit at the end and currently that's not possible because of the above issue.

angjelkom avatar Dec 06 '22 19:12 angjelkom

@angjelkom I will take a look at the issue and try to resolve it asap

chunhtai avatar Dec 06 '22 19:12 chunhtai

@angjelkom, the issue you mentioned has been fixed. Could you update your PR for merging? If you are too busy, I could create an analogic PR. I need it solely for FirebaseAnalyticsObserver.

ycherniavskyi avatar Dec 08 '22 14:12 ycherniavskyi

@ycherniavskyi yes saw the merge today, I will proceed with completing the PR

angjelkom avatar Dec 08 '22 16:12 angjelkom

@ycherniavskyi the PR is ready waiting for @chunhtai to review it and approve it.

angjelkom avatar Dec 08 '22 21:12 angjelkom

any chance this will get merged soon?

arsen avatar Dec 13 '22 21:12 arsen

@arsen I will get on it tonight to finish it up.

angjelkom avatar Dec 14 '22 08:12 angjelkom

@chunhtai as @ycherniavskyi suggested we should use the relevant HeroController based on whether MaterialApp or CupertinoApp is used, in order to do that we need to use the RouteBuilder not the ShellRoute in order to access the context.

The issue is because the RouteBuilder gets rebuild while navigating the HeroController which again breaks the animation, the way I see it we have two options:

Option 1:

Create 3 final variables in the RouteBuilder, one using createMaterialHeroController, one using createCupertinoHeroController and a default HeroController. Then we can use those to pass the correct controller. This way the controllers won't be rebuild and the animation would work. (tested)

Option 2:

Have a heroController parameter to the ShellRoute so that the user passes one himself.

Let me know what would you recommend, I can do the change tonight.

angjelkom avatar Dec 15 '22 17:12 angjelkom

Option2 Vote!

matecode avatar Dec 15 '22 18:12 matecode

GoRouter uses isMaterialApp and isCupertinoApp to infer which Page subclass to use, maybe you can also use the same logic to create hero controllers?

chunhtai avatar Dec 15 '22 23:12 chunhtai

@chunhtai that's not what I meant, of course I would use isMaterialApp and isCupertinoApp, the situation is that creating the hero controller inside the RouteBuilder build method its not possible, because the build method gets called when navigating which breaks the animation.

So there are two solutions one is creating three final variables on the RouteBuilder containing the material, cupertino and default version of HeroController, and then use the isMaterialApp and isCupertinoApp to use the correct final variable controller.

The other solution is to add a heroController parameter to the ShellRoute and let the end user pass the controller himself.

So I'm asking what would be the better approach looking from api perspective?

Add a heroController parameter or solve it internally?

angjelkom avatar Dec 16 '22 06:12 angjelkom

@angjelkom, as for Option 1 - as I understand, we need to create HeroController for each Navigator (ShellRoute) 🤔; as for Option 2 - as I understand @chunhtai already rejects it.

What if we add the optional HeroController property in GoRouterState, then assign it within buildState method of RouteBuilder (where we can use isMaterialApp and isCupertinoApp) and finally use it for for HeroControllerScope as you did in MR. If changing GoRouterState isn't desirable, then an additional registry or even a simple map could be added to RouteBuilder for mapping corresponding HeroController with ShellRoute.

ycherniavskyi avatar Dec 16 '22 17:12 ycherniavskyi

Oh I see what's the issue now. can you create a cache in the RouteBuilder to store navkey to HeroController map? that way you don't recreate the herocontroller if it is in the cache.

I am not sure if this is the best solution because I think the real solution is to refactor herocontroller to not depend on Navigator, see https://github.com/flutter/flutter/issues/54200. but if everything is kept private, then I think it will be fine.

It can be smart about updating the cache when isMaterialApp changes, but I don't think that is a common use case.

chunhtai avatar Dec 16 '22 18:12 chunhtai

@chunhtai I completed the PR, but the dart_unit_tests are failing, locally running the commands work ok, what am I missing here?

angjelkom avatar Dec 18 '22 10:12 angjelkom

looked like the ci is blocked by https://github.com/flutter/packages/pull/2977

chunhtai avatar Dec 20 '22 21:12 chunhtai

@ycherniavskyi Thanks for the suggestion, @chunhtai I added the cache solution, yes looks like CI is blocked

angjelkom avatar Dec 20 '22 21:12 angjelkom

@angjelkom, could you please fix "some nits" mentioned by @chunhtai, so this PR finally could be merged?

ycherniavskyi avatar Jan 08 '23 14:01 ycherniavskyi

Will it be merged soon?

VusDanylo avatar Jan 10 '23 13:01 VusDanylo

@ycherniavskyi nits have been fixed, except for the better unit test, I am on holidays now I can have look during this weekend. If anyone can provide a test snippet now I can merge it sometimes today.

angjelkom avatar Jan 19 '23 10:01 angjelkom

@chunhtai sorry for pinging you but this PR feels like a very important fix. Using shell routes causes any analytic route observers (e.g. firebase or custom ones) to fail working correctly.

This PR has been open for months and imho it gives go_router maintenance a bad look - as well as any outside contribution. I'm also seeing the same issue for other PRs, such as #2650 or go_router_builder support for shell routes.

Is there anything the open source community can do to help?

jonasbark avatar Jan 30 '23 18:01 jonasbark

can't this PR just remove the additional HeroControllerScope feature and instead focus on the observers one ?

ahmednfwela avatar Jan 31 '23 05:01 ahmednfwela

As I understand, the last barrier before merging is extending the hero controller test. @chunhtai am I correct?

ycherniavskyi avatar Feb 01 '23 20:02 ycherniavskyi

As I understand, the last barrier before merging is extending the hero controller test. @chunhtai am I correct?

correct

chunhtai avatar Feb 01 '23 20:02 chunhtai

When will this be merged?

kamami avatar Feb 04 '23 17:02 kamami