Advertisement
Guest User

shell route fun

a guest
Jul 16th, 2023
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 2.64 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'package:go_router/go_router.dart';
  3.  
  4. void main() async {
  5.   WidgetsFlutterBinding.ensureInitialized();
  6.   runApp(const MyApp());
  7. }
  8.  
  9. final locations = ["/home", "/network", "/storage"];
  10. final _rootNavigatorKey = GlobalKey<NavigatorState>();
  11.  
  12. String lastLocation = "/home";
  13.  
  14. final router = GoRouter(
  15.   navigatorKey: _rootNavigatorKey,
  16.   initialLocation: "/home",
  17.   routes: [
  18.     ShellRoute(
  19.       builder: (context, state, child) {
  20.         if (state.location.startsWith("/network")) {
  21.           return child;
  22.         } else {
  23.           lastLocation = state.location;
  24.           return Scaffold(
  25.             appBar: AppBar(title: Text(state.location)),
  26.             bottomNavigationBar: NavigationBar(
  27.               selectedIndex: locations.indexOf(state.location),
  28.               onDestinationSelected: (index) {
  29.                 context.go(locations[index]);
  30.               },
  31.               destinations: [
  32.                 for (final loc in locations)
  33.                   NavigationDestination(
  34.                     icon: Text(loc.substring(1, 2).toUpperCase()),
  35.                     label: loc.substring(1),
  36.                   ),
  37.               ],
  38.             ),
  39.             body: child,
  40.           );
  41.         }
  42.       },
  43.       routes: [
  44.         GoRoute(
  45.           path: "/home",
  46.           builder: (context, state) {
  47.             return const ColoredBox(
  48.               color: Colors.red,
  49.               child: Center(),
  50.             );
  51.           },
  52.         ),
  53.         GoRoute(
  54.           path: "/storage",
  55.           builder: (context, state) {
  56.             return const ColoredBox(
  57.               color: Colors.blue,
  58.               child: Center(),
  59.             );
  60.           },
  61.         ),
  62.         GoRoute(
  63.           path: "/network",
  64.           builder: (context, state) {
  65.             return Scaffold(
  66.               appBar: AppBar(
  67.                 automaticallyImplyLeading: false,
  68.                 leading: const CloseButton(),
  69.               ),
  70.               body: const ColoredBox(
  71.                 color: Colors.green,
  72.                 child: Center(),
  73.               ),
  74.             );
  75.           },
  76.         ),
  77.       ],
  78.     ),
  79.   ],
  80. );
  81.  
  82. class MyApp extends StatelessWidget {
  83.   const MyApp({super.key});
  84.  
  85.   @override
  86.   Widget build(BuildContext context) {
  87.     return MaterialApp.router(
  88.       routerConfig: router,
  89.     );
  90.   }
  91. }
  92.  
  93. class CloseButton extends StatelessWidget {
  94.   const CloseButton({super.key});
  95.  
  96.   @override
  97.   Widget build(BuildContext context) {
  98.     return IconButton(
  99.       onPressed: () => context.go(lastLocation),
  100.       icon: const Icon(Icons.close),
  101.     );
  102.   }
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement