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

3

parent 7c60f91c
......@@ -4,6 +4,8 @@ import 'inspection_history_state.dart';
import '../../../../services/inspection_history_service.dart';
class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
static const int pageSize = 10;
final InspectionHistoryService _service;
final int _deviceIdInt;
......@@ -12,27 +14,27 @@ class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
required InspectionHistoryService service,
}) : _service = service,
_deviceIdInt = int.tryParse(deviceId) ?? 0,
super(InspectionHistoryState(
deviceId: deviceId,
totalCount: 0,
passCount: 0,
warningCount: 0,
failCount: 0,
records: [],
)) {
loadHistoryData();
}
super(InspectionHistoryState(deviceId: deviceId));
Future<void> loadHistoryData() async {
emit(state.copyWith(isLoading: true, error: null));
try {
/// 供 PagingController.fetchPage 回调使用,每次加载新的一页
Future<List<InspectionRecord>> fetchPage(int pageKey) async {
final result = await _service.getList(
pageSize: 10,
pageNum: 1,
pageSize: pageSize,
pageNum: pageKey,
deviceId: _deviceIdInt,
);
final records = result.rows.map((row) {
// 首页时更新统计数
if (pageKey == 1) {
emit(state.copyWith(
totalCount: result.tabCount.total,
passCount: result.tabCount.passCount,
warningCount: result.tabCount.warnCount,
failCount: result.tabCount.failCount,
));
}
return result.rows.map((row) {
return InspectionRecord(
deviceName: result.deviceInfo.deviceName,
inspectorName: row.inspectorName,
......@@ -48,17 +50,5 @@ class InspectionHistoryCubit extends Cubit<InspectionHistoryState> {
icon: Icons.devices,
);
}).toList();
emit(state.copyWith(
isLoading: false,
totalCount: result.tabCount.total,
passCount: result.tabCount.passCount,
warningCount: result.tabCount.warnCount,
failCount: result.tabCount.failCount,
records: records,
));
} catch (e) {
emit(state.copyWith(isLoading: false, error: e.toString()));
}
}
}
\ No newline at end of file
......@@ -7,19 +7,13 @@ class InspectionHistoryState extends Equatable {
final int passCount;
final int warningCount;
final int failCount;
final List<InspectionRecord> records;
final bool isLoading;
final String? error;
const InspectionHistoryState({
required this.deviceId,
required this.totalCount,
required this.passCount,
required this.warningCount,
required this.failCount,
required this.records,
this.isLoading = false,
this.error,
this.totalCount = 0,
this.passCount = 0,
this.warningCount = 0,
this.failCount = 0,
});
InspectionHistoryState copyWith({
......@@ -28,9 +22,6 @@ class InspectionHistoryState extends Equatable {
int? passCount,
int? warningCount,
int? failCount,
List<InspectionRecord>? records,
bool? isLoading,
String? error,
}) {
return InspectionHistoryState(
deviceId: deviceId ?? this.deviceId,
......@@ -38,9 +29,6 @@ class InspectionHistoryState extends Equatable {
passCount: passCount ?? this.passCount,
warningCount: warningCount ?? this.warningCount,
failCount: failCount ?? this.failCount,
records: records ?? this.records,
isLoading: isLoading ?? this.isLoading,
error: error,
);
}
......@@ -51,9 +39,6 @@ class InspectionHistoryState extends Equatable {
passCount,
warningCount,
failCount,
records,
isLoading,
error,
];
}
......
......@@ -2,12 +2,13 @@ import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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/services/inspection_history_service.dart';
import 'cubit/inspection_history_cubit.dart';
import 'cubit/inspection_history_state.dart';
import 'widget/statistics_card.dart';
import 'widget/inspection_record_list.dart';
import 'widget/inspection_record_item.dart';
@RoutePage()
class InspectionHistoryView extends StatelessWidget {
......@@ -24,7 +25,52 @@ class InspectionHistoryView extends StatelessWidget {
repository: InspectionHistoryRepository(),
),
),
child: Scaffold(
child: const _InspectionHistoryBody(),
);
}
}
class _InspectionHistoryBody extends StatefulWidget {
const _InspectionHistoryBody();
@override
State<_InspectionHistoryBody> createState() => _InspectionHistoryBodyState();
}
class _InspectionHistoryBodyState extends State<_InspectionHistoryBody> {
late final PagingController<int, InspectionRecord> _pagingController;
@override
void initState() {
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,
......@@ -46,29 +92,132 @@ class InspectionHistoryView extends StatelessWidget {
),
centerTitle: true,
),
body: SingleChildScrollView(
padding: EdgeInsets.symmetric(horizontal: 28.w, vertical: 20.h),
child: BlocBuilder<InspectionHistoryCubit, InspectionHistoryState>(
builder: (context, state) {
return Column(
children: [
StatisticsCard(
body: PagingListener<int, InspectionRecord>(
controller: _pagingController,
builder: (context, pagingState, fetchNextPage) {
return CustomScrollView(
slivers: [
SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: 28.w, vertical: 20.h),
child: StatisticsCard(
totalCount: state.totalCount,
passCount: state.passCount,
warningCount: state.warningCount,
failCount: state.failCount,
),
SizedBox(height: 20.h),
InspectionRecordList(
records: state.records,
),
SizedBox(height: 30.h),
),
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),
),
),
],
);
),
),
),
SliverPadding(
padding: EdgeInsets.symmetric(horizontal: 28.w),
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),
),
],
);
},
),
);
},
);
}
}
\ 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