-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[go_router_builder] Generate initialLocation with StatefulShellBranch…
…Config (#4880) Add the possibility to generate `initialLocation` with `StatefulShellBranchConfig` before: ```dart class MyRoute extends StatefulShellBranchData { const MyRoute(); } ``` after: ```dart class MyRoute extends StatefulShellBranchData { const MyRoute(); static const String $initialLocation = '/my-route'; } ``` *List which issues are fixed by this PR. You must list at least one issue.* [issues/130786](flutter/flutter#130786 (comment))
- Loading branch information
1 parent
59febcd
commit d08ebfd
Showing
8 changed files
with
442 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
276 changes: 276 additions & 0 deletions
276
packages/go_router_builder/example/lib/stateful_shell_route_initial_location_example.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,276 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// ignore_for_file: public_member_api_docs | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
part 'stateful_shell_route_initial_location_example.g.dart'; | ||
|
||
void main() => runApp(App()); | ||
|
||
class App extends StatelessWidget { | ||
App({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) => MaterialApp.router( | ||
routerConfig: _router, | ||
); | ||
|
||
final GoRouter _router = GoRouter( | ||
routes: $appRoutes, | ||
initialLocation: '/home', | ||
); | ||
} | ||
|
||
class HomeScreen extends StatelessWidget { | ||
const HomeScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) => Scaffold( | ||
appBar: AppBar(title: const Text('foo')), | ||
); | ||
} | ||
|
||
@TypedStatefulShellRoute<MainShellRouteData>( | ||
branches: <TypedStatefulShellBranch<StatefulShellBranchData>>[ | ||
TypedStatefulShellBranch<HomeShellBranchData>( | ||
routes: <TypedRoute<RouteData>>[ | ||
TypedGoRoute<HomeRouteData>( | ||
path: '/home', | ||
), | ||
], | ||
), | ||
TypedStatefulShellBranch<NotificationsShellBranchData>( | ||
routes: <TypedRoute<RouteData>>[ | ||
TypedGoRoute<NotificationsRouteData>( | ||
path: '/notifications/:section', | ||
), | ||
], | ||
), | ||
TypedStatefulShellBranch<OrdersShellBranchData>( | ||
routes: <TypedRoute<RouteData>>[ | ||
TypedGoRoute<OrdersRouteData>( | ||
path: '/orders', | ||
), | ||
], | ||
), | ||
], | ||
) | ||
class MainShellRouteData extends StatefulShellRouteData { | ||
const MainShellRouteData(); | ||
|
||
@override | ||
Widget builder( | ||
BuildContext context, | ||
GoRouterState state, | ||
StatefulNavigationShell navigationShell, | ||
) { | ||
return MainPageView( | ||
navigationShell: navigationShell, | ||
); | ||
} | ||
} | ||
|
||
class HomeShellBranchData extends StatefulShellBranchData { | ||
const HomeShellBranchData(); | ||
} | ||
|
||
class NotificationsShellBranchData extends StatefulShellBranchData { | ||
const NotificationsShellBranchData(); | ||
|
||
static String $initialLocation = '/notifications/old'; | ||
} | ||
|
||
class OrdersShellBranchData extends StatefulShellBranchData { | ||
const OrdersShellBranchData(); | ||
} | ||
|
||
class HomeRouteData extends GoRouteData { | ||
const HomeRouteData(); | ||
|
||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return const HomePageView(label: 'Home page'); | ||
} | ||
} | ||
|
||
enum NotificationsPageSection { | ||
latest, | ||
old, | ||
archive, | ||
} | ||
|
||
class NotificationsRouteData extends GoRouteData { | ||
const NotificationsRouteData({ | ||
required this.section, | ||
}); | ||
|
||
final NotificationsPageSection section; | ||
|
||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return NotificationsPageView( | ||
section: section, | ||
); | ||
} | ||
} | ||
|
||
class OrdersRouteData extends GoRouteData { | ||
const OrdersRouteData(); | ||
|
||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return const OrdersPageView(label: 'Orders page'); | ||
} | ||
} | ||
|
||
class MainPageView extends StatelessWidget { | ||
const MainPageView({ | ||
required this.navigationShell, | ||
super.key, | ||
}); | ||
|
||
final StatefulNavigationShell navigationShell; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(), | ||
body: navigationShell, | ||
bottomNavigationBar: BottomNavigationBar( | ||
items: const <BottomNavigationBarItem>[ | ||
BottomNavigationBarItem( | ||
icon: Icon(Icons.home), | ||
label: 'Home', | ||
), | ||
BottomNavigationBarItem( | ||
icon: Icon(Icons.favorite), | ||
label: 'Notifications', | ||
), | ||
BottomNavigationBarItem( | ||
icon: Icon(Icons.list), | ||
label: 'Orders', | ||
), | ||
], | ||
currentIndex: navigationShell.currentIndex, | ||
onTap: (int index) => _onTap(context, index), | ||
), | ||
); | ||
} | ||
|
||
void _onTap(BuildContext context, int index) { | ||
navigationShell.goBranch( | ||
index, | ||
initialLocation: index == navigationShell.currentIndex, | ||
); | ||
} | ||
} | ||
|
||
class HomePageView extends StatelessWidget { | ||
const HomePageView({ | ||
required this.label, | ||
super.key, | ||
}); | ||
|
||
final String label; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Center( | ||
child: Text(label), | ||
); | ||
} | ||
} | ||
|
||
class NotificationsPageView extends StatelessWidget { | ||
const NotificationsPageView({ | ||
super.key, | ||
required this.section, | ||
}); | ||
|
||
final NotificationsPageSection section; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return DefaultTabController( | ||
length: 3, | ||
initialIndex: NotificationsPageSection.values.indexOf(section), | ||
child: const Column( | ||
children: <Widget>[ | ||
TabBar( | ||
tabs: <Tab>[ | ||
Tab( | ||
child: Text( | ||
'Latest', | ||
style: TextStyle(color: Colors.black87), | ||
), | ||
), | ||
Tab( | ||
child: Text( | ||
'Old', | ||
style: TextStyle(color: Colors.black87), | ||
), | ||
), | ||
Tab( | ||
child: Text( | ||
'Archive', | ||
style: TextStyle(color: Colors.black87), | ||
), | ||
), | ||
], | ||
), | ||
Expanded( | ||
child: TabBarView( | ||
children: <Widget>[ | ||
NotificationsSubPageView( | ||
label: 'Latest notifications', | ||
), | ||
NotificationsSubPageView( | ||
label: 'Old notifications', | ||
), | ||
NotificationsSubPageView( | ||
label: 'Archived notifications', | ||
), | ||
], | ||
), | ||
), | ||
], | ||
), | ||
); | ||
} | ||
} | ||
|
||
class NotificationsSubPageView extends StatelessWidget { | ||
const NotificationsSubPageView({ | ||
required this.label, | ||
super.key, | ||
}); | ||
|
||
final String label; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Center( | ||
child: Text(label), | ||
); | ||
} | ||
} | ||
|
||
class OrdersPageView extends StatelessWidget { | ||
const OrdersPageView({ | ||
required this.label, | ||
super.key, | ||
}); | ||
|
||
final String label; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Center( | ||
child: Text(label), | ||
); | ||
} | ||
} |
Oops, something went wrong.