Files
jiu/client/lib/main.dart
T
wangjia 5dd7c07138 feat(client): 网络恢复自动刷新 + 离线嗅探 + 连通性感知登录
网络嗅探与自动恢复:
- ConnectivityNotifier:在线时 30s 检测,离线时切换为 1min 嗅探
- 新增 networkRecoveryCountProvider:网络恢复时自增,各数据 provider
  通过 ref.watch 实现自动重新加载
- inventory/product/stock_in/stock_out/partner provider 均已接入

FutureBuilder 类屏幕:
- finance_screen + batch_tracking_screen 通过 ref.listen 监听恢复事件

修复问题 1:API 超时 10s/30s → 5s/15s,减少无网络时等待
修复问题 2:offline banner 文字由"写操作已禁用"改为实际行为描述

连通性感知:
- app 启动时(_AppBootstrap)立即 forceCheck,路由跳转前状态已就绪
- 登录页:watch connectivityProvider,离线时显示警告 banner
- 登录按钮:离线状态下直接提示,不等待超时

测试:
- login_screen_test + login_flow_test 通过 ConnectivityNotifier(skipInit: true)
  覆盖 provider,避免测试环境中 pending HTTP timer

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-18 19:52:17 +08:00

90 lines
2.5 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'core/auth/auth_state.dart';
import 'core/router/app_router.dart';
import 'core/theme/app_theme.dart';
import 'providers/connectivity_provider.dart';
void main() {
FlutterError.onError = (details) {
FlutterError.presentError(details);
debugPrint('═══ FlutterError ════════════════════════════');
debugPrint(details.exceptionAsString());
debugPrint(details.stack.toString());
};
runZonedGuarded(
() => runApp(const ProviderScope(child: JiuApp())),
(error, stack) {
debugPrint('═══ Zone Error ═══════════════════════════════');
debugPrint(error.toString());
debugPrint(stack.toString());
},
);
}
class JiuApp extends ConsumerWidget {
const JiuApp({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return MaterialApp(
title: '酒库管理系统',
theme: AppTheme.light(),
debugShowCheckedModeBanner: false,
home: const _AppBootstrap(),
);
}
}
/// Restores persisted auth before handing off to the router.
class _AppBootstrap extends ConsumerStatefulWidget {
const _AppBootstrap();
@override
ConsumerState<_AppBootstrap> createState() => _AppBootstrapState();
}
class _AppBootstrapState extends ConsumerState<_AppBootstrap> {
bool _ready = false;
@override
void initState() {
super.initState();
_init();
}
Future<void> _init() async {
await ref.read(authStateProvider.notifier).restore();
// 启动时立即做一次连通性检测,确保 connectivityProvider 状态在路由跳转前已就绪
await ref.read(connectivityProvider.notifier).forceCheck();
if (mounted) setState(() => _ready = true);
}
@override
Widget build(BuildContext context) {
if (!_ready) {
return const Scaffold(
body: Center(child: CircularProgressIndicator()),
);
}
return _RouterApp();
}
}
class _RouterApp extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final router = ref.watch(appRouterProvider);
return MaterialApp.router(
title: '酒库管理系统',
theme: AppTheme.light(),
routerConfig: router,
debugShowCheckedModeBanner: false,
builder: (context, child) => SelectionArea(child: child!),
);
}
}