Commit 378c338a authored by 张宏's avatar 张宏

3

parent 66f73066
...@@ -7,6 +7,7 @@ import 'package:smart_hotel_app/models/bo/room_bo.dart'; ...@@ -7,6 +7,7 @@ import 'package:smart_hotel_app/models/bo/room_bo.dart';
class ServiceIndexCubit extends Cubit<ServiceIndexState> { class ServiceIndexCubit extends Cubit<ServiceIndexState> {
final RoomService _roomService; final RoomService _roomService;
final Map<String, List<Map<String, dynamic>>> _roomCache = {}; final Map<String, List<Map<String, dynamic>>> _roomCache = {};
List<FloorAreaBO> _floorAreasCache = [];
ServiceIndexCubit() ServiceIndexCubit()
: _roomService = RoomService(repository: RoomRepository()), : _roomService = RoomService(repository: RoomRepository()),
...@@ -18,21 +19,18 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> { ...@@ -18,21 +19,18 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> {
await loadFloorAndAreas(); await loadFloorAndAreas();
} }
/// 加载楼层和区域信息 /// 加载楼层和区域信息(仅进入页面时调用一次)
Future<void> loadFloorAndAreas() async { Future<void> loadFloorAndAreas() async {
emit(state.copyWith(isLoading: true, error: null)); emit(state.copyWith(isLoading: true, error: null));
try { try {
final floorAreas = await _roomService.getFloorAreas(); final floorAreas = await _roomService.getFloorAreas();
_floorAreasCache = floorAreas;
final floors = floorAreas.map((f) => {f.floorId: f.floorId.toString()}).toList(); final floors = floorAreas.map((f) => {f.floorId: f.floorId.toString()}).toList();
final areas = <String>{};
for (final f in floorAreas) {
areas.addAll(f.areas);
}
emit(state.copyWith( emit(state.copyWith(
isLoading: false, isLoading: false,
floors: floors, floors: floors,
areas: areas.toList(),
)); ));
// 默认选中第一个楼层,加载其房间数据 // 默认选中第一个楼层,加载其房间数据
...@@ -40,6 +38,7 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> { ...@@ -40,6 +38,7 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> {
final firstFloor = floors.first; final firstFloor = floors.first;
final firstFloorId = firstFloor.keys.first; final firstFloorId = firstFloor.keys.first;
final firstFloorLabel = firstFloor.values.first; final firstFloorLabel = firstFloor.values.first;
_updateAreasForFloor(firstFloorLabel);
selectFloor(firstFloorLabel); selectFloor(firstFloorLabel);
await loadRooms(floorId: firstFloorId); await loadRooms(floorId: firstFloorId);
} }
...@@ -48,6 +47,15 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> { ...@@ -48,6 +47,15 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> {
} }
} }
/// 根据选中的楼层更新 areas
void _updateAreasForFloor(String floorLabel) {
final matched = _floorAreasCache.where(
(f) => f.floorId.toString() == floorLabel,
);
final areas = matched.isNotEmpty ? matched.first.areas : <String>[];
emit(state.copyWith(areas: areas));
}
/// 加载房间状态列表 /// 加载房间状态列表
Future<void> loadRooms({ Future<void> loadRooms({
int? floorId, int? floorId,
...@@ -137,6 +145,9 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> { ...@@ -137,6 +145,9 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> {
void selectFloor(String? floor) { void selectFloor(String? floor) {
emit(state.copyWith(selectedFloor: floor)); emit(state.copyWith(selectedFloor: floor));
if (floor != null) {
_updateAreasForFloor(floor);
}
} }
void selectType(String? type) { void selectType(String? type) {
......
...@@ -125,6 +125,7 @@ class _RoomManagementBlockState extends State<RoomManagementBlock> { ...@@ -125,6 +125,7 @@ class _RoomManagementBlockState extends State<RoomManagementBlock> {
} else { } else {
_selectedFloor = floorName; _selectedFloor = floorName;
} }
_selectedType = null;
_searchController.clear(); _searchController.clear();
_searchText = ''; _searchText = '';
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment