02082df525
Deploy / build-linux-web (push) Successful in 52s
Deploy / build-windows (push) Successful in 1m48s
Deploy / build-macos (push) Successful in 1m26s
Deploy / build-android (push) Successful in 1m32s
Deploy / build-ios (push) Successful in 7s
Deploy / release-deploy (push) Successful in 1m51s
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
128 lines
4.0 KiB
Dart
128 lines
4.0 KiB
Dart
import 'dart:async';
|
||
import 'dart:io';
|
||
import 'package:flutter/foundation.dart' show kDebugMode;
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import 'package:flutter_web_plugins/url_strategy.dart';
|
||
import 'core/auth/auth_state.dart';
|
||
import 'core/config/app_info.dart';
|
||
import 'core/errors/error_reporter.dart';
|
||
import 'core/platform/single_instance.dart' as single_instance;
|
||
import 'core/router/app_router.dart';
|
||
import 'core/theme/app_theme.dart';
|
||
import 'providers/connectivity_provider.dart';
|
||
|
||
void main() {
|
||
usePathUrlStrategy();
|
||
FlutterError.onError = (details) {
|
||
FlutterError.presentError(details);
|
||
debugPrint('═══ FlutterError ════════════════════════════');
|
||
debugPrint(details.exceptionAsString());
|
||
debugPrint(details.stack.toString());
|
||
ErrorReporter.instance.report(
|
||
errorType: ErrorType.flutterError,
|
||
errorMsg: details.exceptionAsString(),
|
||
stackTrace: details.stack,
|
||
);
|
||
};
|
||
|
||
runZonedGuarded(
|
||
() async {
|
||
WidgetsFlutterBinding.ensureInitialized();
|
||
|
||
if (!await single_instance.acquire()) {
|
||
runApp(const _AlreadyRunningApp());
|
||
return;
|
||
}
|
||
|
||
await AppInfo.load(); // 从配置文件加载「关于我们」品牌信息
|
||
runApp(const ProviderScope(child: JiuApp()));
|
||
},
|
||
(error, stack) {
|
||
debugPrint('═══ Zone Error ═══════════════════════════════');
|
||
debugPrint(error.toString());
|
||
debugPrint(stack.toString());
|
||
ErrorReporter.instance.report(
|
||
errorType: ErrorType.zoneError,
|
||
errorMsg: error.toString(),
|
||
stackTrace: stack,
|
||
);
|
||
},
|
||
);
|
||
}
|
||
|
||
class _AlreadyRunningApp extends StatelessWidget {
|
||
const _AlreadyRunningApp();
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return MaterialApp(
|
||
debugShowCheckedModeBanner: false,
|
||
home: Builder(
|
||
builder: (context) {
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
showDialog<void>(
|
||
context: context,
|
||
barrierDismissible: false,
|
||
builder: (_) => AlertDialog(
|
||
title: const Text('岩美酒库已在运行'),
|
||
content: const Text('请勿重复打开,程序已在运行中。\n请在任务栏或 Dock 中找到已打开的窗口。'),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () => exit(0),
|
||
child: const Text('确定'),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
});
|
||
return const Scaffold();
|
||
},
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
class JiuApp extends ConsumerStatefulWidget {
|
||
const JiuApp({super.key});
|
||
|
||
@override
|
||
ConsumerState<JiuApp> createState() => _JiuAppState();
|
||
}
|
||
|
||
class _JiuAppState extends ConsumerState<JiuApp> {
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
// 异步初始化:恢复 auth + 连通性检测
|
||
// 路由 redirect 在 initialized=false 时返回 null(不重定向),
|
||
// 初始化完成后 authState 变化触发 router refresh,redirect 重新执行
|
||
WidgetsBinding.instance.addPostFrameCallback((_) async {
|
||
await ref.read(authStateProvider.notifier).restore();
|
||
await ref.read(connectivityProvider.notifier).forceCheck();
|
||
});
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final router = ref.watch(appRouterProvider);
|
||
return MaterialApp.router(
|
||
title: kDebugMode ? '岩美 [DEBUG]' : '岩美',
|
||
theme: AppTheme.light(),
|
||
routerConfig: router,
|
||
debugShowCheckedModeBanner: false,
|
||
localizationsDelegates: const [
|
||
GlobalMaterialLocalizations.delegate,
|
||
GlobalWidgetsLocalizations.delegate,
|
||
GlobalCupertinoLocalizations.delegate,
|
||
],
|
||
supportedLocales: const [
|
||
Locale('zh', 'CN'),
|
||
Locale('en', 'US'),
|
||
],
|
||
locale: const Locale('zh', 'CN'),
|
||
);
|
||
}
|
||
}
|