Files
jiu/client/lib/core/router/app_router.dart
T
wangjia 30a8e791a3 feat: 扫码商品详情页重设计 + 宣传主页 + nginx 路由完善
- public_product_screen: 1:1 复刻设计稿,新增商品介绍、批次防伪(自定义圆形印章)、完整商品参数表、门店营业时间、页脚链接
- app_theme: 完整品牌色系(brand/gray/accent/semantic)
- shop model: 新增 business_hours 字段,public 接口同步返回
- nginx: 新增 /product/ → Flutter app 扫码入口,修复 /docs /download html 后缀兼容
- web/: 新增宣传主页、功能页、文档页、扫码 HTML 及设计资源
- app_shell: 界面优化

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-23 18:58:04 +08:00

143 lines
5.3 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import '../../screens/auth/login_screen.dart';
import '../../screens/shell/app_shell.dart';
import '../../screens/stock_in/stock_in_list_screen.dart';
import '../../screens/stock_in/stock_in_form_screen.dart';
import '../../screens/stock_out/stock_out_list_screen.dart';
import '../../screens/stock_out/stock_out_form_screen.dart';
import '../../screens/inventory/inventory_list_screen.dart';
import '../../screens/inventory/inventory_check_screen.dart';
import '../../screens/partners/partners_screen.dart';
import '../../screens/finance/finance_screen.dart';
import '../../screens/products/products_screen.dart';
import '../../screens/products/product_detail_screen.dart';
import '../../screens/public/public_product_screen.dart';
import '../../screens/settings/settings_screen.dart';
import '../auth/auth_state.dart';
Page<void> _noTransition(Widget child) =>
NoTransitionPage<void>(child: child);
/// ChangeNotifier that bridges Riverpod auth state → GoRouter refreshListenable.
class _RouterNotifier extends ChangeNotifier {
final Ref _ref;
_RouterNotifier(this._ref) {
_ref.listen<AuthState>(authStateProvider, (prev, next) {
debugPrint('[Router] authState changed:'
' initialized=${next.initialized}'
' isLoggedIn=${next.isLoggedIn}'
' user=${next.user?.username}');
notifyListeners();
});
}
String? redirect(BuildContext context, GoRouterState state) {
final authState = _ref.read(authStateProvider);
final isLoggedIn = authState.isLoggedIn;
final loc = state.matchedLocation;
final isPublicRoute = loc == '/login' ||
loc.startsWith('/product/');
final result = !authState.initialized
? null
: (!isLoggedIn && !isPublicRoute)
? '/login'
: (isLoggedIn && loc == '/login')
? '/stock-in'
: null;
debugPrint('[Router] redirect: location=$loc'
' initialized=${authState.initialized}'
' isLoggedIn=$isLoggedIn'
'${result ?? "null (no redirect)"}');
return result;
}
}
/// Separate provider so that appRouterProvider has NO dependencies and
/// is never rebuilt when auth state changes (prevents router reset to /login).
final _routerNotifierProvider = Provider<_RouterNotifier>((ref) {
final notifier = _RouterNotifier(ref);
ref.onDispose(notifier.dispose);
return notifier;
});
final appRouterProvider = Provider<GoRouter>((ref) {
// Use ref.read (not ref.watch) so appRouterProvider never rebuilds on auth change.
final notifier = ref.read(_routerNotifierProvider);
final router = GoRouter(
initialLocation: '/login',
refreshListenable: notifier,
redirect: notifier.redirect,
routes: [
// Public product scan — no auth, no shell nav bar
GoRoute(
path: '/product/:public_id',
builder: (context, state) =>
PublicProductScreen(publicId: state.pathParameters['public_id']!),
),
GoRoute(
path: '/login',
builder: (context, state) => const LoginScreen(),
),
ShellRoute(
builder: (context, state, child) => AppShell(child: child),
routes: [
GoRoute(
path: '/stock-in',
pageBuilder: (_, __) => _noTransition(const StockInListScreen())),
GoRoute(
path: '/stock-in/new',
pageBuilder: (_, __) => _noTransition(const StockInFormScreen())),
GoRoute(
path: '/stock-in/edit/:id',
pageBuilder: (_, state) => _noTransition(
StockInFormScreen(
editOrderId: int.parse(state.pathParameters['id']!)))),
GoRoute(
path: '/stock-out',
pageBuilder: (_, __) => _noTransition(const StockOutListScreen())),
GoRoute(
path: '/stock-out/new',
pageBuilder: (_, __) => _noTransition(const StockOutFormScreen())),
GoRoute(
path: '/stock-out/edit/:id',
pageBuilder: (_, state) => _noTransition(
StockOutFormScreen(
editOrderId: int.parse(state.pathParameters['id']!)))),
GoRoute(
path: '/inventory',
pageBuilder: (_, __) =>
_noTransition(const InventoryListScreen())),
GoRoute(
path: '/inventory/check',
pageBuilder: (_, __) =>
_noTransition(const InventoryCheckScreen())),
GoRoute(
path: '/partners',
pageBuilder: (_, __) => _noTransition(const PartnersScreen())),
GoRoute(
path: '/finance',
pageBuilder: (_, __) => _noTransition(const FinanceScreen())),
GoRoute(
path: '/products',
pageBuilder: (_, __) => _noTransition(const ProductsScreen())),
GoRoute(
path: '/products/:id',
pageBuilder: (_, state) => _noTransition(ProductDetailScreen(
productId: int.parse(state.pathParameters['id']!)))),
GoRoute(
path: '/settings',
pageBuilder: (_, __) => _noTransition(const SettingsScreen())),
],
),
],
);
ref.onDispose(router.dispose);
return router;
});