routed_widget_switcher 1.0.1+6
routed_widget_switcher: ^1.0.1+6 copied to clipboard
Declaratively switch child widgets based on the current `Router` location.
example/lib/main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:go_router/go_router.dart';
import 'package:routed_widget_switcher/routed_widget_switcher.dart';
import 'widgets.dart';
void main() {
runApp(MyGoRouterApp());
}
class MyGoRouterApp extends StatefulWidget {
@override
State<MyGoRouterApp> createState() => _MyGoRouterAppState();
}
class _MyGoRouterAppState extends State<MyGoRouterApp> {
GoRoute routeFromPath(String path) => GoRoute(
path: path,
pageBuilder: (_, s) {
final content = InstructionsScaffold(child: Center(child: Text(path)));
return MaterialPage(key: s.pageKey, child: content);
});
late GoRouter goRouter = GoRouter(
navigatorBuilder: (_, __, navigator) {
return Row(children: [
const SideBar(),
Expanded(child: navigator),
]);
},
routes: [
routeFromPath('/'),
routeFromPath('/home1'),
routeFromPath('/home2'),
routeFromPath('/dashboard'),
routeFromPath('/dashboard/1'),
routeFromPath('/dashboard/2'),
routeFromPath('/settings'),
routeFromPath('/settings/1'),
routeFromPath('/settings/2'),
],
);
@override
void initState() {
super.initState();
WidgetsBinding.instance?.keyboard.addHandler((event) {
if (event is KeyDownEvent) {
final actions = {
LogicalKeyboardKey.digit1: '/',
LogicalKeyboardKey.digit2: '/home1',
LogicalKeyboardKey.digit3: '/home2',
LogicalKeyboardKey.digit4: '/dashboard',
LogicalKeyboardKey.digit5: '/dashboard/1',
LogicalKeyboardKey.digit6: '/dashboard/2',
LogicalKeyboardKey.digit7: '/settings',
LogicalKeyboardKey.digit8: '/settings/1',
LogicalKeyboardKey.digit9: '/settings/2',
};
if (actions.containsKey(event.logicalKey)) {
goRouter.go(actions[event.logicalKey]!);
}
}
return true;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routeInformationParser: goRouter.routeInformationParser,
routerDelegate: goRouter.routerDelegate,
);
}
}
class SideBar extends StatelessWidget {
const SideBar({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Material(
child: Container(
width: 250,
color: Colors.grey,
child: RoutedWidgetSwitcher(
builders: [
PathBuilder('/', builder: (_) => const MainMenu()),
PathBuilder('/dashboard', builder: (_) => const DashboardMenu()),
PathBuilder('/settings', builder: (_) => const SettingsMenu()),
],
),
),
);
}
}