Commit 97313115 authored by 张宏's avatar 张宏

parent e4085511
...@@ -4,9 +4,13 @@ import '../models/bo/alert_list_bo.dart'; ...@@ -4,9 +4,13 @@ import '../models/bo/alert_list_bo.dart';
class AlertListRepository { class AlertListRepository {
/// 获取告警列表 /// 获取告警列表
Future<ResponseModel<AlertListBO>> getList() { Future<ResponseModel<AlertListBO>> getList({
required int pageSize,
required int pageNum,
}) {
return DioRequest.instance.get<AlertListBO>( return DioRequest.instance.get<AlertListBO>(
'/app/alert/list', '/app/alert/list',
queryParameters: {'pageSize': pageSize, 'pageNum': pageNum},
fromJsonT: (data) => fromJsonT: (data) =>
AlertListBO.fromJson(data as Map<String, dynamic>), AlertListBO.fromJson(data as Map<String, dynamic>),
); );
......
...@@ -7,8 +7,11 @@ class AlertListService { ...@@ -7,8 +7,11 @@ class AlertListService {
AlertListService({required AlertListRepository repository}) AlertListService({required AlertListRepository repository})
: _repository = repository; : _repository = repository;
Future<AlertListBO> getList() async { Future<AlertListBO> getList({required int pageSize, required int pageNum}) async {
final result = await _repository.getList(); final result = await _repository.getList(
pageSize: pageSize,
pageNum: pageNum,
);
if (result.success && result.data != null) { if (result.success && result.data != null) {
return result.data!; return result.data!;
} }
......
...@@ -9,29 +9,34 @@ import 'package:smart_hotel_app/views/home/abnormal_list/cubit/abnormal_list_sta ...@@ -9,29 +9,34 @@ import 'package:smart_hotel_app/views/home/abnormal_list/cubit/abnormal_list_sta
class AbnormalListCubit extends Cubit<AbnormalListState> { class AbnormalListCubit extends Cubit<AbnormalListState> {
final AlertListService _service; final AlertListService _service;
static const int _pageSize = 20;
AbnormalListCubit() AbnormalListCubit()
: _service = AlertListService( : _service = AlertListService(
repository: AlertListRepository(), repository: AlertListRepository(),
), ),
super(const AbnormalListState()) { super(const AbnormalListState());
loadList();
}
Future<void> loadList() async { /// 供 PagingController.fetchPage 回调使用,每次加载新的一页
emit(const AbnormalListState(isLoading: true)); Future<List<AlarmItem>> fetchPage(int pageKey) async {
try { final alertList = await _service.getList(
final alertList = await _service.getList(); pageSize: _pageSize,
emit(AbnormalListState( pageNum: pageKey,
alarmList: );
alertList.rows.map((e) => AlarmItem.fromAlertListItemBO(e)).toList(),
// 首頁時更新 tab 统计数
if (pageKey == 1) {
emit(state.copyWith(
allCount: alertList.tabCount.all, allCount: alertList.tabCount.all,
pendingCount: alertList.tabCount.pending, pendingCount: alertList.tabCount.pending,
processedCount: alertList.tabCount.processed, processedCount: alertList.tabCount.processed,
falseAlarmCount: alertList.tabCount.falseAlarm, falseAlarmCount: alertList.tabCount.falseAlarm,
)); ));
} catch (e) {
emit(AbnormalListState(error: e.toString()));
} }
return alertList.rows
.map((e) => AlarmItem.fromAlertListItemBO(e))
.toList();
} }
void setFilter(AlarmStatus filter) { void setFilter(AlarmStatus filter) {
......
...@@ -61,7 +61,6 @@ class AbnormalListState extends Equatable { ...@@ -61,7 +61,6 @@ class AbnormalListState extends Equatable {
final bool isLoading; final bool isLoading;
final String? error; final String? error;
final AlarmStatus currentFilter; final AlarmStatus currentFilter;
final List<AlarmItem> alarmList;
final int allCount; final int allCount;
final int pendingCount; final int pendingCount;
final int processedCount; final int processedCount;
...@@ -71,7 +70,6 @@ class AbnormalListState extends Equatable { ...@@ -71,7 +70,6 @@ class AbnormalListState extends Equatable {
this.isLoading = false, this.isLoading = false,
this.error, this.error,
this.currentFilter = AlarmStatus.all, this.currentFilter = AlarmStatus.all,
this.alarmList = const [],
this.allCount = 0, this.allCount = 0,
this.pendingCount = 0, this.pendingCount = 0,
this.processedCount = 0, this.processedCount = 0,
...@@ -82,7 +80,6 @@ class AbnormalListState extends Equatable { ...@@ -82,7 +80,6 @@ class AbnormalListState extends Equatable {
bool? isLoading, bool? isLoading,
String? error, String? error,
AlarmStatus? currentFilter, AlarmStatus? currentFilter,
List<AlarmItem>? alarmList,
int? allCount, int? allCount,
int? pendingCount, int? pendingCount,
int? processedCount, int? processedCount,
...@@ -92,7 +89,6 @@ class AbnormalListState extends Equatable { ...@@ -92,7 +89,6 @@ class AbnormalListState extends Equatable {
isLoading: isLoading ?? this.isLoading, isLoading: isLoading ?? this.isLoading,
error: error, error: error,
currentFilter: currentFilter ?? this.currentFilter, currentFilter: currentFilter ?? this.currentFilter,
alarmList: alarmList ?? this.alarmList,
allCount: allCount ?? this.allCount, allCount: allCount ?? this.allCount,
pendingCount: pendingCount ?? this.pendingCount, pendingCount: pendingCount ?? this.pendingCount,
processedCount: processedCount ?? this.processedCount, processedCount: processedCount ?? this.processedCount,
...@@ -100,19 +96,11 @@ class AbnormalListState extends Equatable { ...@@ -100,19 +96,11 @@ class AbnormalListState extends Equatable {
); );
} }
List<AlarmItem> get filteredList {
if (currentFilter == AlarmStatus.all) {
return alarmList;
}
return alarmList.where((item) => item.status == currentFilter).toList();
}
@override @override
List<Object?> get props => [ List<Object?> get props => [
isLoading, isLoading,
error, error,
currentFilter, currentFilter,
alarmList,
allCount, allCount,
pendingCount, pendingCount,
processedCount, processedCount,
......
...@@ -2,6 +2,7 @@ import 'package:auto_route/auto_route.dart'; ...@@ -2,6 +2,7 @@ import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:smart_hotel_app/views/home/abnormal_list/cubit/abnormal_list_cubit.dart'; import 'package:smart_hotel_app/views/home/abnormal_list/cubit/abnormal_list_cubit.dart';
import 'package:smart_hotel_app/views/home/abnormal_list/cubit/abnormal_list_state.dart'; import 'package:smart_hotel_app/views/home/abnormal_list/cubit/abnormal_list_state.dart';
import 'package:smart_hotel_app/views/home/abnormal_list/widget/alarm_list_item.dart'; import 'package:smart_hotel_app/views/home/abnormal_list/widget/alarm_list_item.dart';
...@@ -15,7 +16,47 @@ class AbnormalListView extends StatelessWidget { ...@@ -15,7 +16,47 @@ class AbnormalListView extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
create: (_) => AbnormalListCubit(), create: (_) => AbnormalListCubit(),
child: Scaffold( child: const _AbnormalListBody(),
);
}
}
class _AbnormalListBody extends StatefulWidget {
const _AbnormalListBody();
@override
State<_AbnormalListBody> createState() => _AbnormalListBodyState();
}
class _AbnormalListBodyState extends State<_AbnormalListBody> {
late final PagingController<int, AlarmItem> _pagingController;
@override
void initState() {
super.initState();
_pagingController = PagingController<int, AlarmItem>(
getNextPageKey: (state) {
if (state.keys == null || state.keys!.isEmpty) return 1;
return state.lastPageIsEmpty ? null : state.nextIntPageKey;
},
fetchPage: (pageKey) =>
context.read<AbnormalListCubit>().fetchPage(pageKey),
);
}
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final cubit = context.read<AbnormalListCubit>();
return BlocBuilder<AbnormalListCubit, AbnormalListState>(
builder: (context, state) {
return Scaffold(
backgroundColor: const Color.fromRGBO(242, 243, 245, 1), backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
appBar: AppBar( appBar: AppBar(
backgroundColor: Colors.white, backgroundColor: Colors.white,
...@@ -37,79 +78,114 @@ class AbnormalListView extends StatelessWidget { ...@@ -37,79 +78,114 @@ class AbnormalListView extends StatelessWidget {
), ),
centerTitle: true, centerTitle: true,
), ),
body: BlocBuilder<AbnormalListCubit, AbnormalListState>( body: PagingListener<int, AlarmItem>(
builder: (context, state) { controller: _pagingController,
final cubit = context.read<AbnormalListCubit>(); builder: (context, pagingState, fetchNextPage) {
return CustomScrollView(
if (state.isLoading) { slivers: [
return const Center(child: CircularProgressIndicator()); SliverToBoxAdapter(
}
if (state.error != null) {
return Center(
child: Padding( child: Padding(
padding: EdgeInsets.symmetric(horizontal: 28.w), padding: EdgeInsets.symmetric(horizontal: 28.w),
child: Column( child: Column(
children: [
FilterTabBlock(
currentFilter: state.currentFilter,
onFilterChanged: cubit.setFilter,
allCount: state.allCount,
pendingCount: state.pendingCount,
processedCount: state.processedCount,
falseAlarmCount: state.falseAlarmCount,
),
SizedBox(height: 10.h),
],
),
),
),
SliverPadding(
padding: EdgeInsets.symmetric(horizontal: 28.w),
sliver: PagedSliverList<int, AlarmItem>(
state: pagingState,
fetchNextPage: fetchNextPage,
builderDelegate:
PagedChildBuilderDelegate<AlarmItem>(
firstPageErrorIndicatorBuilder: (_) => Center(
child: Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
Text( Text(
'加载失败: ${state.error}', '加载失败: ${pagingState.error}',
style: TextStyle( style: TextStyle(
fontSize: 28.sp, fontSize: 28.sp,
color: const Color.fromRGBO(255, 100, 101, 1), color: const Color.fromRGBO(
255, 100, 101, 1),
), ),
textAlign: TextAlign.center, textAlign: TextAlign.center,
), ),
SizedBox(height: 24.h), SizedBox(height: 24.h),
ElevatedButton( ElevatedButton(
onPressed: () => cubit.loadList(), onPressed: fetchNextPage,
child: Text('重新加载', style: TextStyle(fontSize: 28.sp)), child: Text('重新加载',
style:
TextStyle(fontSize: 28.sp)),
), ),
], ],
), ),
), ),
); noItemsFoundIndicatorBuilder: (_) => Padding(
}
return SingleChildScrollView(
padding: EdgeInsets.symmetric(horizontal: 28.w),
child: Column(
children: [
FilterTabBlock(
currentFilter: state.currentFilter,
onFilterChanged: cubit.setFilter,
allCount: state.allCount,
pendingCount: state.pendingCount,
processedCount: state.processedCount,
falseAlarmCount: state.falseAlarmCount,
),
SizedBox(height: 10.h),
if (state.filteredList.isEmpty)
Padding(
padding: EdgeInsets.only(top: 60.h), padding: EdgeInsets.only(top: 60.h),
child: Text( child: Text(
'暂无数据', '暂无数据',
style: TextStyle( style: TextStyle(
fontSize: 28.sp, fontSize: 28.sp,
color: const Color.fromRGBO(100, 116, 139, 1), color: const Color.fromRGBO(
100, 116, 139, 1),
),
),
),
newPageErrorIndicatorBuilder: (_) => Center(
child: Padding(
padding:
EdgeInsets.symmetric(vertical: 20.h),
child: Column(
children: [
Text(
'加载失败: ${pagingState.error}',
style: TextStyle(
fontSize: 24.sp,
color: const Color.fromRGBO(
255, 100, 101, 1),
),
),
SizedBox(height: 12.h),
TextButton(
onPressed: fetchNextPage,
child: Text('重试',
style:
TextStyle(fontSize: 24.sp)),
),
],
), ),
), ),
) ),
else itemBuilder: (_, item, __) {
...state.filteredList.map((item) {
return AlarmListItem( return AlarmListItem(
item: item, item: item,
onTap: (i) => onTap: (i) =>
cubit.onAlarmItemTap(i, context), cubit.onAlarmItemTap(i, context),
); );
}).toList(), },
SizedBox(height: 30.h), ),
], ),
), ),
SliverToBoxAdapter(
child: SizedBox(height: 30.h),
),
],
); );
}, },
), ),
), );
},
); );
} }
} }
\ No newline at end of file
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