From 5653c044a1f5190273741076f78d08373fef424d Mon Sep 17 00:00:00 2001 From: mmc <1556375442@qq.com> Date: Thu, 5 Mar 2026 16:34:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A6=96=E9=A1=B5=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E8=8E=B7=E5=8F=96=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../home/presentation/pages/home_page.dart | 105 +++++++++--------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/lib/features/home/presentation/pages/home_page.dart b/lib/features/home/presentation/pages/home_page.dart index 90e7b5b1..15943387 100644 --- a/lib/features/home/presentation/pages/home_page.dart +++ b/lib/features/home/presentation/pages/home_page.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:maibu_satabot_v2/core/app/app_user_cubit.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'; @@ -11,59 +12,63 @@ import '../widgets/work_params_card.dart'; class HomePage extends StatelessWidget { const HomePage({super.key}); - @override - Widget build(BuildContext context) { - // 同时监听用户和设备状态 - 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) { - return Scaffold( - backgroundColor: const Color(0xFFF7F7F7), - body: CustomScrollView( - physics: const BouncingScrollPhysics(), - slivers: [ + /// 校验选中设备是否有效,无效则返回列表第一个设备 + DeviceEntity? _getValidCurrentDevice({required List deviceList, required DeviceEntity? selectedDevice}) { + if (deviceList.isEmpty) return null; - SliverToBoxAdapter(child: ImmersionHeader(device: defaultDevice)), + // 用deviceName判断设备唯一性(如果有deviceId建议替换为deviceId) + final bool isSelectedDeviceValid = deviceList.any((device) => device.deviceName == selectedDevice?.deviceName); + debugPrint("选中设备有效性:$isSelectedDeviceValid,选中设备名:${selectedDevice?.deviceName},设备列表:${deviceList.map((e) => e.deviceName).toList()}"); - // 2. 功能网格(保持原有逻辑) - const SliverToBoxAdapter(child: QuickActionsGrid()), - - // 3. 作业参数卡片(显示未绑定设备提示) - const SliverToBoxAdapter(child: WorkParamsCard()), - - // 4. 地图区域占位 - const SliverToBoxAdapter(child: SizedBox(height: 120)), - ], - ), - ); - } - - // 如果有绑定设备,正常展示页面 - return Scaffold( - backgroundColor: const Color(0xFFF7F7F7), - body: CustomScrollView( - physics: const BouncingScrollPhysics(), - slivers: [ - // 1. 沉浸式顶部展示区 - SliverToBoxAdapter(child: ImmersionHeader(device: currentDevice)), - - // 2. 功能网格 - const SliverToBoxAdapter(child: QuickActionsGrid()), - - // 3. 作业参数卡片 - SliverToBoxAdapter(child: WorkParamsCard()), - - // 4. 地图区域占位 - const SliverToBoxAdapter(child: SizedBox(height: 120)), - ], - ), - ); + return isSelectedDeviceValid ? selectedDevice : deviceList.first; } + @override + Widget build(BuildContext context) { + // 获取用户状态(确保用户已登录) + final userState = context.watch().state; + final String? username = userState.user?.username; + + // 页面首次渲染完成后,触发设备列表请求(避免同步调用导致的生命周期问题) + WidgetsBinding.instance.addPostFrameCallback((_) { + if (username != null && username.isNotEmpty) { + final devicesCubit = context.read(); + // 仅在设备列表为空或首次进入时请求(避免重复请求) + devicesCubit.fetchAllDevices(username); + } + }); + + // 构建默认设备(无绑定设备时展示) + final DeviceEntity defaultDevice = DeviceEntity(deviceName: '暂未绑定设备', productId: 00000, productName: '', tenantId: 00000, tenantName: ''); + + return BlocBuilder( + builder: (context, deviceState) { + // 3. 正常状态:处理设备数据并渲染页面 + final List deviceList = deviceState.devices ?? []; + final DeviceEntity? selectedDevice = deviceState.selectedDevice; + final DeviceEntity? currentDevice = _getValidCurrentDevice(deviceList: deviceList, selectedDevice: selectedDevice); + + return Scaffold( + backgroundColor: const Color(0xFFF7F7F7), + body: CustomScrollView( + physics: const BouncingScrollPhysics(), + slivers: [ + // 1. 沉浸式头部(优先展示有效设备,无则展示默认设备) + SliverToBoxAdapter(child: ImmersionHeader(device: currentDevice ?? defaultDevice)), + + // 2. 功能网格 + const SliverToBoxAdapter(child: QuickActionsGrid()), + + // 3. 作业参数卡片 + SliverToBoxAdapter(child: WorkParamsCard()), + + // 4. 地图区域占位 + const SliverToBoxAdapter(child: SizedBox(height: 120)), + ], + ), + ); + }, + ); + } }