Migrate veggieseasons to go_router#1544
Migrate veggieseasons to go_router#1544miquelbeltran merged 23 commits intoflutter:mainfrom miquelbeltran:mb_veggieseasons_gorouter
veggieseasons to go_router#1544Conversation
|
@domesticmouse @johnpryan I'm having problems with the 'restoration smoke test' in the veggieseasons app when using go_router, part of the restoration tests work, but some don't. For example, the issue I am having is that the scroll position is not being restored when the route is restored, but seems that route restoration works as I am able to restore a route to a details screen. Could you do a review on the current PR code and see if you can find anything that I may have missed? Doing a bit of research, I've found this issue: flutter/flutter#99124 and this PR here: flutter/packages#2650 could this be related as well? |
| }, | ||
| debugLogDiagnostics: true, | ||
| routes: [ | ||
| ShellRoute( |
There was a problem hiding this comment.
Using a ShellRoute to present the contents for the navigation tabs.
| final index = _getSelectedIndex(GoRouter.of(context).location); | ||
| return RestorationScope( | ||
| restorationId: restorationId, | ||
| child: CupertinoTabScaffold( |
There was a problem hiding this comment.
CupertinoTabScaffold is gone, and is replaced by a CupertinoPageScaffold that contains the ShellRoute child and the CupertinoTabBar instead.
veggieseasons/lib/main.dart
Outdated
| builder: (context, state, child) { | ||
| return HomeScreen( | ||
| restorationId: 'home', | ||
| child: child, |
There was a problem hiding this comment.
pass the ShellRoute child to the HomeScreen, will be displayed inside the CupertinoTabScaffold
|
After a lot of trial and error, I think that GoRouter is the main cause on why the state of the screens is not correctly restored, I have created a new issue on the Flutter repo with all the info I could collect as well as widget tests that can reproduce the issue: flutter/flutter#117683 My conclusion is that simply put, GoRouter doesn't restore the state of the screens it creates inside its router configuration, and I fail to see what is needed to be done for that to work. I couldn't find any examples or documentation about this, so I am following up, and I'll try to update any relevant docs. |
|
@johnpryan love to get your eyes on this |
veggieseasons to go_router
|
@johnpryan PTAL |
|
According to the responses in flutter/flutter#117683 (comment) this should work with a combination of using I will have a look and retake this |
|
Looks like we have some lint fixes required. |
|
Yes, I am still fixing the unit tests. Part of the restoration tests are working, except for the details screen. The problem is with the go_router push function. Previously, the code used Otherwise, we are getting some progress here. |
|
Cool, ping me when this is ready for review. Thanks! |
veggieseasons to go_routerveggieseasons to go_router
| final _rootNavigatorKey = GlobalKey<NavigatorState>(); | ||
| final _shellNavigatorKey = GlobalKey<NavigatorState>(); |
There was a problem hiding this comment.
Different navigator key helps to open the detail pages inside or outside the shell navigator as a top modal screen
| ); | ||
| } | ||
|
|
||
| GoRoute _buildDetailsRoute() { |
There was a problem hiding this comment.
The details route is shared between the /list, /favorites and /search paths, but works exactly the same
|
|
||
| import 'package:flutter/cupertino.dart'; | ||
|
|
||
| class FadeTransitionPage<T> extends Page<T> { |
There was a problem hiding this comment.
There was a problem hiding this comment.
Hmm, maybe CustomTransitionPage needs add support for the restorationId, but this LGTM for now.
| return GestureDetector( | ||
| onTap: () => DetailsScreen.show(Navigator.of(context), veggie.id), | ||
| onTap: () { | ||
| context.go('${GoRouter.of(context).location}/details/${veggie.id}'); |
There was a problem hiding this comment.
Allows to dynamically call this from either /favorites or /search, and builds /favorites/details/1 or /search/details/1
There was a problem hiding this comment.
Yeah, go_router doesn't have support for relative routes (flutter/flutter#108177), it might be a good idea to add a comment here to explain what this line is doing.
|
@domesticmouse ready for review now, I updated the PR description with my implementation details |
|
PTAL @johnpryan |
johnpryan
left a comment
There was a problem hiding this comment.
LGTM overall, but the fade transitions aren't working for me.
veggieseasons/lib/main.dart
Outdated
| pageBuilder: (context, state) { | ||
| return FadeTransitionPage( | ||
| restorationId: 'route.list', | ||
| child: Builder(builder: (context) { |
There was a problem hiding this comment.
I'm curious, why are you using a Builder for these routes?
There was a problem hiding this comment.
Right, they don't seem to be necessary here!
| GoRoute( | ||
| path: '/list', | ||
| pageBuilder: (context, state) { | ||
| return FadeTransitionPage( |
There was a problem hiding this comment.
I'm not seeing a fade transition when I tap on destinations in the bottom navigation bar - is that what this is supposed to do?
There was a problem hiding this comment.
Edit: All good now! Turns out I had to pass the state.pageKey to the FadeTransitionPage in order to work.
|
|
||
| import 'package:flutter/cupertino.dart'; | ||
|
|
||
| class FadeTransitionPage<T> extends Page<T> { |
There was a problem hiding this comment.
Hmm, maybe CustomTransitionPage needs add support for the restorationId, but this LGTM for now.
| return GestureDetector( | ||
| onTap: () => DetailsScreen.show(Navigator.of(context), veggie.id), | ||
| onTap: () { | ||
| context.go('${GoRouter.of(context).location}/details/${veggie.id}'); |
There was a problem hiding this comment.
Yeah, go_router doesn't have support for relative routes (flutter/flutter#108177), it might be a good idea to add a comment here to explain what this line is doing.
Screencast.from.23.02.2023.15.05.40.webmFYI the transition animation is now fixed. |
johnpryan
left a comment
There was a problem hiding this comment.
LGTM - the CI failure looks unrelated to this change.
There's an open bug tracking that failure, I don't know how to fix it. |
|
@miquelbeltran land when you are ready |
Migration of veggieseaons to go_router.
Applying the same solution using
ShellRouteas in #1437ShellRouteto handle navigation within aCupertinoTabBar/listgoes to/list/details/1, opening details for the same item from favorites is/favorites/details/1, etc. This warranties that the navigation stack is always preserved when restoring the app.pushdid not work here, like pushing/details/1from/list, as the navigation stack was lost during restoration. This could be due to a limitation in the go_router package. The previous implementation usedrestorablePushwhich does not exist ongo_router.Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-devrel channel on Discord.