Files
kooltab/lib/app/router.dart
T
2026-07-29 01:56:24 +02:00

86 lines
2.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:kooltab2/views/settings_view.dart';
import '../viewmodels/pin_lock_view_model.dart';
import '../views/bar_screen_view.dart';
import '../views/dev_menu_view.dart';
import '../views/history_screen_view.dart';
import '../views/pin_lock_view.dart';
import '../views/product_form_view.dart';
import '../views/product_list_view.dart';
final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();
GoRouter createAppRouter(PinLockViewModel pinLockViewModel) {
return GoRouter(
initialLocation: '/bar',
observers: [routeObserver],
refreshListenable: pinLockViewModel,
redirect: (context, state) {
if (!pinLockViewModel.hasLoaded) return null;
final needsSetup = !pinLockViewModel.isPinSet;
final isLocked =
pinLockViewModel.isPinSet && !pinLockViewModel.isUnlocked;
final mustBeOnLock = needsSetup || isLocked;
final goingToLock = state.matchedLocation == '/lock';
if (mustBeOnLock && !goingToLock) return '/lock';
if (!mustBeOnLock && goingToLock) return '/bar';
return null;
},
routes: [
GoRoute(path: '/', redirect: (context, state) => '/bar'),
GoRoute(
path: '/lock',
builder: (context, state) => PinEntryView(
mode: pinLockViewModel.isPinSet
? PinEntryMode.unlock
: PinEntryMode.create,
onSuccess: () {
context.go("/bar");
},
),
),
GoRoute(path: '/bar', builder: (context, state) => const BarScreenView()),
GoRoute(
path: '/products',
builder: (context, state) => const ProductListView(),
routes: [
GoRoute(
path: 'new',
builder: (context, state) => const ProductFormView(),
),
GoRoute(
path: ':id/edit',
builder: (context, state) {
final productId = state.pathParameters['id']!;
return ProductFormView(productId: productId);
},
),
],
),
GoRoute(
path: '/history',
builder: (context, state) => const HistoryScreenView(),
),
GoRoute(
path: '/settings',
builder: (context, state) => const SettingsScreenView(),
),
GoRoute(
path: '/dev',
builder: (context, state) => const DevMenuView(),
),
],
);
}