-
Notifications
You must be signed in to change notification settings - Fork 28.5k
[go_router] Add support for the TabView for more beautiful animation #112267
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
Comments
Perhaps this can also be solved with a solution like the one I proposed in a comment to #99124. That change proposes a new I was also able to create a workaround without modifying |
This feature can be added once flutter/packages#2650 is landed |
+1 for TabView support. You may want to also support PageView (in case we don't have tabs and want to scroll pages by gesture). |
Some have solution for this issue? Instead of auto_route package |
Unfortunately, |
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
The great flutter/packages#2650 PR has finally been merged! I understand, though, that animations were out of the scope. Are there any hopes for adding TabView or PageView support, so we can take profit of those animations? Or maybe there is another workaround to use those animations with the current |
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
Added functionality for building route configuration with support for preserving state in nested navigators. This change introduces a new shell route class called `StatefulShellRoute`, that uses separate navigators for its child routes as well as preserving state in each navigation branch. This is convenient when for instance implementing a UI with a `BottomNavigationBar`, with a persistent navigation state for each tab (i.e. building a `Navigator` for each tab). An example showcasing a UI with BottomNavigationBar and StatefulShellRoute has also been added ([`stateful_shell_route.dart`](https://github.com/tolo/flutter_packages/blob/nested-persistent-navigation/packages/go_router/example/lib/stateful_shell_route.dart)). Other examples of using `StatefulShellRoute` are also available in these repositories: * [stateful_books](https://github.com/tolo/stateful_books) - A fork of the Books example of go_router. * [stateful_navbar](https://github.com/tolo/stateful_navbar) - A clone of the Flutter Material 3 Navigation Bar example. <br/> Below is a short example of how a `StatefulShellRoute` can be setup: ```dart StatefulShellRoute( /// Each separate stateful navigation tree (i.e. Navigator) is represented by /// a StatefulShellBranch, which defines the routes that will be placed on that /// Navigator. StatefulShellBranch also makes it possible to configure /// things like an (optional) Navigator key, the default location (i.e. the /// location the branch will be navigated to when loading it for the first time) etc. branches: <StatefulShellBranch>[ StatefulShellBranch(navigatorKey: optionalNavigatorKey, routes: <RouteBase>[ GoRoute( path: '/a', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'A'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'A'), ), ], ), ]), /// The default location of a branch will by default be the first of the /// configured routes. To configure a different route, provide the /// defaultLocation parameter. StatefulShellBranch(defaultLocation: '/b/detail', routes: <RouteBase>[ GoRoute( path: '/b', builder: (BuildContext context, GoRouterState state) => const RootScreen(label: 'B'), routes: <RouteBase>[ GoRoute( path: 'details', builder: (BuildContext context, GoRouterState state) => const DetailsScreen(label: 'B'), ), ], ), ]), ], /// Like ShellRoute, the builder builds the navigation shell around the /// sub-routes, but with StatefulShellRoute, this navigation shell is able to /// maintain the state of the Navigators for each branch. The navigation shell /// could for instance use a BottomNavigationBar or similar. builder: (BuildContext context, StatefulShellRouteState state, Widget child) => ScaffoldWithNavBar(shellState: state, body: child), ) ``` This fixes issue flutter/flutter#99124. It also (at least partially) addresses flutter/flutter#112267.
You already have a |
There is a // GoRouter
final routerConfig = GoRouter(
initialLocation: '/central-manager',
navigatorKey: _rootNavigatorKey,
routes: [
StatefulShellRoute(
// builder: (context, state, navigationShell) {
// return HomeView(navigationShell: navigationShell);
// },
builder: (context, state, navigationShell) => navigationShell,
navigatorContainerBuilder: (context, navigationShell, children) {
return HomeView(
navigationShell: navigationShell,
children: children,
);
},
branches: [
StatefulShellBranch(
routes: [
GoRoute(
path: '/central-manager',
builder: (context, state) {
return const CentralManagerView();
},
routes: [
GoRoute(
path: 'peripheral',
builder: (context, state) {
final eventArgs = state.extra as DiscoveredEventArgs;
return PeripheralView(
eventArgs: eventArgs,
);
},
),
],
),
],
),
StatefulShellBranch(
routes: [
GoRoute(
path: '/peripheral-manager',
builder: (context, state) {
return const PeripheralManagerView();
},
),
],
),
],
),
],
); // HomeView
class HomeView extends StatefulWidget {
final StatefulNavigationShell navigationShell;
final List<Widget> children;
const HomeView({
super.key,
required this.navigationShell,
required this.children,
});
@override
State<HomeView> createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
late final PageController _controller;
@override
void initState() {
super.initState();
_controller = PageController(
initialPage: widget.navigationShell.currentIndex,
);
}
@override
Widget build(BuildContext context) {
final navigationShell = widget.navigationShell;
final children = widget.children;
return Scaffold(
// body: navigationShell,
// body: AnimatedBranchContainer(
// currentIndex: widget.navigationShell.currentIndex,
// children: widget.children,
// ),
body: PageView.builder(
controller: _controller,
onPageChanged: (index) {
debugPrint(
'index: $index, currentIndex: ${navigationShell.currentIndex}');
// Ignore tap events.
if (index == navigationShell.currentIndex) {
return;
}
navigationShell.goBranch(
index,
initialLocation: false,
);
},
itemBuilder: (context, index) {
return children[index];
},
itemCount: children.length,
),
bottomNavigationBar: BottomNavigationBar(
onTap: (index) {
navigationShell.goBranch(
index,
initialLocation: index == navigationShell.currentIndex,
);
},
items: const [
BottomNavigationBarItem(
icon: Icon(Icons.radar),
label: 'Central',
),
BottomNavigationBarItem(
icon: Icon(Icons.sensors),
label: 'Peripheral',
),
],
currentIndex: widget.navigationShell.currentIndex,
),
);
}
@override
void didUpdateWidget(covariant HomeView oldWidget) {
super.didUpdateWidget(oldWidget);
final navigationShell = widget.navigationShell;
final page = _controller.page ?? _controller.initialPage;
final index = page.round();
debugPrint('index: $index, currentIndex: ${navigationShell.currentIndex}');
// Ignore swipe events.
if (index == navigationShell.currentIndex) {
return;
}
_controller.animateToPage(
widget.navigationShell.currentIndex,
duration: const Duration(milliseconds: 300),
curve: Curves.linear,
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
} |
Any update on StatefulShellRoute.pageView ? This modified example of the custom example seems to work, do you see any limitations ? Enregistrement.2024-09-04.180815.mp4
|
…ple (#7583) Updated `custom_stateful_shell_route.dart` example to better support swiping in TabView. Also added code to demonstrate use of PageView instead of TabView. Note that to be fully effective from a usability perspective, the PR #6467 (branch preloading) need also be merged. This PR addresses: * flutter/flutter#150837 * flutter/flutter#112267 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] 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 [linked to 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], or this PR is [exempt from CHANGELOG changes]. - [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]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [pub versioning philosophy]: https://dart.dev/tools/pub/versioning [exempt from version changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version [following repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style [exempt from CHANGELOG changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
Use case
In our app we are using a TabBar with TabView. This create a nice animation when using the tabs.
Now with
go_router
5.0.0 we can use a ShellRoute but it returns the child in a Navigator. This works but we are losing the nice animation when switching tabs with a TabView.I was thinking maybe we can create a
TabBarShellRoute
which gets wrapped in aDefaultTabController
and then returns a child as aTabView
with all the child routes inside of which each have their ownNavigator
.Then maybe create a
TabViewGoRoute
which doesn't have a pageBuilder and of which the childroutes
are a list of the normalGoRoute
objects.Proposal
The text was updated successfully, but these errors were encountered: