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,101 +16,176 @@ class AbnormalListView extends StatelessWidget { ...@@ -15,101 +16,176 @@ 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(),
backgroundColor: const Color.fromRGBO(242, 243, 245, 1), );
appBar: AppBar( }
backgroundColor: Colors.white, }
elevation: 0,
leading: IconButton(
icon: const Icon(Icons.arrow_back_ios,
color: Color.fromRGBO(100, 116, 139, 1)),
onPressed: () {
context.popRoute();
},
),
title: Text(
'告警信息',
style: TextStyle(
color: const Color.fromRGBO(10, 13, 20, 1),
fontSize: 32.sp,
fontWeight: FontWeight.w200,
),
),
centerTitle: true,
),
body: BlocBuilder<AbnormalListCubit, AbnormalListState>(
builder: (context, state) {
final cubit = context.read<AbnormalListCubit>();
if (state.isLoading) { class _AbnormalListBody extends StatefulWidget {
return const Center(child: CircularProgressIndicator()); const _AbnormalListBody();
}
if (state.error != null) { @override
return Center( State<_AbnormalListBody> createState() => _AbnormalListBodyState();
child: Padding( }
padding: EdgeInsets.symmetric(horizontal: 28.w),
child: Column( class _AbnormalListBodyState extends State<_AbnormalListBody> {
mainAxisSize: MainAxisSize.min, late final PagingController<int, AlarmItem> _pagingController;
children: [
Text( @override
'加载失败: ${state.error}', void initState() {
style: TextStyle( super.initState();
fontSize: 28.sp, _pagingController = PagingController<int, AlarmItem>(
color: const Color.fromRGBO(255, 100, 101, 1), getNextPageKey: (state) {
), if (state.keys == null || state.keys!.isEmpty) return 1;
textAlign: TextAlign.center, return state.lastPageIsEmpty ? null : state.nextIntPageKey;
), },
SizedBox(height: 24.h), fetchPage: (pageKey) =>
ElevatedButton( context.read<AbnormalListCubit>().fetchPage(pageKey),
onPressed: () => cubit.loadList(), );
child: Text('重新加载', style: TextStyle(fontSize: 28.sp)), }
),
], @override
), void dispose() {
), _pagingController.dispose();
); super.dispose();
} }
@override
Widget build(BuildContext context) {
final cubit = context.read<AbnormalListCubit>();
return SingleChildScrollView( return BlocBuilder<AbnormalListCubit, AbnormalListState>(
padding: EdgeInsets.symmetric(horizontal: 28.w), builder: (context, state) {
child: Column( return Scaffold(
children: [ backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
FilterTabBlock( appBar: AppBar(
currentFilter: state.currentFilter, backgroundColor: Colors.white,
onFilterChanged: cubit.setFilter, elevation: 0,
allCount: state.allCount, leading: IconButton(
pendingCount: state.pendingCount, icon: const Icon(Icons.arrow_back_ios,
processedCount: state.processedCount, color: Color.fromRGBO(100, 116, 139, 1)),
falseAlarmCount: state.falseAlarmCount, onPressed: () {
context.popRoute();
},
),
title: Text(
'告警信息',
style: TextStyle(
color: const Color.fromRGBO(10, 13, 20, 1),
fontSize: 32.sp,
fontWeight: FontWeight.w200,
),
),
centerTitle: true,
),
body: PagingListener<int, AlarmItem>(
controller: _pagingController,
builder: (context, pagingState, fetchNextPage) {
return CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Padding(
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),
],
),
),
), ),
SizedBox(height: 10.h), SliverPadding(
if (state.filteredList.isEmpty) padding: EdgeInsets.symmetric(horizontal: 28.w),
Padding( sliver: PagedSliverList<int, AlarmItem>(
padding: EdgeInsets.only(top: 60.h), state: pagingState,
child: Text( fetchNextPage: fetchNextPage,
'暂无数据', builderDelegate:
style: TextStyle( PagedChildBuilderDelegate<AlarmItem>(
fontSize: 28.sp, firstPageErrorIndicatorBuilder: (_) => Center(
color: const Color.fromRGBO(100, 116, 139, 1), child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'加载失败: ${pagingState.error}',
style: TextStyle(
fontSize: 28.sp,
color: const Color.fromRGBO(
255, 100, 101, 1),
),
textAlign: TextAlign.center,
),
SizedBox(height: 24.h),
ElevatedButton(
onPressed: fetchNextPage,
child: Text('重新加载',
style:
TextStyle(fontSize: 28.sp)),
),
],
),
),
noItemsFoundIndicatorBuilder: (_) => Padding(
padding: EdgeInsets.only(top: 60.h),
child: Text(
'暂无数据',
style: TextStyle(
fontSize: 28.sp,
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)),
),
],
),
),
), ),
itemBuilder: (_, item, __) {
return AlarmListItem(
item: item,
onTap: (i) =>
cubit.onAlarmItemTap(i, context),
);
},
), ),
) ),
else ),
...state.filteredList.map((item) { SliverToBoxAdapter(
return AlarmListItem( child: SizedBox(height: 30.h),
item: item, ),
onTap: (i) =>
cubit.onAlarmItemTap(i, context),
);
}).toList(),
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