-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Flutter Web - Side Nav Example Using ShellRoute #6603
Open
alifarhanaf
wants to merge
3
commits into
flutter:main
Choose a base branch
from
alifarhanaf:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
350 changes: 350 additions & 0 deletions
350
packages/go_router/example/lib/web-side-nav-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,350 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:side_navigation/side_navigation.dart'; | ||
|
||
final GlobalKey<NavigatorState> _rootNavigatorKey = | ||
GlobalKey<NavigatorState>(debugLabel: 'root'); | ||
final GlobalKey<NavigatorState> _shellNavigatorKey = | ||
GlobalKey<NavigatorState>(debugLabel: 'shell'); | ||
final GlobalKey<NavigatorState> _internalNavigatorKey = | ||
GlobalKey<NavigatorState>(debugLabel: 'internal'); | ||
|
||
void main() { | ||
runApp(ShellRouteExampleAppForWeb()); | ||
} | ||
|
||
/// An example demonstrating how to use [ShellRoute] | ||
class ShellRouteExampleAppForWeb extends StatelessWidget { | ||
/// Creates a [ShellRouteExampleAppForWeb] | ||
ShellRouteExampleAppForWeb({super.key}); | ||
|
||
final GoRouter _router = GoRouter( | ||
navigatorKey: _rootNavigatorKey, | ||
initialLocation: '/a/d', | ||
debugLogDiagnostics: true, | ||
routes: <RouteBase>[ | ||
/// Application shell | ||
ShellRoute( | ||
navigatorKey: _shellNavigatorKey, | ||
builder: (BuildContext context, GoRouterState state, Widget child) { | ||
return ScaffoldWithNavBar(child: child); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: '/a', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return Container(); | ||
}, | ||
routes: <RouteBase>[ | ||
ShellRoute( | ||
navigatorKey: _internalNavigatorKey, | ||
builder: | ||
(BuildContext context, GoRouterState state, Widget child) { | ||
return InternalScaffoldWithNavBar(child: child); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'd', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const Screen( | ||
label: "D (Sub Menu of A)", | ||
dynamicRoute: '/a/d/details', | ||
); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const DetailsScreen(label: 'D'); | ||
}, | ||
), | ||
], | ||
), | ||
GoRoute( | ||
path: 'e', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const Screen( | ||
label: "E (Sub Menu of A)", | ||
dynamicRoute: '/a/e/details', | ||
); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
parentNavigatorKey: _shellNavigatorKey, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const DetailsScreen(label: 'E'); | ||
}, | ||
), | ||
], | ||
), | ||
GoRoute( | ||
path: 'f', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const Screen( | ||
label: "F (Sub Menu of A)", | ||
dynamicRoute: '/a/f/details', | ||
); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
parentNavigatorKey: _rootNavigatorKey, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const DetailsScreen(label: 'F'); | ||
}, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
), | ||
GoRoute( | ||
path: '/b', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const Screen( | ||
label: "Menu B", | ||
dynamicRoute: '/b/details', | ||
); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const DetailsScreen(label: 'B'); | ||
}, | ||
), | ||
], | ||
), | ||
GoRoute( | ||
path: '/c', | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const Screen( | ||
label: "Menu C", | ||
dynamicRoute: '/c/details', | ||
); | ||
}, | ||
routes: <RouteBase>[ | ||
GoRoute( | ||
path: 'details', | ||
parentNavigatorKey: _rootNavigatorKey, | ||
builder: (BuildContext context, GoRouterState state) { | ||
return const DetailsScreen(label: 'C'); | ||
}, | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return MaterialApp.router( | ||
title: 'Flutter Demo', | ||
theme: ThemeData( | ||
primarySwatch: Colors.blue, | ||
), | ||
routerConfig: _router, | ||
); | ||
} | ||
} | ||
|
||
class ScaffoldWithNavBar extends StatelessWidget { | ||
/// Constructs an [ScaffoldWithNavBar]. | ||
const ScaffoldWithNavBar({ | ||
required this.child, | ||
super.key, | ||
}); | ||
|
||
final Widget child; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Row( | ||
children: [ | ||
SideNavigationBar( | ||
selectedIndex: _calculateSelectedIndex(context), | ||
items: const [ | ||
SideNavigationBarItem( | ||
icon: Icons.dashboard, | ||
label: 'Menu A', | ||
), | ||
SideNavigationBarItem( | ||
icon: Icons.dashboard, | ||
label: 'Menu B', | ||
), | ||
SideNavigationBarItem( | ||
icon: Icons.dashboard, | ||
label: 'Menu C', | ||
), | ||
], | ||
onTap: (int idx) => _onItemTapped(idx, context)), | ||
Container( | ||
height: MediaQuery.of(context).size.height, | ||
width: 2, | ||
color: Colors.black12, | ||
), | ||
Expanded( | ||
child: child, | ||
) | ||
], | ||
), | ||
); | ||
} | ||
|
||
static int _calculateSelectedIndex(BuildContext context) { | ||
final String location = GoRouterState.of(context).uri.path; | ||
if (location.startsWith('/a')) { | ||
return 0; | ||
} | ||
if (location.startsWith('/b')) { | ||
return 1; | ||
} | ||
if (location.startsWith('/c')) { | ||
return 2; | ||
} | ||
return 0; | ||
} | ||
|
||
void _onItemTapped(int index, BuildContext context) { | ||
switch (index) { | ||
case 0: | ||
GoRouter.of(context).go('/a/d'); | ||
case 1: | ||
GoRouter.of(context).go('/b'); | ||
case 2: | ||
GoRouter.of(context).go('/c'); | ||
} | ||
} | ||
} | ||
|
||
class InternalScaffoldWithNavBar extends StatelessWidget { | ||
const InternalScaffoldWithNavBar({ | ||
required this.child, | ||
super.key, | ||
}); | ||
final Widget child; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Row( | ||
children: [ | ||
SideNavigationBar( | ||
selectedIndex: _calculateInternalSelectedIndex(context), | ||
items: const [ | ||
SideNavigationBarItem( | ||
icon: Icons.dashboard, | ||
label: 'D (Sub Menu of A)', | ||
), | ||
SideNavigationBarItem( | ||
icon: Icons.dashboard, | ||
label: 'E (Sub Menu of A)', | ||
), | ||
SideNavigationBarItem( | ||
icon: Icons.dashboard, | ||
label: 'F (Sub Menu of A)', | ||
), | ||
], | ||
onTap: (int idx) => _onInternalItemTapped(idx, context)), | ||
Container( | ||
height: MediaQuery.of(context).size.height, | ||
width: 2, | ||
color: Colors.black12, | ||
), | ||
Expanded( | ||
child: child, | ||
) | ||
], | ||
), | ||
); | ||
} | ||
|
||
static int _calculateInternalSelectedIndex(BuildContext context) { | ||
final String location = GoRouterState.of(context).uri.path; | ||
if (location.startsWith('/a/d')) { | ||
return 0; | ||
} | ||
if (location.startsWith('/a/e')) { | ||
return 1; | ||
} | ||
if (location.startsWith('/a/f')) { | ||
return 2; | ||
} | ||
return 0; | ||
} | ||
|
||
void _onInternalItemTapped(int index, BuildContext context) { | ||
switch (index) { | ||
case 0: | ||
GoRouter.of(context).go('/a/d'); | ||
case 1: | ||
GoRouter.of(context).go('/a/e'); | ||
case 2: | ||
GoRouter.of(context).go('/a/f'); | ||
} | ||
} | ||
} | ||
|
||
class Screen extends StatelessWidget { | ||
/// Constructs a [ScreenA] widget. | ||
const Screen({super.key, required this.label, required this.dynamicRoute}); | ||
final String label; | ||
final String dynamicRoute; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
appBar: AppBar(), | ||
body: Center( | ||
child: Column( | ||
mainAxisSize: MainAxisSize.min, | ||
children: <Widget>[ | ||
Text('Screen $label'), | ||
TextButton( | ||
onPressed: () { | ||
GoRouter.of(context).go(dynamicRoute); | ||
}, | ||
child: Text('View $label details'), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} | ||
|
||
/// The details screen for either the A, B or C screen. | ||
class DetailsScreen extends StatelessWidget { | ||
/// Constructs a [DetailsScreen]. | ||
const DetailsScreen({ | ||
required this.label, | ||
super.key, | ||
}); | ||
|
||
/// The label to display in the center of the screen. | ||
final String label; | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Container( | ||
width: 400, | ||
color: Colors.red, | ||
child: Scaffold( | ||
appBar: AppBar( | ||
title: const Text('Details Screen'), | ||
), | ||
body: Center( | ||
child: Text( | ||
'Details for $label', | ||
style: Theme.of(context).textTheme.headlineMedium, | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
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,38 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:go_router_examples/web-side-nav-example.dart'; | ||
|
||
void main() { | ||
group('Navigation tests', () { | ||
testWidgets('Initial route is /a/d', (WidgetTester tester) async { | ||
await tester.pumpWidget(ShellRouteExampleAppForWeb()); | ||
|
||
final state = GoRouterState.of(tester.element(find.byType(ScaffoldWithNavBar))); | ||
expect(state.uri.path, '/a/d'); | ||
}); | ||
|
||
testWidgets('Navigation to /b', (WidgetTester tester) async { | ||
await tester.pumpWidget(ShellRouteExampleAppForWeb()); | ||
await tester.tap(find.text('Menu B')); | ||
|
||
await tester.pumpAndSettle(); | ||
|
||
final state = GoRouterState.of(tester.element(find.byType(ScaffoldWithNavBar))); | ||
expect(state.uri.path, '/b'); | ||
}); | ||
|
||
// Add more tests for navigation to /c, /a/e, /a/f, /a/d/details, etc. | ||
}); | ||
|
||
group('UI rendering tests', () { | ||
testWidgets('Check if all menu items are displayed', (WidgetTester tester) async { | ||
await tester.pumpWidget(ShellRouteExampleAppForWeb()); | ||
|
||
expect(find.text('Menu A'), findsOneWidget); | ||
expect(find.text('Menu B'), findsOneWidget); | ||
expect(find.text('Menu C'), findsOneWidget); | ||
}); | ||
|
||
// Add more tests to check UI rendering for different screens and components. | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file needs to be in https://github.com/flutter/packages/tree/0bd0307e2187bfe873b6a3fe80219eb0799406c1/packages/go_router/example/test
and we use underscore for file name. same for the example file