修复首页设备获取的逻辑
This commit is contained in:
@@ -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<AppUserCubit>().state;
|
||||
final deviceState = context.watch<DevicesCubit>().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<DeviceEntity> 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<AppUserCubit>().state;
|
||||
final String? username = userState.user?.username;
|
||||
|
||||
// 页面首次渲染完成后,触发设备列表请求(避免同步调用导致的生命周期问题)
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (username != null && username.isNotEmpty) {
|
||||
final devicesCubit = context.read<DevicesCubit>();
|
||||
// 仅在设备列表为空或首次进入时请求(避免重复请求)
|
||||
devicesCubit.fetchAllDevices(username);
|
||||
}
|
||||
});
|
||||
|
||||
// 构建默认设备(无绑定设备时展示)
|
||||
final DeviceEntity defaultDevice = DeviceEntity(deviceName: '暂未绑定设备', productId: 00000, productName: '', tenantId: 00000, tenantName: '');
|
||||
|
||||
return BlocBuilder<DevicesCubit, DevicesState>(
|
||||
builder: (context, deviceState) {
|
||||
// 3. 正常状态:处理设备数据并渲染页面
|
||||
final List<DeviceEntity> 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)),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user