Hugo Pointcheval 45458e7784
Some checks failed
continuous-integration/drone/pr Build is failing
chore: initialize go router package
2023-11-14 18:19:10 +01:00

109 lines
3.1 KiB
Dart

// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:wyatt_go_router/wyatt_go_router.dart';
import 'package:wyatt_go_router_example/bottom_bar.dart';
import 'package:wyatt_go_router_example/page_a.dart';
import 'package:wyatt_go_router_example/page_b.dart';
import 'package:wyatt_go_router_example/page_c.dart';
abstract class AppRouter {
/// Default transition for all pages
static Page<void> defaultTransition(
BuildContext context,
GoRouterState state,
Widget child,
) =>
CupertinoPage<void>(key: state.pageKey, child: child);
/// Disable transition animation
static Page<void> noTransition(
BuildContext context,
GoRouterState state,
Widget child,
) =>
NoTransitionPage(key: state.pageKey, child: child);
static final GlobalKey<NavigatorState> rootNavigatorKey =
GlobalKey<NavigatorState>();
static final GlobalKey<NavigatorState> shellNavigatorKey =
GlobalKey<NavigatorState>();
/// Defines GoRoute routes.
static final List<RouteBase> routes = [
ShellRoute(
navigatorKey: shellNavigatorKey,
builder: (context, state, child) {
final currentRoute = context.currentRoute;
return Scaffold(
appBar: AppBar(
title: Text(
currentRoute.name ?? 'Unknown',
),
),
body: child,
bottomNavigationBar: BottomBar(
currentRoute: currentRoute,
),
);
},
routes: [
GoRoute(
path: '/page-a',
name: 'A',
pageBuilder: (context, state) => noTransition(
context,
state,
const PageA(),
),
),
GoRoute(
path: '/page-b',
name: 'B',
pageBuilder: (context, state) => noTransition(
context,
state,
const PageB(),
),
),
],
),
GoRoute(
path: '/page-c',
name: 'C',
parentNavigatorKey: rootNavigatorKey,
pageBuilder: (context, state) => defaultTransition(
context,
state,
const PageC(),
),
),
];
static GoRouter? _router;
/// Returns the router.
static GoRouter routerOf(BuildContext context) => _router ??= GoRouter(
initialLocation: '/page-a',
routes: AppRouter.routes,
navigatorKey: rootNavigatorKey,
debugLogDiagnostics: true,
);
}