125 lines
4.0 KiB
Dart
125 lines
4.0 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:get_it/get_it.dart';
|
|
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
|
import 'package:maibu_satabot_v2/core/consts/tcp_consts.dart';
|
|
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
|
import 'package:maibu_satabot_v2/core/network/tcp/tcp_client.dart';
|
|
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
|
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_state.dart'; // 👈 必须加
|
|
|
|
import '../../../devices/domain/entities/device_entity.dart';
|
|
import '../widgets/ImmersionHeader.dart';
|
|
import '../widgets/quick_actions_grid.dart';
|
|
import '../widgets/work_params_card.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({super.key});
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
bool _isInitializing = false;
|
|
bool _isDevicesLoading = true; // 👈 新加:设备加载中状态
|
|
bool _hasLoadedDevices = false;
|
|
|
|
/// 🔥 修复:等待设备加载完成,再执行后续逻辑
|
|
Future<void> _initializePage() async {
|
|
if (_isInitializing) return;
|
|
_isInitializing = true;
|
|
|
|
try {
|
|
final username = context.read<AppUserCubit>().state.user?.username;
|
|
if (username == null || username.isEmpty) {
|
|
setState(() => _isDevicesLoading = false);
|
|
return;
|
|
}
|
|
|
|
final devicesCubit = context.read<DevicesCubit>();
|
|
|
|
// 🔥 关键:等待设备加载完成
|
|
await devicesCubit.fetchAllDevices(username);
|
|
|
|
// 🔥 加载完成!关闭 loading
|
|
setState(() {
|
|
_isDevicesLoading = false;
|
|
_hasLoadedDevices = true;
|
|
});
|
|
|
|
// 🔥 现在再拿设备列表 = 最新的,绝对不是旧的!
|
|
final deviceList = devicesCubit.state.devices ?? [];
|
|
if (deviceList.isNotEmpty) {
|
|
final firstDevice = deviceList.first;
|
|
|
|
// 🔥 自动选中第一个设备(保证是最新的)
|
|
devicesCubit.selectDevice(firstDevice);
|
|
|
|
// 连接 TCP
|
|
final tcpClient = GetIt.instance<TcpClient>();
|
|
if (!tcpClient.isConnected) {
|
|
tcpClient.connectBySwitch(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT, deviceName: firstDevice.deviceName).then((_) {
|
|
debugPrint('✅ [HomePage] TCP 连接成功');
|
|
tcpClient.startHeartbeat(interval: const Duration(seconds: 4));
|
|
});
|
|
}
|
|
}
|
|
} catch (e) {
|
|
setState(() => _isDevicesLoading = false);
|
|
debugPrint('初始化异常:$e');
|
|
} finally {
|
|
_isInitializing = false;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
_initializePage();
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final userState = context.watch<AppUserCubit>().state;
|
|
final deviceState = context.watch<DevicesCubit>().state;
|
|
|
|
// 🔥 加载中 → 显示 Loading
|
|
if (_isDevicesLoading) {
|
|
return const Scaffold(body: Center(child: CircularProgressIndicator()));
|
|
}
|
|
|
|
// 🔥 现在取的设备 = 100% 最新加载完成的
|
|
final currentDevice = deviceState.selectedDevice;
|
|
|
|
final defaultDevice = DeviceEntity(
|
|
deviceName: AppLocalizations.of(context).translate('home.no_device_bound'),
|
|
productId: 00000,
|
|
productName: '',
|
|
tenantId: 00000,
|
|
tenantName: '',
|
|
);
|
|
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFFF7F7F7),
|
|
body: CustomScrollView(
|
|
physics: const BouncingScrollPhysics(),
|
|
slivers: [
|
|
SliverToBoxAdapter(child: ImmersionHeader(device: currentDevice ?? defaultDevice)),
|
|
const SliverToBoxAdapter(child: QuickActionsGrid()),
|
|
SliverToBoxAdapter(child: WorkParamsCard()),
|
|
const SliverToBoxAdapter(child: SizedBox(height: 120)),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
}
|
|
}
|