Skip to content
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_adaptive_scaffold] Go router sample for AdaptiveScaffold #7452

Merged
merged 19 commits into from
Sep 4, 2024
martijn00 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.

import 'package:flutter/material.dart';

import 'go_router_demo/app_router.dart';

void main() {
runApp(const MyApp());
}

/// The main application widget for this example.
class MyApp extends StatelessWidget {
/// Creates a const main application widget.
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp.router(
restorationScopeId: 'demo',
routerConfig: AppRouter.router,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.blue,
dynamicSchemeVariant: DynamicSchemeVariant.vibrant,
primary: Colors.blue,
),
useMaterial3: true,
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import 'pages/pages.dart';
import 'scaffold_shell.dart';

/// The root navigator key for the main router of the app.
final GlobalKey<NavigatorState> rootNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'root');

final GlobalKey<NavigatorState> _homeNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'home');
final GlobalKey<NavigatorState> _counterNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'counter');
final GlobalKey<NavigatorState> _moreNavigatorKey =
GlobalKey<NavigatorState>(debugLabel: 'more');

/// The [AppRouter] maintains the main route configuration for the app.
///
/// Routes that are `fullScreenDialogs` should also set `_rootNavigatorKey` as
/// the `parentNavigatorKey` to ensure that the dialog is displayed correctly.
class AppRouter {
/// The authentication status of the user.
static ValueNotifier<bool> authenticatedNotifier = ValueNotifier<bool>(false);

/// The router with the routes of pages that should be displayed.
static final GoRouter router = GoRouter(
navigatorKey: rootNavigatorKey,
debugLogDiagnostics: true,
errorPageBuilder: (BuildContext context, GoRouterState state) {
return const MaterialPage<void>(child: NavigationErrorPage());
},
redirect: (BuildContext context, GoRouterState state) {
if (state.uri.path == '/') {
return HomePage.path;
}
return null;
},
refreshListenable: authenticatedNotifier,
routes: <RouteBase>[
_unauthenticatedRoutes,
_authenticatedRoutes,
..._openRoutes,
],
);

static final GoRoute _unauthenticatedRoutes = GoRoute(
name: LoginPage.name,
path: LoginPage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return const MaterialPage<void>(child: LoginPage());
},
redirect: (BuildContext context, GoRouterState state) {
if (authenticatedNotifier.value) {
return HomePage.path;
}
return null;
},
routes: <RouteBase>[
GoRoute(
name: ForgotPasswordPage.name,
path: ForgotPasswordPage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return const MaterialPage<void>(
child: ForgotPasswordPage(),
);
},
),
],
);

static final StatefulShellRoute _authenticatedRoutes =
StatefulShellRoute.indexedStack(
parentNavigatorKey: rootNavigatorKey,
builder: (
BuildContext context,
GoRouterState state,
StatefulNavigationShell navigationShell,
) {
return ScaffoldShell(navigationShell: navigationShell);
},
redirect: (BuildContext context, GoRouterState state) {
if (!authenticatedNotifier.value) {
return LoginPage.path;
}
return null;
},
branches: <StatefulShellBranch>[
StatefulShellBranch(
navigatorKey: _homeNavigatorKey,
routes: <RouteBase>[
GoRoute(
name: HomePage.name,
path: HomePage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return const NoTransitionPage<void>(
child: HomePage(),
);
},
routes: <RouteBase>[
GoRoute(
name: DetailOverviewPage.name,
path: DetailOverviewPage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return const MaterialPage<void>(
child: DetailOverviewPage(),
);
},
routes: <RouteBase>[
GoRoute(
name: DetailPage.name,
path: DetailPage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return MaterialPage<void>(
child: DetailPage(
itemName: state.uri.queryParameters['itemName']!),
);
},
),
]),
GoRoute(
name: DetailModalPage.name,
path: DetailModalPage.path,
parentNavigatorKey: rootNavigatorKey,
pageBuilder: (BuildContext context, GoRouterState state) {
return const MaterialPage<void>(
fullscreenDialog: true,
child: DetailModalPage(),
);
},
),
],
),
],
),
StatefulShellBranch(
navigatorKey: _counterNavigatorKey,
routes: <RouteBase>[
GoRoute(
name: CounterPage.name,
path: CounterPage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return const NoTransitionPage<void>(child: CounterPage());
},
),
],
),
StatefulShellBranch(
navigatorKey: _moreNavigatorKey,
routes: <RouteBase>[
GoRoute(
name: MorePage.name,
path: MorePage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return const NoTransitionPage<void>(
key: ValueKey<String>(MorePage.name),
child: MorePage(),
);
},
routes: <RouteBase>[
GoRoute(
path: ProfilePage.path,
name: ProfilePage.name,
pageBuilder: (BuildContext context, GoRouterState state) {
return const MaterialPage<void>(child: ProfilePage());
},
),
GoRoute(
name: SettingsPage.name,
path: SettingsPage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return const MaterialPage<void>(child: SettingsPage());
},
),
],
),
],
),
],
);

static final List<GoRoute> _openRoutes = <GoRoute>[
GoRoute(
name: LanguagePage.name,
path: LanguagePage.path,
pageBuilder: (BuildContext context, GoRouterState state) {
return const MaterialPage<void>(
child: LanguagePage(),
);
},
),
];
}
martijn00 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';

/// The counter page.
class CounterPage extends StatelessWidget {
/// Construct the counter page.
const CounterPage({super.key});

/// The path for the counter page.
static const String path = '/counter';

/// The name for the counter page.
static const String name = 'Counter';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Counter Page'),
),
body: const Center(
child: Text('Counter Page'),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';

/// The detail modal page.
class DetailModalPage extends StatelessWidget {
/// Construct the detail modal page.
const DetailModalPage({super.key});

/// The path for the detail modal page.
static const String path = 'detail-modal';

/// The name for the detail modal page.
static const String name = 'DetailModal';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Detail Modal Page'),
),
body: const Center(
child: Text('Detail modal Page'),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

import 'detail_page.dart';

/// The detail overview page.
class DetailOverviewPage extends StatelessWidget {
/// Construct the detail overview page.
const DetailOverviewPage({super.key});

/// The path for the detail page.
static const String path = 'detail-overview';

/// The name for the detail page.
static const String name = 'DetailOverview';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Detail Overview Page'),
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Item $index'),
onTap: () {
context.goNamed(
DetailPage.name,
queryParameters: <String, String>{'itemName': '$index'},
);
},
);
},
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:flutter/material.dart';

/// The detail page.
class DetailPage extends StatelessWidget {
/// Construct the detail page.
const DetailPage({super.key, required this.itemName});

/// The path for the detail page.
static const String path = 'detail';

/// The name for the detail page.
static const String name = 'Detail';

/// The item name for the detail page.
final String itemName;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Detail Page'),
),
body: Center(
child: Text('Detail Page: $itemName'),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';

/// The forgot password page.
class ForgotPasswordPage extends StatelessWidget {
/// Construct the forgot password page.
const ForgotPasswordPage({super.key});

/// The path for the forgot password page.
static const String path = 'forgot_password';

/// The name for the forgot password page.
static const String name = 'ForgotPassword';

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Forgot Password'),
),
body: const Center(
child: Text('ForgotPassword Page'),
),
);
}
}
Loading