import 'package:flutter_bloc/flutter_bloc.dart'; import 'warning_center_event.dart'; import 'warning_center_state.dart'; class WarningCenterBloc extends Bloc { WarningCenterBloc() : super(const WarningCenterInitial()) { on(_onLoadData); on(_onFilterByLevel); on(_onFilterByTime); on(_onSearch); } Future _onLoadData( WarningCenterLoadData event, Emitter emit, ) async { emit(const WarningCenterLoading()); try { await Future.delayed(const Duration(milliseconds: 500)); final stats = const WarningStats( totalCount: 156, todayCount: 156, severeCount: 8, processingCount: 15, closedCount: 133, avgResponseTime: 2.3, changePercent: -12, ); final warnings = [ const WarningItem( id: '1', level: '严重', deviceName: '逆变器12', deviceType: '组串式', content: '逆变器停机', status: '处理中', time: '2025-05-25 09:18:32', duration: '2小时18分', responsiblePerson: '张运维', ), const WarningItem( id: '2', level: '重要', deviceName: '逆变器13', deviceType: '组串式', content: '逆变器停机', status: '处理中', time: '2025-05-25 09:18:32', duration: '2小时18分', responsiblePerson: '张运维', ), const WarningItem( id: '3', level: '严重', deviceName: '逆变器05', deviceType: '组串式', content: '逆变器停机', status: '处理中', time: '2025-05-25 09:18:32', duration: '2小时18分', responsiblePerson: '张运维', ), const WarningItem( id: '4', level: '一般', deviceName: '逆变器08', deviceType: '组串式', content: '温度过高告警', status: '待处理', time: '2025-05-25 08:30:15', duration: '3小时05分', responsiblePerson: '李工程师', ), const WarningItem( id: '5', level: '提示', deviceName: '逆变器15', deviceType: '组串式', content: '效率低于阈值', status: '已关闭', time: '2025-05-24 16:45:20', duration: '1天5小时', responsiblePerson: '王技术员', ), const WarningItem( id: '6', level: '严重', deviceName: '汇流箱03', deviceType: '直流汇流', content: '通信中断', status: '处理中', time: '2025-05-25 07:20:10', duration: '4小时15分', responsiblePerson: '张运维', ), const WarningItem( id: '7', level: '重要', deviceName: '逆变器20', deviceType: '组串式', content: '输出电压异常', status: '待处理', time: '2025-05-25 10:05:30', duration: '1小时30分', responsiblePerson: '赵工程师', ), const WarningItem( id: '8', level: '一般', deviceName: '变压器02', deviceType: '升压变压器', content: '负载率超过80%', status: '已关闭', time: '2025-05-24 14:20:45', duration: '1天10小时', responsiblePerson: '李工程师', ), ]; emit(WarningCenterLoaded( stats: stats, warnings: warnings, )); } catch (e) { emit(WarningCenterError('加载数据失败:$e')); } } Future _onFilterByLevel( WarningCenterFilterByLevel event, Emitter emit, ) async { if (state is WarningCenterLoaded) { final currentState = state as WarningCenterLoaded; List filteredWarnings; if (event.level == '全部') { filteredWarnings = currentState.warnings; } else { filteredWarnings = currentState.warnings .where((w) => w.level == event.level) .toList(); } emit(currentState.copyWith( selectedLevel: event.level, warnings: filteredWarnings, )); } } Future _onFilterByTime( WarningCenterFilterByTime event, Emitter emit, ) async { if (state is WarningCenterLoaded) { final currentState = state as WarningCenterLoaded; emit(currentState.copyWith( selectedTimeRange: event.timeRange, )); } } Future _onSearch( WarningCenterSearch event, Emitter emit, ) async { if (state is WarningCenterLoaded) { final currentState = state as WarningCenterLoaded; List filteredWarnings; if (event.keyword.isEmpty) { filteredWarnings = currentState.warnings; } else { filteredWarnings = currentState.warnings .where((w) => w.deviceName.contains(event.keyword) || w.content.contains(event.keyword) || w.deviceType.contains(event.keyword)) .toList(); } emit(currentState.copyWith( searchKeyword: event.keyword, warnings: filteredWarnings, )); } } }