Commit 56619d0f authored by 张宏's avatar 张宏

3

parent 7c60f91c
...@@ -4,6 +4,8 @@ import 'inspection_history_state.dart'; ...@@ -4,6 +4,8 @@ import 'inspection_history_state.dart';
import '../../../../services/inspection_history_service.dart'; import '../../../../services/inspection_history_service.dart';
class InspectionHistoryCubit extends Cubit<InspectionHistoryState> { class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
static const int pageSize = 10;
final InspectionHistoryService _service; final InspectionHistoryService _service;
final int _deviceIdInt; final int _deviceIdInt;
...@@ -12,53 +14,41 @@ class InspectionHistoryCubit extends Cubit<InspectionHistoryState> { ...@@ -12,53 +14,41 @@ class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
required InspectionHistoryService service, required InspectionHistoryService service,
}) : _service = service, }) : _service = service,
_deviceIdInt = int.tryParse(deviceId) ?? 0, _deviceIdInt = int.tryParse(deviceId) ?? 0,
super(InspectionHistoryState( super(InspectionHistoryState(deviceId: deviceId));
deviceId: deviceId,
totalCount: 0,
passCount: 0,
warningCount: 0,
failCount: 0,
records: [],
)) {
loadHistoryData();
}
Future<void> loadHistoryData() async {
emit(state.copyWith(isLoading: true, error: null));
try {
final result = await _service.getList(
pageSize: 10,
pageNum: 1,
deviceId: _deviceIdInt,
);
final records = result.rows.map((row) { /// 供 PagingController.fetchPage 回调使用,每次加载新的一页
return InspectionRecord( Future<List<InspectionRecord>> fetchPage(int pageKey) async {
deviceName: result.deviceInfo.deviceName, final result = await _service.getList(
inspectorName: row.inspectorName, pageSize: pageSize,
inspectionTime: row.inspectTime, pageNum: pageKey,
result: row.inspectStatus, deviceId: _deviceIdInt,
remark: row.remark, );
items: row.itemsDetail
.map((item) => InspectionItemRecord(
name: item.name,
result: item.status,
))
.toList(),
icon: Icons.devices,
);
}).toList();
// 首页时更新统计数
if (pageKey == 1) {
emit(state.copyWith( emit(state.copyWith(
isLoading: false,
totalCount: result.tabCount.total, totalCount: result.tabCount.total,
passCount: result.tabCount.passCount, passCount: result.tabCount.passCount,
warningCount: result.tabCount.warnCount, warningCount: result.tabCount.warnCount,
failCount: result.tabCount.failCount, failCount: result.tabCount.failCount,
records: records,
)); ));
} catch (e) {
emit(state.copyWith(isLoading: false, error: e.toString()));
} }
return result.rows.map((row) {
return InspectionRecord(
deviceName: result.deviceInfo.deviceName,
inspectorName: row.inspectorName,
inspectionTime: row.inspectTime,
result: row.inspectStatus,
remark: row.remark,
items: row.itemsDetail
.map((item) => InspectionItemRecord(
name: item.name,
result: item.status,
))
.toList(),
icon: Icons.devices,
);
}).toList();
} }
} }
\ No newline at end of file
...@@ -7,19 +7,13 @@ class InspectionHistoryState extends Equatable { ...@@ -7,19 +7,13 @@ class InspectionHistoryState extends Equatable {
final int passCount; final int passCount;
final int warningCount; final int warningCount;
final int failCount; final int failCount;
final List<InspectionRecord> records;
final bool isLoading;
final String? error;
const InspectionHistoryState({ const InspectionHistoryState({
required this.deviceId, required this.deviceId,
required this.totalCount, this.totalCount = 0,
required this.passCount, this.passCount = 0,
required this.warningCount, this.warningCount = 0,
required this.failCount, this.failCount = 0,
required this.records,
this.isLoading = false,
this.error,
}); });
InspectionHistoryState copyWith({ InspectionHistoryState copyWith({
...@@ -28,9 +22,6 @@ class InspectionHistoryState extends Equatable { ...@@ -28,9 +22,6 @@ class InspectionHistoryState extends Equatable {
int? passCount, int? passCount,
int? warningCount, int? warningCount,
int? failCount, int? failCount,
List<InspectionRecord>? records,
bool? isLoading,
String? error,
}) { }) {
return InspectionHistoryState( return InspectionHistoryState(
deviceId: deviceId ?? this.deviceId, deviceId: deviceId ?? this.deviceId,
...@@ -38,9 +29,6 @@ class InspectionHistoryState extends Equatable { ...@@ -38,9 +29,6 @@ class InspectionHistoryState extends Equatable {
passCount: passCount ?? this.passCount, passCount: passCount ?? this.passCount,
warningCount: warningCount ?? this.warningCount, warningCount: warningCount ?? this.warningCount,
failCount: failCount ?? this.failCount, failCount: failCount ?? this.failCount,
records: records ?? this.records,
isLoading: isLoading ?? this.isLoading,
error: error,
); );
} }
...@@ -51,9 +39,6 @@ class InspectionHistoryState extends Equatable { ...@@ -51,9 +39,6 @@ class InspectionHistoryState extends Equatable {
passCount, passCount,
warningCount, warningCount,
failCount, failCount,
records,
isLoading,
error,
]; ];
} }
......
...@@ -2,12 +2,13 @@ import 'package:auto_route/auto_route.dart'; ...@@ -2,12 +2,13 @@ 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/repositories/inspection_history_repository.dart'; import 'package:smart_hotel_app/repositories/inspection_history_repository.dart';
import 'package:smart_hotel_app/services/inspection_history_service.dart'; import 'package:smart_hotel_app/services/inspection_history_service.dart';
import 'cubit/inspection_history_cubit.dart'; import 'cubit/inspection_history_cubit.dart';
import 'cubit/inspection_history_state.dart'; import 'cubit/inspection_history_state.dart';
import 'widget/statistics_card.dart'; import 'widget/statistics_card.dart';
import 'widget/inspection_record_list.dart'; import 'widget/inspection_record_item.dart';
@RoutePage() @RoutePage()
class InspectionHistoryView extends StatelessWidget { class InspectionHistoryView extends StatelessWidget {
...@@ -24,51 +25,199 @@ class InspectionHistoryView extends StatelessWidget { ...@@ -24,51 +25,199 @@ class InspectionHistoryView extends StatelessWidget {
repository: InspectionHistoryRepository(), repository: InspectionHistoryRepository(),
), ),
), ),
child: Scaffold( child: const _InspectionHistoryBody(),
backgroundColor: const Color.fromRGBO(242, 243, 245, 1), );
appBar: AppBar( }
backgroundColor: Colors.white, }
elevation: 0,
leading: IconButton( class _InspectionHistoryBody extends StatefulWidget {
icon: const Icon(Icons.arrow_back_ios, const _InspectionHistoryBody();
color: Color.fromRGBO(100, 116, 139, 1)),
onPressed: () { @override
context.popRoute(); State<_InspectionHistoryBody> createState() => _InspectionHistoryBodyState();
}, }
),
title: Text( class _InspectionHistoryBodyState extends State<_InspectionHistoryBody> {
'巡检记录', late final PagingController<int, InspectionRecord> _pagingController;
style: TextStyle(
color: const Color.fromRGBO(10, 13, 20, 1), @override
fontSize: 32.sp, void initState() {
fontWeight: FontWeight.w200, super.initState();
_pagingController = PagingController<int, InspectionRecord>(
getNextPageKey: (state) {
if (state.keys == null || state.keys!.isEmpty) return 1;
final total =
context.read<InspectionHistoryCubit>().state.totalCount;
final nextKey = state.nextIntPageKey;
if (nextKey == null) return null;
if ((nextKey - 1) * InspectionHistoryCubit.pageSize >= total) {
return null;
}
return nextKey;
},
fetchPage: (pageKey) =>
context.read<InspectionHistoryCubit>().fetchPage(pageKey),
);
}
@override
void dispose() {
_pagingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return BlocBuilder<InspectionHistoryCubit, InspectionHistoryState>(
builder: (context, state) {
return Scaffold(
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,
), ),
centerTitle: true, body: PagingListener<int, InspectionRecord>(
), controller: _pagingController,
body: SingleChildScrollView( builder: (context, pagingState, fetchNextPage) {
padding: EdgeInsets.symmetric(horizontal: 28.w, vertical: 20.h), return CustomScrollView(
child: BlocBuilder<InspectionHistoryCubit, InspectionHistoryState>( slivers: [
builder: (context, state) { SliverToBoxAdapter(
return Column( child: Padding(
children: [ padding: EdgeInsets.symmetric(
StatisticsCard( horizontal: 28.w, vertical: 20.h),
totalCount: state.totalCount, child: StatisticsCard(
passCount: state.passCount, totalCount: state.totalCount,
warningCount: state.warningCount, passCount: state.passCount,
failCount: state.failCount, warningCount: state.warningCount,
failCount: state.failCount,
),
),
),
SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.only(
left: 28.w, right: 28.w, bottom: 16.h),
child: Row(
children: [
Text(
'巡检历史',
style: TextStyle(
fontSize: 28.sp,
fontWeight: FontWeight.bold,
color:
const Color.fromRGBO(10, 13, 20, 1.0),
),
),
Text(
' (${state.totalCount})',
style: TextStyle(
fontSize: 28.sp,
fontWeight: FontWeight.bold,
color:
const Color.fromRGBO(10, 13, 20, 1.0),
),
),
],
),
),
), ),
SizedBox(height: 20.h), SliverPadding(
InspectionRecordList( padding: EdgeInsets.symmetric(horizontal: 28.w),
records: state.records, sliver: PagedSliverList<int, InspectionRecord>(
state: pagingState,
fetchNextPage: fetchNextPage,
builderDelegate:
PagedChildBuilderDelegate<InspectionRecord>(
firstPageErrorIndicatorBuilder: (_) => Center(
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 InspectionRecordItem(record: item);
},
),
),
),
SliverToBoxAdapter(
child: SizedBox(height: 30.h),
), ),
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