Files
hartmann-foto_documentation/hartmann-foto-documentation-frontend/lib/utils/global_router.dart
2026-01-30 14:30:53 +01:00

79 lines
3.1 KiB
Dart

// needed for web horizontal scroll behavior
import 'package:flutter/material.dart';
import 'package:fotodocumentation/main.dart';
import 'package:fotodocumentation/pages/customer/customer_list_widget.dart';
import 'package:fotodocumentation/pages/customer/customer_widget.dart';
import 'package:fotodocumentation/pages/customer/picture_widget.dart';
import 'package:fotodocumentation/pages/login/login_widget.dart';
import 'package:fotodocumentation/utils/di_container.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import 'package:go_router/go_router.dart';
class GlobalRouter {
static final GlobalKey<NavigatorState> rootNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'root');
static final GlobalKey<NavigatorState> bottomBarNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'bottombar');
static final GlobalKey<NavigatorState> adminNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'admin');
static final GlobalKey<NavigatorState> skillEditorNavigatorKey = GlobalKey<NavigatorState>(debugLabel: 'skillEditor');
static final String pathHome = "/home";
static final String pathCustomer = "/customer";
static final String pathPicture = "/picture";
static final String pathLogin = "/login";
static final GoRouter router = createRouter(pathHome);
static GoRouter createRouter(String initialLocation) {
return GoRouter(
navigatorKey: rootNavigatorKey,
initialLocation: initialLocation,
routes: <RouteBase>[
GoRoute(
path: "/",
redirect: (_, __) => pathHome,
),
GoRoute(
path: pathLogin,
builder: (BuildContext context, GoRouterState state) => const LoginWidget(),
),
GoRoute(
path: pathHome,
builder: (context, state) => CustomerListWidget(),
routes: [
GoRoute(
path: "$pathCustomer/:customerId",
builder: (context, state) {
var idStr = state.pathParameters['customerId'];
var id = idStr == null ? null : int.tryParse(idStr);
return CustomerWidget(customerId: id ?? -1);
},
routes: [
GoRoute(
path: "$pathPicture/:pictureId",
builder: (context, state) {
var customerIdStr = state.pathParameters['customerId'];
var customerId = customerIdStr == null ? null : int.tryParse(customerIdStr);
var pictureIdStr = state.pathParameters['pictureId'];
var pictureId = pictureIdStr == null ? null : int.tryParse(pictureIdStr);
return PictureWidget(customerId: customerId ?? -1, pictureId: pictureId ?? -1);
},
),
],
),
],
),
],
redirect: (context, state) {
var uriStr = state.uri.toString();
logger.t("uri $uriStr");
LoginCredentials loginCredentials = DiContainer.get();
if (!loginCredentials.isLoggedIn) {
return pathLogin;
}
return null;
},
);
}
}