From d5cde33b3ebb0cd9e3b67be9965ca2a78719e81d Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Tue, 17 Mar 2026 11:05:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=AF=8F=E6=AC=A1=20=20?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E4=B9=8B=E5=90=8E=20=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E8=AE=BE=E5=A4=87=E6=98=AF=20=20=E4=B8=8A?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E7=99=BB=E5=BD=95=E7=9A=84=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../home/presentation/pages/home_page.dart | 114 +++++------------- 1 file changed, 33 insertions(+), 81 deletions(-) diff --git a/lib/features/home/presentation/pages/home_page.dart b/lib/features/home/presentation/pages/home_page.dart index 9c813e73..a35316ad 100644 --- a/lib/features/home/presentation/pages/home_page.dart +++ b/lib/features/home/presentation/pages/home_page.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:get_it/get_it.dart'; @@ -5,6 +6,7 @@ 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/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'; @@ -19,75 +21,53 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { - bool _isInitializing = false; // 标记是否正在初始化 - bool _hasLoadedDevices = false; // 标记是否已加载过设备列表 + bool _isInitializing = false; + bool _isDevicesLoading = true; // 👈 新加:设备加载中状态 + bool _hasLoadedDevices = false; - /// 🔥 核心方法:页面显示前的初始化操作 + /// 🔥 修复:等待设备加载完成,再执行后续逻辑 Future _initializePage() async { - if (_isInitializing) { - debugPrint('[HomePage] 正在初始化,跳过重复调用'); - return; - } - + if (_isInitializing) return; _isInitializing = true; try { - final userState = context.read().state; - final String? username = userState.user?.username; - + final username = context.read().state.user?.username; if (username == null || username.isEmpty) { - debugPrint('[HomePage] 用户未登录,跳过初始化'); + setState(() => _isDevicesLoading = false); return; } final devicesCubit = context.read(); - // 🔥 步骤 1: 先检查设备列表是否需要刷新 - if (!_hasLoadedDevices && (devicesCubit.state.devices == null || devicesCubit.state.devices!.isEmpty)) { - debugPrint('[HomePage] 设备列表为空,触发获取...'); - - // ❌ 不要 await,让它后台执行 - devicesCubit.fetchAllDevices(username); - - // 🔥 等待一小段时间让数据过来 - // await Future.delayed(const Duration(milliseconds: 500)); + // 🔥 关键:等待设备加载完成 + await devicesCubit.fetchAllDevices(username); + // 🔥 加载完成!关闭 loading + setState(() { + _isDevicesLoading = false; _hasLoadedDevices = true; - } - - // 🔥 步骤 2: 获取最新设备列表 - final deviceState = devicesCubit.state; - final List deviceList = deviceState.devices ?? []; + }); + // 🔥 现在再拿设备列表 = 最新的,绝对不是旧的! + final deviceList = devicesCubit.state.devices ?? []; if (deviceList.isNotEmpty) { - // 🔥 步骤 3: TCP 连接(异步执行,不阻塞 UI) - final firstDevice = deviceState.selectedDevice ?? deviceList.first; - debugPrint('🔌 [HomePage] 准备连接设备:${firstDevice.deviceName}'); + final firstDevice = deviceList.first; + // 🔥 自动选中第一个设备(保证是最新的) + devicesCubit.selectDevice(firstDevice); + + // 连接 TCP final tcpClient = GetIt.instance(); - var bool = tcpClient.isConnected; - debugPrint('tcpClient,$bool'); if (!tcpClient.isConnected) { - // ❌ 异步执行,不阻塞 - tcpClient.connectBySwitch( - host: TCPConsts.TCP_IP, - port: TCPConsts.TCP_PORT, - deviceName: firstDevice.deviceName - ).then((_) { + tcpClient.connectBySwitch(host: TCPConsts.TCP_IP, port: TCPConsts.TCP_PORT, deviceName: firstDevice.deviceName).then((_) { debugPrint('✅ [HomePage] TCP 连接成功'); tcpClient.startHeartbeat(interval: const Duration(seconds: 4)); - }).catchError((e) { - debugPrint('❌ [HomePage] TCP 连接失败:$e'); }); - } else { - debugPrint('✅ [HomePage] TCP 已连接'); } - } else { - debugPrint('⚠️ [HomePage] 设备列表仍为空'); } - } catch (e) { - debugPrint('❌ [HomePage] 初始化异常:$e'); + setState(() => _isDevicesLoading = false); + debugPrint('初始化异常:$e'); } finally { _isInitializing = false; } @@ -96,10 +76,6 @@ class _HomePageState extends State { @override void initState() { super.initState(); - debugPrint('📄 [HomePage] initState - 页面开始初始化'); - - // 🔥 关键:在页面显示之前执行初始化 - // 使用 addPostFrameCallback 确保 build 完成后立即执行 WidgetsBinding.instance.addPostFrameCallback((_) { _initializePage(); }); @@ -107,48 +83,25 @@ class _HomePageState extends State { @override Widget build(BuildContext context) { - debugPrint('🎨 [HomePage] build - 渲染 UI'); - - // 同时监听用户和设备状态 final userState = context.watch().state; final deviceState = context.watch().state; - final currentDevice = deviceState.selectedDevice; - final defaultDevice = DeviceEntity( - deviceName: '暂未绑定设备', - productId: 00000, - productName: '', - tenantId: 00000, - tenantName: '' - ); - - // 如果没有绑定设备,展示未绑定设备的提示页面 - if (currentDevice == null) { - debugPrint('ℹ️ [HomePage] 无选中设备,显示默认设备'); - - return Scaffold( - backgroundColor: const Color(0xFFF7F7F7), - body: CustomScrollView( - physics: const BouncingScrollPhysics(), - slivers: [ - SliverToBoxAdapter(child: ImmersionHeader(device: defaultDevice)), - const SliverToBoxAdapter(child: QuickActionsGrid()), - const SliverToBoxAdapter(child: WorkParamsCard()), - const SliverToBoxAdapter(child: SizedBox(height: 120)), - ], - ), - ); + // 🔥 加载中 → 显示 Loading + if (_isDevicesLoading) { + return const Scaffold(body: Center(child: CircularProgressIndicator())); } - // 如果有绑定设备,正常展示页面 - debugPrint('✅ [HomePage] 有选中设备:${currentDevice.deviceName}'); + // 🔥 现在取的设备 = 100% 最新加载完成的 + final currentDevice = deviceState.selectedDevice; + + final defaultDevice = DeviceEntity(deviceName: '暂未绑定设备', productId: 00000, productName: '', tenantId: 00000, tenantName: ''); return Scaffold( backgroundColor: const Color(0xFFF7F7F7), body: CustomScrollView( physics: const BouncingScrollPhysics(), slivers: [ - SliverToBoxAdapter(child: ImmersionHeader(device: currentDevice)), + SliverToBoxAdapter(child: ImmersionHeader(device: currentDevice ?? defaultDevice)), const SliverToBoxAdapter(child: QuickActionsGrid()), SliverToBoxAdapter(child: WorkParamsCard()), const SliverToBoxAdapter(child: SizedBox(height: 120)), @@ -159,7 +112,6 @@ class _HomePageState extends State { @override void dispose() { - debugPrint('🗑️ [HomePage] dispose - 页面销毁'); super.dispose(); } }