- 添加 Web/Windows 平台支持 (flutter create --platforms) - 下载字体资源 (NotoSansSC/Caveat Regular+Bold) - Isar 3.x Web 不兼容:添加 kIsWeb 守卫,Web 上跳过 Isar 初始化 - IsarJournalRepository: instance 返回 nullable,Web 上使用 RemoteJournalRepository - SyncEngine: persistPendingQueue/restorePendingQueue Web 安全 - SettingsBloc: 从 RepositoryProvider 改为 ListenableProvider - ApiClient base URL: 8080 → 3000 匹配后端端口 - Isar .g.dart: 64 位 ID 替换为 JS 安全范围值
114 lines
3.9 KiB
Dart
114 lines
3.9 KiB
Dart
// 暖记 App 根组件 — MaterialApp + BLoC Provider 注入
|
||
//
|
||
// 依赖注入结构:
|
||
// MultiRepositoryProvider
|
||
// ├─ ApiClient
|
||
// ├─ AuthRepository
|
||
// ├─ JournalRepository (IsarJournalRepository — 离线优先)
|
||
// ├─ RemoteJournalRepository (供 SyncEngine 使用)
|
||
// └─ ClassRepository
|
||
// └─ BlocProvider<AuthBloc>
|
||
// └─ MaterialApp.router
|
||
|
||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:provider/provider.dart' show ListenableProvider;
|
||
|
||
import 'core/theme/app_theme.dart';
|
||
import 'core/routing/app_router.dart';
|
||
import 'data/remote/api_client.dart';
|
||
import 'data/repositories/auth_repository.dart';
|
||
import 'data/repositories/journal_repository.dart';
|
||
import 'data/repositories/isar_journal_repository.dart';
|
||
import 'data/repositories/remote_journal_repository.dart';
|
||
import 'data/repositories/class_repository.dart';
|
||
import 'data/services/sync_engine.dart';
|
||
import 'features/auth/bloc/auth_bloc.dart';
|
||
import 'features/profile/bloc/settings_bloc.dart';
|
||
|
||
/// 暖记 App — 根组件
|
||
class NuanjiApp extends StatelessWidget {
|
||
const NuanjiApp({super.key});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
// 创建全局依赖(App 生命周期内单例)
|
||
final apiClient = ApiClient();
|
||
final authRepository = AuthRepository(apiClient: apiClient);
|
||
// 离线优先:Isar 为主要本地仓库,Remote 供 SyncEngine 推送
|
||
// Web 平台:Isar 3.x 不支持 Web,直接使用远程仓库
|
||
final journalRepository = kIsWeb
|
||
? RemoteJournalRepository(api: apiClient)
|
||
: IsarJournalRepository();
|
||
final remoteJournalRepository = RemoteJournalRepository(api: apiClient);
|
||
final syncEngine = SyncEngine(apiClient: apiClient);
|
||
final classRepository = ClassRepository(api: apiClient);
|
||
final settingsBloc = SettingsBloc();
|
||
final authBloc = AuthBloc(authRepository: authRepository);
|
||
|
||
// 启动时检查认证状态
|
||
authBloc.add(const AppStarted());
|
||
|
||
// 异步恢复 SyncEngine 持久化队列(fire-and-forget,不阻塞 UI)
|
||
syncEngine.restorePendingQueue();
|
||
|
||
// 认证成功后注入 JWT token 到 ApiClient
|
||
authBloc.stream.listen((state) {
|
||
if (state is Authenticated) {
|
||
// TODO: 从 SecureStorage 读取 token 并设置
|
||
// apiClient.setToken(token);
|
||
} else {
|
||
apiClient.clearToken();
|
||
}
|
||
});
|
||
|
||
return MultiRepositoryProvider(
|
||
providers: [
|
||
RepositoryProvider<ApiClient>.value(value: apiClient),
|
||
RepositoryProvider<AuthRepository>.value(value: authRepository),
|
||
RepositoryProvider<JournalRepository>.value(value: journalRepository),
|
||
RepositoryProvider<RemoteJournalRepository>.value(value: remoteJournalRepository),
|
||
RepositoryProvider<SyncEngine>.value(value: syncEngine),
|
||
RepositoryProvider<ClassRepository>.value(value: classRepository),
|
||
],
|
||
child: ListenableProvider<SettingsBloc>.value(
|
||
value: settingsBloc,
|
||
child: Builder(
|
||
builder: (context) {
|
||
final settings = context.watch<SettingsBloc>();
|
||
return BlocProvider<AuthBloc>.value(
|
||
value: authBloc,
|
||
child: _AppView(
|
||
router: createAppRouter(authBloc),
|
||
themeMode: settings.state.themeMode,
|
||
),
|
||
);
|
||
},
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
/// App 视图 — MaterialApp.router 包装
|
||
class _AppView extends StatelessWidget {
|
||
final GoRouter router;
|
||
final ThemeMode themeMode;
|
||
|
||
const _AppView({required this.router, this.themeMode = ThemeMode.system});
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return MaterialApp.router(
|
||
title: '暖记',
|
||
debugShowCheckedModeBanner: false,
|
||
theme: AppTheme.light(),
|
||
darkTheme: AppTheme.dark(),
|
||
themeMode: themeMode,
|
||
routerConfig: router,
|
||
);
|
||
}
|
||
}
|