75 lines
3.3 KiB
Dart
75 lines
3.3 KiB
Dart
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';
|
||
import '../widgets/quick_actions_grid.dart';
|
||
import '../widgets/work_params_card.dart';
|
||
|
||
class HomePage extends StatelessWidget {
|
||
const HomePage({super.key});
|
||
|
||
/// 校验选中设备是否有效,无效则返回列表第一个设备
|
||
DeviceEntity? _getValidCurrentDevice({required List<DeviceEntity> deviceList, required DeviceEntity? selectedDevice}) {
|
||
if (deviceList.isEmpty) return null;
|
||
|
||
// 用deviceName判断设备唯一性(如果有deviceId建议替换为deviceId)
|
||
final bool isSelectedDeviceValid = deviceList.any((device) => device.deviceName == selectedDevice?.deviceName);
|
||
|
||
debugPrint("选中设备有效性:$isSelectedDeviceValid,选中设备名:${selectedDevice?.deviceName},设备列表:${deviceList.map((e) => e.deviceName).toList()}");
|
||
|
||
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)),
|
||
],
|
||
),
|
||
);
|
||
},
|
||
);
|
||
}
|
||
}
|