Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- import 'package:go_router/go_router.dart';
- void main() async {
- WidgetsFlutterBinding.ensureInitialized();
- runApp(const MyApp());
- }
- final locations = ["/home", "/network", "/storage"];
- final _rootNavigatorKey = GlobalKey<NavigatorState>();
- String lastLocation = "/home";
- final router = GoRouter(
- navigatorKey: _rootNavigatorKey,
- initialLocation: "/home",
- routes: [
- ShellRoute(
- builder: (context, state, child) {
- if (state.location.startsWith("/network")) {
- return child;
- } else {
- lastLocation = state.location;
- return Scaffold(
- appBar: AppBar(title: Text(state.location)),
- bottomNavigationBar: NavigationBar(
- selectedIndex: locations.indexOf(state.location),
- onDestinationSelected: (index) {
- context.go(locations[index]);
- },
- destinations: [
- for (final loc in locations)
- NavigationDestination(
- icon: Text(loc.substring(1, 2).toUpperCase()),
- label: loc.substring(1),
- ),
- ],
- ),
- body: child,
- );
- }
- },
- routes: [
- GoRoute(
- path: "/home",
- builder: (context, state) {
- return const ColoredBox(
- color: Colors.red,
- child: Center(),
- );
- },
- ),
- GoRoute(
- path: "/storage",
- builder: (context, state) {
- return const ColoredBox(
- color: Colors.blue,
- child: Center(),
- );
- },
- ),
- GoRoute(
- path: "/network",
- builder: (context, state) {
- return Scaffold(
- appBar: AppBar(
- automaticallyImplyLeading: false,
- leading: const CloseButton(),
- ),
- body: const ColoredBox(
- color: Colors.green,
- child: Center(),
- ),
- );
- },
- ),
- ],
- ),
- ],
- );
- class MyApp extends StatelessWidget {
- const MyApp({super.key});
- @override
- Widget build(BuildContext context) {
- return MaterialApp.router(
- routerConfig: router,
- );
- }
- }
- class CloseButton extends StatelessWidget {
- const CloseButton({super.key});
- @override
- Widget build(BuildContext context) {
- return IconButton(
- onPressed: () => context.go(lastLocation),
- icon: const Icon(Icons.close),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement